[wxPython] LED Number Control set w/ variable

Post here if you need help with creating a Graphical User Interface in Python.

[wxPython] LED Number Control set w/ variable

Postby ms4sman » Sat Feb 16, 2013 6:43 pm

I am so glad this forum is back online, and I am going to jump right in.
I am building a GUI for a school-related programming project. I am using a LEDNumberCtrl, and I want to set its value using a variable, but I get an error when I do.
Code: Select all
self.lEDNumberCtrl1.SetValue(ave)

"ave" being the variable. Here is the error I get: AttributeError : 'LEDNumberCtrl' object has no attribute 'SetValue(ave)'
I have never used an LED number control before, so this is new. I assume it means that the control doesnt take a variable, and it assumes that it is a string, but it is actually a number. I am sure the solution is simple, but I just don't know where to go with it. Any ideas? Thanks!
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: LED Number Control set w/ variable

Postby stranac » Sat Feb 16, 2013 7:18 pm

ms4sman wrote:
Code: Select all
self.lEDNumberCtrl1.SetValue(ave)

"ave" being the variable. Here is the error I get: AttributeError : 'LEDNumberCtrl' object has no attribute 'SetValue(ave)'

You're gonna have to show us more code, and the entire error traceback.

The only things I can say for now are:
  1. wx.gizmos.LEDNumberCtrl definitely has a SetValue() method
  2. the argument should be a string
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: LED Number Control set w/ variable

Postby ms4sman » Sat Feb 16, 2013 7:29 pm

That is the entire traceback I get. The other line just tells the filename and line the error occured on.
Code to create the LED number Ctrl:
Code: Select all
 self.lEDNumberCtrl1 = wx.gizmos.LEDNumberCtrl(id=wxID_EVALUATELEDNUMBERCTRL1,
              parent=self, pos=wx.Point(344, 104), size=wx.Size(64, 48),
              style=wx.gizmos.LED_ALIGN_LEFT)
        self.lEDNumberCtrl1.SetValue('0.0')
        self.lEDNumberCtrl1.SetLabel('control')
        self.lEDNumberCtrl1.SetToolTipString('')

This is all the code pertaining to the LED control.
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: LED Number Control set w/ variable

Postby stranac » Sat Feb 16, 2013 7:47 pm

I had to remove the id, since that seems to be something you define earlier in your code, but other than that, this code works fine for me.
Here's what I get:
LEDNumberCtrl.png
LEDNumberCtrl.png (2.62 KiB) Viewed 2675 times

So, I guess you'll have to show all of your code.

As for the traceback, even if the information in it doesn't seem relevant, post it anyway.
You never know, it might help.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: LED Number Control set w/ variable

Postby Yoriz » Sat Feb 16, 2013 8:36 pm

Out of interest before the
Code: Select all
self.lEDNumberCtrl1.SetValue(ave)
line that errors
put a
Code: Select all
print self.lEDNumberCtrl1
to see if your variable is actually pointing to a led control, because your error is saying that it has no method SetValue.
you should get something like the following printed.
Code: Select all
<wx.gizmos.LEDNumberCtrl; proxy of <Swig Object of type 'wxLEDNumberCtrl *' at 0x42f35c0> >
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 724
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: LED Number Control set w/ variable

Postby ms4sman » Sat Feb 16, 2013 9:37 pm

AttributeError : 'LEDNumberCtrl' object has no attribute 'SetValue(ave)'
234: evaluate.py: self.lEDNumberCtrl1.SetValue(ave)

That is word for word exact.

I tried the line you gave Yoriz, and nothing changed.

Stranac, the program runs, and builds the control, but there is a button, which updates the numbers in the LED and when I push the button, I get the error.

The reason I am reluctant to post code is because I do not code very efficiently and I know it is probably full of errors. When I have posted code in the past, people just tell me all my other problems that I am already aware of, and don't help with what I am asking about.
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: LED Number Control set w/ variable

Postby stranac » Sat Feb 16, 2013 9:47 pm

ms4sman wrote:The reason I am reluctant to post code is because I do not code very efficiently and I know it is probably full of errors. When I have posted code in the past, people just tell me all my other problems that I am already aware of, and don't help with what I am asking about.

Don't worry, we won't just point out other problems you have.
We will also try to help with the one you're asking about.

The reason we point out other problems is, forums are not meant to help a single user, once.
Other people will run into what's written here, and if they see code that works, and no one points out the mistakes, they could assume it's the best way to do it.

Anyway, it's hard to help without actually seeing the problem.
The code you posted works fine.
There's no way for me to guess what's wrong later, in the code you haven't shown.

If you(for whatever reason) don't want to show the actual code, at least show a basic(runnable, please) example that reproduces the error.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: LED Number Control set w/ variable

Postby Yoriz » Sat Feb 16, 2013 11:12 pm

