wxpython: another missing attribute

J

John Salerno

Ah, the object-oriented stuff is just so FUN! :) Here's my code,
followed by the error. I thought I was referring to the 'text' attribute
correctly, but it seems not.

import wx


class InputForm(wx.Frame):

def __init__(self, parent, id, title, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE,
name='frame'):
wx.Frame.__init__(self, parent, id, title, pos, size, style, name)
panel = wx.Panel(self)
text = wx.StaticText(panel, -1, 'Click results')
btnOK = wx.Button(panel, -1, 'OK')
self.Bind(wx.EVT_BUTTON, self.clickOK, btnOK)
btnCancel = wx.Button(panel, -1, 'Cancel')
self.Bind(wx.EVT_BUTTON, self.clickCancel, btnCancel)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(btnOK, 0, wx.ALL, 10)
sizer.Add(btnCancel, 0, wx.ALL, 10)
sizer.Add(text, 0, wx.ALL, 10)
panel.SetSizer(sizer)

def clickOK(self, event):
self.text.SetLabel('You clicked OK')

def clickCancel(self, event):
self.text.SetLabel('You clicked Cancel')


class MyApp(wx.App):

def OnInit(self):
frame = InputForm(None, -1, 'Data Entry Form')
self.SetTopWindow(frame)
frame.Show()
return True


app = MyApp()
app.MainLoop()

-------------------

Traceback (most recent call last):
File "C:\Python24\myscripts\wx_tests\wxtest.py", line 23, in clickOK
self.text.SetLabel('You clicked OK')
AttributeError: 'InputForm' object has no attribute 'text'
 
M

Morpheus

John Salerno said:
Ah, the object-oriented stuff is just so FUN! :) Here's my code,
followed by the error. I thought I was referring to the 'text' attribute
correctly, but it seems not.

import wx


class InputForm(wx.Frame):

def __init__(self, parent, id, title, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE,
name='frame'):
wx.Frame.__init__(self, parent, id, title, pos, size, style, name)
panel = wx.Panel(self)
text = wx.StaticText(panel, -1, 'Click results')

This becomes a local var, i.e. local to __init__. To make it an instance var
write

self.text = wx.StaticText(panel, -1, 'Click results')

HTH
Morpheus
 
J

John Salerno

Morpheus said:
This becomes a local var, i.e. local to __init__. To make it an instance var
write

self.text = wx.StaticText(panel, -1, 'Click results')

Ah, more namespace trickery! Thanks! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top