Getting Error when trying to get Data from Button Pressed

def fetchHandler(event):
print item3.GetValue()

def MyKeypadFunc( parent, call_fit = True, set_sizer = True ):
item0 = wx.BoxSizer( wx.VERTICAL )

item1 = wx.GridSizer( 0, 3, 0, 0 )

item2 = wx.StaticText( parent, ID_TEXT, "Password", wx.DefaultPosition, wx.DefaultSize, 0 )
item1.Add( item2, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

item3 = wx.TextCtrl( parent, ID_TEXTCTRL, "", wx.DefaultPosition, [80,-1], 0 )
item1.Add( item3, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

item1.Add( [ 20, 20 ] , 0, wx.ALIGN_CENTER|wx.ALL, 5 )

item4 = wx.Button( parent, ID_BUTTON1, "1", wx.DefaultPosition, wx.DefaultSize, 0 )
item1.Add( item4, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
item4.Bind(wx.EVT_BUTTON, fetchHandler)

Error I get:
Traceback (most recent call last):
File “keypad_wdr.py”, line 15, in fetchHandler
print item3.GetValue()
NameError: global name ‘item3’ is not defined

Because item3 is a local variable in the MyKeypadFunc function, and only exists in that scope.

···

On 2/16/12 8:20 AM, Sal Munguia wrote:

def fetchHandler(event):
        print item3.GetValue()

def MyKeypadFunc( parent, call_fit = True, set_sizer = True ):
     item0 = wx.BoxSizer( wx.VERTICAL )

     item1 = wx.GridSizer( 0, 3, 0, 0 )

     item2 = wx.StaticText( parent, ID_TEXT, "Password",
wx.DefaultPosition, wx.DefaultSize, 0 )
     item1.Add( item2, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

     item3 = wx.TextCtrl( parent, ID_TEXTCTRL, "", wx.DefaultPosition,
[80,-1], 0 )
     item1.Add( item3, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

     item1.Add( [ 20, 20 ] , 0, wx.ALIGN_CENTER|wx.ALL, 5 )

     item4 = wx.Button( parent, ID_BUTTON1, "1", wx.DefaultPosition,
wx.DefaultSize, 0 )
     item1.Add( item4, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
     item4.Bind(wx.EVT_BUTTON, fetchHandler)

Error I get:
Traceback (most recent call last):
   File "keypad_wdr.py", line 15, in fetchHandler
     print item3.GetValue()
NameError: global name 'item3' is not defined

--
Robin Dunn
Software Craftsman

I’ve tried to send self, in the argument, but still getting errors. I tried
def fetchHandler(self,event):
print self.item3.GetValue()

item4.Bind(wx.EVT_BUTTON, self.fetchHandler)

And still no luck.

···

On Thu, Feb 16, 2012 at 12:17 PM, Robin Dunn robin@alldunn.com wrote:

On 2/16/12 8:20 AM, Sal Munguia wrote:

def fetchHandler(event):

    print item3.GetValue()

def MyKeypadFunc( parent, call_fit = True, set_sizer = True ):

 item0 = wx.BoxSizer( wx.VERTICAL )



 item1 = wx.GridSizer( 0, 3, 0, 0 )



 item2 = wx.StaticText( parent, ID_TEXT, "Password",

wx.DefaultPosition, wx.DefaultSize, 0 )

 item1.Add( item2, 0, wx.ALIGN_CENTER|wx.ALL, 5 )



 item3 = wx.TextCtrl( parent, ID_TEXTCTRL, "", wx.DefaultPosition,

[80,-1], 0 )

 item1.Add( item3, 0, wx.ALIGN_CENTER|wx.ALL, 5 )



 item1.Add( [ 20, 20 ] , 0, wx.ALIGN_CENTER|wx.ALL, 5 )



 item4 = wx.Button( parent, ID_BUTTON1, "1", wx.DefaultPosition,

wx.DefaultSize, 0 )

 item1.Add( item4, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

 item4.Bind(wx.EVT_BUTTON, fetchHandler)

Error I get:

Traceback (most recent call last):

File “keypad_wdr.py”, line 15, in fetchHandler

 print item3.GetValue()

NameError: global name ‘item3’ is not defined

Because item3 is a local variable in the MyKeypadFunc function, and only exists in that scope.

Robin Dunn

Software Craftsman

http://wxPython.org

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

That is because item3 is only in scope within MyKeypadFunc if you
need to access it in your event handler you have a few choices:

  1. Make it a global - * MESSY I could get lynched for even
    mentioning it* but the possibility does exist.
  2.     Make it a member of the parent and go fully object oriented -
    

better.

  1.     Use event.GetEventObject() to get the object from the event
    

and your code then becomes, short an messy but should work:
print event.GetEventObject().GetValue()

Gadget/Steve
···

wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=en

When sending more than 1 value in key(self, ‘1’), the program doesn’t run.

···

On Thu, Feb 16, 2012 at 12:25 PM, Sal Munguia savetimeusdev@gmail.com wrote:

I’ve tried to send self, in the argument, but still getting errors. I tried
def key(self,event):

    print self.item3.GetValue()

item4.Bind(wx.EVT_BUTTON, self.key)

And still no luck.

On Thu, Feb 16, 2012 at 12:17 PM, Robin Dunn robin@alldunn.com wrote:

On 2/16/12 8:20 AM, Sal Munguia wrote:

def fetchHandler(event):

    print item3.GetValue()

def MyKeypadFunc( parent, call_fit = True, set_sizer = True ):

 item0 = wx.BoxSizer( wx.VERTICAL )



 item1 = wx.GridSizer( 0, 3, 0, 0 )



 item2 = wx.StaticText( parent, ID_TEXT, "Password",

wx.DefaultPosition, wx.DefaultSize, 0 )

 item1.Add( item2, 0, wx.ALIGN_CENTER|wx.ALL, 5 )



 item3 = wx.TextCtrl( parent, ID_TEXTCTRL, "", wx.DefaultPosition,

[80,-1], 0 )

 item1.Add( item3, 0, wx.ALIGN_CENTER|wx.ALL, 5 )



 item1.Add( [ 20, 20 ] , 0, wx.ALIGN_CENTER|wx.ALL, 5 )



 item4 = wx.Button( parent, ID_BUTTON1, "1", wx.DefaultPosition,

wx.DefaultSize, 0 )

 item1.Add( item4, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

 item4.Bind(wx.EVT_BUTTON, fetchHandler)

Error I get:

Traceback (most recent call last):

File “keypad_wdr.py”, line 15, in fetchHandler

 print item3.GetValue()

NameError: global name ‘item3’ is not defined

Because item3 is a local variable in the MyKeypadFunc function, and only exists in that scope.

Robin Dunn

Software Craftsman

http://wxPython.org

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Hi Sal,

I suggest to use better names for things, your current naming "scheme" will very quickly be very very difficult to follow.

I am not sure what the end use of this code is, but as you use sizers you don't need to have all this "Defaultetc" and I assume you will do a SetSizer somewhere else in your code, right?

I attach the code as that is most often easier to read/use.

Werner

salnaming.py (696 Bytes)

Thank you so much for the help. I’m not too worried about the naming convention now, since most of the code is automatically generated using WxDesigner. Hopefully, when I have time to build the code from scratch, if I ever decide to do that, I’ll start using my own naming convention, but now I’ve been using the naming convention dumped by the RAD tool.

How would I capture the event from the button, if lets say I wanted to add the button pressed to the wx.TextCtrl string. My goal is to get the keys punched and added back to the string. So the sample code I have now is:

def key(event)
value = self.item3.GetValue()
self.item3.SetValue(value + event)

item4.Bind(wx.EVT_BUTTON, key)

But it is currently not working.

···

On Thu, Feb 16, 2012 at 1:30 PM, werner wbruhin@free.fr wrote:

Hi Sal,

I suggest to use better names for things, your current naming “scheme” will very quickly be very very difficult to follow.

I am not sure what the end use of this code is, but as you use sizers you don’t need to have all this “Defaultetc” and I assume you will do a SetSizer somewhere else in your code, right?

I attach the code as that is most often easier to read/use.

Werner

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

How would I capture the event from the button, if lets say I wanted to add the button pressed to the wx.TextCtrl string. My goal is to get the keys punched and added back to the string.

Generally (using meaningful names in this case) something like:

self.myTextCtrl = wx.TextCtrl(self.backgroundpanel,-1, ‘’)

Button1 = wx.Button(self.backgroundpanel, -1, “Press me!”,name=‘Button1’)

Button1.Bind(wx.EVT_BUTTON, self.OnButton)

#You could create and bind all your buttons to this handler in a loop, by

the way.

def OnButton(self,event):
button_name = event.GetEventObject().GetName()

 self.myTextCtrl.SetValue(button_name + " was pressed.")

HTH,
Che

Thank you so much for the help. I'm not too worried about the naming convention now, since most of the code is automatically generated using WxDesigner.

That is a weak excuse;-) , seriously in the past I used Boa Constructructur to create my UI stuff and I always changed the default names it assigned to controls in the property editor, I never used wxDesigner but I am sure it offers the same thing.

Currently I use mostly the wx.lib.sized stuff which automatically does the sizer stuff.

How would I capture the event from the button, if lets say I wanted to add the button pressed to the wx.TextCtrl string. My goal is to get the keys punched and added back to the string. So the sample code I have now is:

def key(event)
       value = self.item3.GetValue()
       self.item3.SetValue(`value` + event)

item4.Bind(wx.EVT_BUTTON, key)

But it is currently not working.

It just didn't work? Looking at your event handler "key" I would think that you would have gotten an exception as it is missing the "self" in the parentheses . If you get an exception always include it in your post as they are very very helpful in pointing to the error.

BTW, doesn't wxDesigner allow you to generate the event handler?

Werner

···

On 16/02/2012 21:03, Sal Munguia wrote:

With wxDesigner, all the definitions in your MyKeypadFunc are local to the procedure (and you never usually want to even see them). But the IDs for each control - which YOU can name in the designer, the one above are the defaults - are global so the conventional way to access things in you main program is like:

self.mytextfield = self.FindWindowById(ID_TEXTCTRL)
self.mybutton = self.FindWindowById(ID_BUTTON)

self.Bind(wx.EVT_BUTTON, self.OnPress, self.mybutton)

or, if you don’t need to reference the button itself

self.Bind(wx.EVT_BUTTON, self.OnPress, id=ID_BUTTON)

def OnPress(self, event):
print self.mytextfield.GetValue()

···

On Thursday, 16 February 2012 16:20:01 UTC, Sal Munguia wrote:

def fetchHandler(event):
print item3.GetValue()

def MyKeypadFunc( parent, call_fit = True, set_sizer = True ):
item0 = wx.BoxSizer( wx.VERTICAL )

item1 = wx.GridSizer( 0, 3, 0, 0 )


item2 = wx.StaticText( parent, ID_TEXT, "Password", wx.DefaultPosition, wx.DefaultSize, 0 )
item1.Add( item2, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

item3 = wx.TextCtrl( parent, ID_TEXTCTRL, "", wx.DefaultPosition, [80,-1], 0 )

item1.Add( item3, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

item1.Add( [ 20, 20 ] , 0, wx.ALIGN_CENTER|wx.ALL, 5 )

item4 = wx.Button( parent, ID_BUTTON1, "1", wx.DefaultPosition, wx.DefaultSize, 0 )

item1.Add( item4, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
item4.Bind(wx.EVT_BUTTON, fetchHandler)

Error I get:
Traceback (most recent call last):
File “keypad_wdr.py”, line 15, in fetchHandler

print item3.GetValue()

NameError: global name ‘item3’ is not defined

Regards,

David Hughes
Forestfield Software