ms4sman wrote:
I tried the line you gave Yoriz, and nothing changed.



It wasn't meant to change anything it just a print statement, i get the feeling that your variable 'self.lEDNumberCtrl1' does not contain a led control and to check that i asked you to print the result of that variable.
what was the result of the print statement i asked you to try in the previous post ?
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 724
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: LED Number Control set w/ variable

Postby ms4sman » Sun Feb 17, 2013 6:22 pm

stranac wrote:It wasn't meant to change anything it just a print statement

Sorry, when I said nothing changed, I meant that there was no output that I could see.

However, when I tried to recreate the problem in a less cluttered app for you guys to see, I found part of my problem. The error I showed you was different. I know what happened, but it would be hard to explain. Here is some code that reproduces the real problem. Yes, I still use Boa Constructor, and I know it is outdated, but I still like it. Don't hate! :)

Here is the App:
Code: Select all
#!/usr/bin/env python
#Boa:App:BoaApp

import wx

import Frame1

modules ={'Frame1': [1, 'Main frame of Application', u'Frame1.py']}

class BoaApp(wx.App):
    def OnInit(self):
        self.main = Frame1.create(None)
        self.main.Show()
        self.SetTopWindow(self.main)
        return True

def main():
    application = BoaApp(0)
    application.MainLoop()

if __name__ == '__main__':
    main()


And here is the frame itself:
Code: Select all
#Boa:Frame:Frame1

import wx
import wx.gizmos

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1LEDNUMBERCTRL1,
 wxID_FRAME1PANEL1, wxID_FRAME1TEXTCTRL1,
] = [wx.NewId() for _init_ctrls in range(5)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(359, 305), size=wx.Size(232, 183),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(216, 145))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(216, 145),
              style=wx.TAB_TRAVERSAL)

        self.lEDNumberCtrl1 = wx.gizmos.LEDNumberCtrl(id=wxID_FRAME1LEDNUMBERCTRL1,
              parent=self.panel1, pos=wx.Point(8, 8), size=wx.Size(200, 100),
              style=wx.gizmos.LED_ALIGN_LEFT)
        self.lEDNumberCtrl1.SetLabel('control')
        self.lEDNumberCtrl1.SetValue('0.0')

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self.panel1, pos=wx.Point(8, 112), size=wx.Size(100, 21),
              style=0, value='textCtrl1')

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='button1',
              name='button1', parent=self.panel1, pos=wx.Point(136, 112),
              size=wx.Size(75, 24), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton1Button(self, event):
        ave = self.textCtrl1.GetValue()
        ave = float(ave+ave) / 4
        self.lEDNumberCtrl1.SetValue(ave)
       


And here is the traceback:
TypeError : String or Unicode type required
47: Frame1.py: self.lEDNumberCtrl1.SetValue(ave)
334: gizmos.py: return +gizmos.LEDNumberCtrl_SetValue(*args, **kwargs)
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: LED Number Control set w/ variable

Postby stranac » Sun Feb 17, 2013 6:29 pm

Ok, that traceback actually makes sense, and it tells you exactly what your problem is.

wx.gizmos.LEDNumberCtrl.SetValue() takes a string, not a float(as I already mentioned).
Just convert your value to a string(using str()), and it should work just fine.

Edit:
Oh, actually, that's not enough for floats.
You should also convert ave to float before adding it to itself.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: LED Number Control set w/ variable

Postby ms4sman » Sun Feb 17, 2013 7:06 pm

Code: Select all
        quality_value = self.quality_slider.GetValue()
        habits_value = self.habits_slider.GetValue()
        knowledge_value = self.knowledge_slider.GetValue()
        behavior_value = self.behavior_slider.GetValue()
        overall_value = self.overall_slider.GetValue()
        ave = float(quality_value+habits_value+knowledge_value+behavior_value) / 4
        self.lEDNumberCtrl1.SetValueString(ave)

ok, I thought it was something like that, so here is the actually button definition from my code. Where would I convert the variable to a string?
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: LED Number Control set w/ variable

Postby stranac » Sun Feb 17, 2013 7:18 pm

Something like this:
Code: Select all
# convert values to float before adding
quality_value = float(self.quality_slider.GetValue())
habits_value = float(self.habits_slider.GetValue())
# ...
ave = (quality_value + habits_value + knowledge_value + behavior_value) / 4
# there is no .SetValueString(), or maybe I just don't see it??
self.lEDNumberCtrl1.SetValue(str(ave))


I would put all the slider values into a list and use sum() with a list comprehension to do the addition.
But I guess you could say it's just personal preference.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: LED Number Control set w/ variable

Postby ms4sman » Sun Feb 17, 2013 7:31 pm

Oh my gosh! It works now! Thank you so much! Sorry about the SetValueString, that was something I was trying, and it didn't work, but I forgot to change it before posting code. Thanks again for your help!
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests