wxPython - 刷新/更新部分 GUI

发布于 2024-11-23 18:27:37 字数 2153 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别低头,皇冠会掉 2024-11-30 18:27:37

您所需要做的就是调用 StaticText 的 SetLabel 方法来更新它。我通常不建议执行整个parent.widget.SetLabel() 事情,因为这很快就会变得非常难看。它确实有效,但我更喜欢自己使用 pubsub。您可以在这里阅读相关内容: http ://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

编辑:例如,您可以拥有接收器像这样设置:

Publisher().subscribe(self.updateDisplay, ("update.display"))

然后在函数中,你会做这样的事情:

def updateDisplay(self, msg):
    self.myStaticTxtCtrl.SetLabel(msg.data)

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)
恏ㄋ傷疤忘ㄋ疼 2024-11-30 18:27:37

您需要在 GridBagSizer 中保存对 wxStaticText 的引用,然后使用新文本更新控件。

这是一个带有 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)

稍后我会这样做:

self.panel.fNameC.SetLabel(fName)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文