wxPython:禁用多个按钮
我有一个wxPython GUI,如下所示:wxPython:禁用多个按钮,python,wxpython,Python,Wxpython,我有一个wxPython GUI,如下所示: 如您所见,有三个“列”,每个列都包含在wx.StaticBox中。我想禁用列中的所有按钮、文本框和单选按钮。我尝试在静态框上使用.Disable,但没有效果。有没有一种简单的方法可以禁用静态框中的所有内容?awx。静态框实际上不是一个容器,它只是: 因此,使用StaticBox可以做到这一点的唯一方法是跟踪逻辑组合在一起的小部件,并对所有小部件调用Disable 或者,您也可以将这些小部件放入任何实际的容器小部件(如windows和Sizer)中
如您所见,有三个“列”,每个列都包含在
wx.StaticBox
中。我想禁用列中的所有按钮、文本框和单选按钮。我尝试在静态框上使用.Disable
,但没有效果。有没有一种简单的方法可以禁用静态框中的所有内容?awx。静态框实际上不是一个容器,它只是:
因此,使用StaticBox
可以做到这一点的唯一方法是跟踪逻辑组合在一起的小部件,并对所有小部件调用Disable
或者,您也可以将这些小部件放入任何实际的容器小部件(如windows和Sizer)中,然后只禁用容器。如果您还没有将StaticBoxSizer设置为类属性(即self.myzer而不是仅仅myzer)。然后可以使用其GetChildren()方法返回小部件。接下来,您只需循环遍历小部件并禁用它们。像这样的东西应该可以做到:
children = self.mySizer.GetChildren()
for child in children:
child.Disable()
您可能需要在循环中添加一个检查,以确保它是一个按钮或文本控件。我建议使用Python的isinstance来实现这一点。这里有一个工作示例:
"""
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()