wxPython - 刷新/更新部分 GUI
我使用 GridBagSizer 和多个包含 StaticTextCtrl 的单元格。其中一些文本需要根据用户行为进行更改。因此,当发生更改时,我需要更新 GUI。 GridBagSizer 在一个函数中定义,该函数由另一个类中的函数调用,而该类又在 GUI 组装期间被调用。为了说明这一点,这里有一个代码片段:
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.ALIGN_CENTER|wx.ALL, 5)
return sizer
我现在需要在类 Canvas
中定义一个函数(或者必须将其放置在类 SampleClass
中?),该函数将被调用当需要更改 GUI 时:
def updateCanvas(self):
??? .Refresh() ???
# ----
有人可以给我一个提示,我应该使用哪些命令吗?
谢谢,樵夫
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.ALIGN_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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所需要做的就是调用 StaticText 的 SetLabel 方法来更新它。我通常不建议执行整个parent.widget.SetLabel() 事情,因为这很快就会变得非常难看。它确实有效,但我更喜欢自己使用 pubsub。您可以在这里阅读相关内容: http ://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/
编辑:例如,您可以拥有接收器像这样设置:
然后在函数中,你会做这样的事情:
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:
Then in the function, you'd do something like this:
您需要在
GridBagSizer
中保存对wxStaticText
的引用,然后使用新文本更新控件。这是一个带有
FlexGridSizer
的示例:稍后我会这样做:
You need to save a reference to the
wxStaticText
in yourGridBagSizer
and then update the control with the new text.Here is an example with a
FlexGridSizer
:later on I do this: