Simple wxPython code with error

Not sure if this is the proper way to post code and question, I am
registered.
I use the book "wxPython IN ACTION" and learn by adapting some of the
example code.
I attempted to try to use a GridSizer and most of it works with the
exception noted at the bottom
of the included code. I removed a lot of existing code to get this down to
just the error. Hope I am posting correctly and get some answer. Thanks.

#!/usr/bin/env python
''' experimental code : XXXGasChkGrid.py '''
import wx

class GridSizerFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "GasChkGrid.py",pos=(200,100))
        
        #calculated MPG
        mpgLbl=wx.StaticText(self,-1,"MPG:")
        mpg=wx.TextCtrl(self,-1,"",size=(80,20))
        
        #Buttons
        button1=wx.Button(self,-1,"Calculate")
        button2=wx.Button(self,-1,"button2")
        #button Event(s)
        self.Bind(wx.EVT_BUTTON, self.OnClick,button2)
        #create sizer
        sizer = wx.GridSizer(rows=12, cols=2,hgap=5,vgap=5)
         # add widgets to sizer
        sizer.Add(mpgLbl,0,wx.ALIGN_RIGHT)
        sizer.Add(mpg,0,0)
        #add button(s)
        sizer.Add(button1,0,0)
        sizer.Add(button2,0,0)
        self.SetSizer(sizer)
        self.Fit()
        self.Show() #redundit? see line 67
        mpg.SetValue("123") #this is just testing, verify can change
text....
        
    def OnClick(self,event):
        print 'OK' # <===this works as expected.
        mpg.SetValue("Clicked") #<=== gives error

app = wx.PySimpleApp()
GridSizerFrame().Show()
app.MainLoop()

''' Traceback (most recent call last):
  File "XXXGasChkGrid.py", line 33, in OnClick
    mpg.SetValue("Clicked") #<=== gives error
    NameError: global name 'mpg' is not defined
'''

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/Simple-wxPython-code-with-error-tp5720796.html
Sent from the wxPython-users mailing list archive at Nabble.com.

Hi,

Not sure if this is the proper way to post code and question, I am
registered.

I prefere if one attaches the code but that is a minor issue.

I use the book "wxPython IN ACTION" and learn by adapting some of the
example code.
I attempted to try to use a GridSizer and most of it works with the
exception noted at the bottom
of the included code. I removed a lot of existing code to get this down to
just the error. Hope I am posting correctly and get some answer. Thanks.

#!/usr/bin/env python
''' experimental code : XXXGasChkGrid.py '''
import wx

class GridSizerFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, None, -1, "GasChkGrid.py",pos=(200,100))
                  #calculated MPG
         mpgLbl=wx.StaticText(self,-1,"MPG:")

Change 'mpg' in the following line to self.mpg.

         mpg=wx.TextCtrl(self,-1,"",size=(80,20))
                  #Buttons
         button1=wx.Button(self,-1,"Calculate")
         button2=wx.Button(self,-1,"button2")
         #button Event(s)
         self.Bind(wx.EVT_BUTTON, self.OnClick,button2)
         #create sizer
         sizer = wx.GridSizer(rows=12, cols=2,hgap=5,vgap=5)
          # add widgets to sizer
         sizer.Add(mpgLbl,0,wx.ALIGN_RIGHT)
         sizer.Add(mpg,0,0)
         #add button(s)
         sizer.Add(button1,0,0)
         sizer.Add(button2,0,0)
         self.SetSizer(sizer)
         self.Fit()
         self.Show() #redundit? see line 67

same in the following line

         mpg.SetValue("123") #this is just testing, verify can change
text....
              def OnClick(self,event):
         print 'OK' # <===this works as expected.

here you reference 'mpg' as a local or global variable in your OnCLick method. By adding 'self' to the above lines we made 'mgp' a variable of your class and if you change the line below in the same way it will work.

         mpg.SetValue("Clicked") #<=== gives error
  

If you use a recent version of wx (definitely in 2.9+ might have changed before then) then you should just use wx.App or you might also want look into using the WIT to help you debug sizer layouts and other things in your application.

wiki.wxpython.org/Widget Inspection Tool

app = wx.PySimpleApp()
GridSizerFrame().Show()
app.MainLoop()

''' Traceback (most recent call last):
   File "XXXGasChkGrid.py", line 33, in OnClick
     mpg.SetValue("Clicked") #<=== gives error
     NameError: global name 'mpg' is not defined
'''

Hope this helps
Werner

···

On 4/19/2014 0:06, eightbits wrote:

Thanks for the suggestions. I did find some answers at a another forum that
got me through that part.
Still learning to use wxPython. I will take a look at using the Widget
Inspection Tool.
Not sure what version of wxpython I have installed. I have other issues that
I am at a loss to understand and will post in a new post.

So, thanks again werner-3.

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/Simple-wxPython-code-with-error-tp5720860p5720867.html
Sent from the wxPython-users mailing list archive at Nabble.com.