I use a GridBagSizer with several cells containing a StaticTextCtrl. Some of these texts need to be changed depending on user behaviour. So I need to update the GUI when a change is made. The GridBagSizer is definded in a function that is called by a function in another class, which in turn is called during GUI assembly. To illustrate this, here is a code snippet:
class SampleClass(wx.MiniFrame):
[...some other code...]
def makeGUI(self):
# make panels
panelFoo = self.makePanelFoo()
panelBar = self.makePanelBar()
panelFinal = self.makePanelFinal()
# pack elements
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(panelFoo, 1, wx.EXPAND, 0)
self.mainSizer.Add(panelBar, 1, wx.EXPAND, 0)
self.mainSizer.Add(panelFinal, 1, wx.EXPAND, 0)
# fit layout
self.mainSizer.Fit(self)
self.SetSizer(self.mainSizer)
# ----
[...some other code...]
def makePanelFinal(self):
panel = Canvas(self, -1)
# define canvas parts
self.partA = panel.makePartA()
self.partB = panel.makePartB()
self.partC = panel.makePartC()
# arrange canvas parts
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.partA, 1, wx.EXPAND|wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT, mwx.PANEL_SPACE_MAIN)
mainSizer.Add(self.partB, 0, wx.EXPAND|wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT, mwx.PANEL_SPACE_MAIN)
mainSizer.Add(self.partC, 0, wx.EXPAND|wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT|wx.BOTTOM, mwx.PANEL_SPACE_MAIN)
# fit layout
mainSizer.Fit(panel)
panel.SetSizer(mainSizer)
return panel
# ----
class Canvas(panel):
[...some other code...]
def makePartC(self):
sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, ""), wx.VERTICAL)
grid = wx.GridBagSizer(mwx.GRIDBAG_VSPACE, mwx.GRIDBAG_HSPACE)
[...some code where the GridBagSizer is made...]
sizer.Add(grid, 1, wx.EXPAND|wx.AL开发者_运维百科IGN_CENTER|wx.ALL, 5)
return sizer
I now need to define a function in the class Canvas
(or must this be placed in class SampleClass
?) that will be called when a change in the GUI is neccessary:
def updateCanvas(self):
??? .Refresh() ???
# ----
Could someone please give me a hint which commands I should use?
Thanks, Woodpicker
All you need to do is call the StaticText's SetLabel method to update it. I don't usually recommend doing the whole parent.widget.SetLabel() thing though as that can get pretty ugly pretty quickly. It does work, but I prefer using pubsub myself. You can read about that here: http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/
EDIT: For example, you could have the receiver set up like this:
Publisher().subscribe(self.updateDisplay, ("update.display"))
Then in the function, you'd do something like this:
def updateDisplay(self, msg):
self.myStaticTxtCtrl.SetLabel(msg.data)
You need to save a reference to the wxStaticText
in your GridBagSizer
and then update the control with the new text.
Here is an example with a FlexGridSizer
:
self.fNameC = wx.StaticText(self, -1, fn)
fgs = wx.FlexGridSizer(5, 2, 5, 5)
fgs.Add(self.fNameC, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
later on I do this:
self.panel.fNameC.SetLabel(fName)
精彩评论