wxPython:禁用多个按钮

时间:2013-12-16 21:37:42

标签: python wxpython

我有一个wxPython GUI,如下所示:

enter image description here

正如您所看到的,有三个“列”,每个列都包含在wx.StaticBox中。我想禁用列中的所有按钮,文本框和单选按钮。我尝试在静态框中使用.Disable,但它没有效果。是否有一种简单的方法可以禁用静态框内的所有内容?

3 个答案:

答案 0 :(得分:0)

wx.StaticBox实际上不是一个容器,它只是:

  

a rectangle drawn around other panel items to denote a logical grouping of items.

因此,使用StaticBox执行此操作的唯一方法是跟踪逻辑上组合在一起的小部件,并在所有小部件上调用Disable

或者,或者,您可以将小部件放入任何实际的容器小部件(例如窗口和大小调整器)中,然后只放置Disable容器。

答案 1 :(得分:0)

如果尚未将StaticBoxSizer设置为类属性(即self.mySizer而不仅仅是mySizer)。然后,您可以使用其GetChildren()方法返回小部件。接下来,您只需遍历小部件并禁用它们。这样的事情应该这样做:

children = self.mySizer.GetChildren()
for child in children:
   child.Disable()

您可能需要在循环中添加一个检查以确保它是一个按钮或文本控件。我建议使用Python的 isinstance

答案 2 :(得分:0)

这是一个可行的示例:

"""
Demonstration on how to disable / enable all objects within a sizer
Reinhard Daemon / Austria
08.10.2019
"""
import wx

class MainWindow(wx.Frame):
    def __init__(self, parent=None):
        wx.Frame.__init__(self, parent, -1,
            title='Disable / Enable all Widgets within a Sizer',size=(500,200))
        self.Move((50,50))
        panel = wx.Panel(self)

        # layout (sizers, boxes,...):
        top_sizer = wx.BoxSizer(wx.VERTICAL)
        widget_box = wx.StaticBox(panel, id=-1,
            label='Widgets, which are controlled')
        widget_box.SetBackgroundColour("yellow")
        control_box = wx.StaticBox(panel, -1,
            label='Widgets, which controll')
        control_box.SetBackgroundColour("yellow")
        self.widget_sizer = wx.StaticBoxSizer(widget_box, wx.HORIZONTAL)
        control_sizer = wx.StaticBoxSizer(control_box, wx.HORIZONTAL)

        # create the widgets:
        widget_1 = wx.TextCtrl(panel, value='Text 1')
        widget_2 = wx.RadioButton(panel, label='Radio 1')
        widget_3 = wx.RadioButton(panel, label='Radio 2')
        widget_4 = wx.Button(panel, label='Button 1')
        widget_disable = wx.Button(panel, label='DISABLE')
        self.widget_enable = wx.Button(panel, label='ENABLE', pos = (100,50))

        # add the widgets to the layout:
        self.widget_sizer.Add(widget_1)
        self.widget_sizer.Add(widget_2)
        self.widget_sizer.Add(widget_3)
        self.widget_sizer.Add(widget_4)
        control_sizer.Add(widget_disable)
        control_sizer.Add(self.widget_enable)

        # finalize the layout:
        top_sizer.Add(sizer=self.widget_sizer, flag=wx.CENTER | wx.EXPAND)
        top_sizer.AddSpacer(30)
        top_sizer.Add(control_sizer, 0, wx.CENTER | wx.EXPAND)
        panel.SetSizer(top_sizer)
        panel.Fit()

        # bindings:
        widget_disable.Bind(wx.EVT_BUTTON, self.on_button_disable)
        self.widget_enable.Bind(wx.EVT_BUTTON, self.on_button_enable)

    def on_button_disable(self, evt):
        children = self.widget_sizer.GetChildren()
        for child in children:
            print(child.GetWindow(),end='')
            try:
                child.GetWindow().Enable(False)
                print(' DISABLED')
            except:
                print(' ERROR')

    def on_button_enable(self, evt):
        children = self.widget_sizer.GetChildren()
        for child in children:
            print(child.GetWindow(),end='')
            try:
                child.GetWindow().Enable(True)
                print(' ENABLED')
            except:
                print(' ERROR')


if __name__ == "__main__":
    app = wx.App()
    view1 = MainWindow()
    view1.Show()
    app.MainLoop()