Hi,
First step:
Learn how to do OO programming in Python first! If you do some exercises with simple examples first, then you won't get wx issues mixed up with Python ones. There are a lot of good tutorials on the web and in books. The chapter on OO in "Learning Python" got me started.
self.sizer=wx.BoxSizer(wx.VERTICAL)
TextCtrl3 = wx.TextCtrl(panel,-1,"1")
As soon as I see this construction, I get nervous. Chances are you'd be better off making a custom Panel, and putting your TextCtrl on that. Then put the panel on the Frame separately. Like this:
#!/usr/bin/env python2.4
import wx
class MyPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
wx.Panel.__init__(self, parent, *args, **kwargs)
sizer=wx.BoxSizer(wx.VERTICAL)
self.TextCtrl3 = wx.TextCtrl(self,-1,"1")
sizer.Add(self.TextCtrl3, 1, wx.EXPAND)
But = wx.Button(self, wx.ID_ANY, "Calc")
But.Bind(wx.EVT_BUTTON, self.OnCalc)
sizer.Add(But, 0, wx.ALL, 4)
self.SetSizerAndFit(sizer)
def OnCalc(self,e):
self.TextCtrl3.SetValue("Button Push")
class TopFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
mypanel = MyPanel(self, wx.ID_ANY)
self.Fit()
class MyApp(wx.App):
def OnInit(self):
self.topframe = TopFrame(None, wx.ID_ANY, "TEST")
self.SetTopWindow(self.topframe)
self.topframe.Show(True)
return True
app = MyApp()
app.MainLoop()
This may look like extra work now, but it keeps your design cleaner and more structured, and keeps your namespaces from being cluttered and confusing.
Uwe C. Schroeder wrote:
in your case. if you just need to access the one textctrl then assign the object to self aka
self.TextCtrl3=wx.TextCtrl(...)
exactly.
if you need more controls in the panel, assign the panel to self
(earlier mail) and give the controls decent Id's, i.e.
TextCtrl3 = wx.TextCtrl(panel,10000,"1")
and then find the TextXtrl by ID in the onCalc method
txt=self.panel.FindWindowById(10000)
txt.SetValue('whatever')
This is a style issue, I suppose, but I would never do this!.
1) Use default IDs. (wx.ID_ANY, which equals -1, or use keyword arguments, and no ID at all)
2) Give your controls reasonable names
3) If you have a set of controls that all act similarly, then you might not want to give them all names, but you should store them in a pythonic manner, in a list or dictionary:
self.controls = {}
self.controls["A good name"] = wx.TextCtrl(panel, wx.ID_ANY, "1")
def OnCalc(self, e):
txt=self.controls["A good Name"]
txt.SetValue('whatever')
This doesn't look much different than just giving them names, but this way you can pass them around and act on them as a group. You can also automatically generate them more easily this way:
for i in range(NumberOfButtons):
name = "Button#%i"%i
self.controls[name] = wx.Button(self, label=name)
Magic numbers are bad. Meaningful names are good!
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov