请考虑以下代码:
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.cb1 = wx.CheckBox(self, -1, "CheckBox 1")
self.cb2 = wx.CheckBox(self, -1, "CheckBox 2")
self.cb3 = wx.CheckBox(self, -1, "CheckBox 3")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.cb1, 0, wx.ADJUST_MINSIZE, 0)
sizer.Add(self.cb2, 0, wx.ADJUST_MINSIZE, 0)
sizer.Add(self.cb3, 0, wx.ADJUST_MINSIZE, 0)
self.SetSizer(sizer)
self.Layout()
self.Bind(wx.EVT_CHECKBOX, self.OnCb1, self.cb1)
self.Bind(wx.EVT_CHECKBOX, self.OnCb2, self.cb2)
def OnCb1(self, evt):
self.cb2.SetValue(evt.IsChecked())
def OnCb2(self, evt):
self.cb3.SetValue(evt.IsChecked())
if __name__ == "__main__":
app = wx.PySimpleApp(0)
frame = MyFrame(None, -1, "")
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
这里我有3个复选框绑定在一起,所以cb2
在cb1
执行时被选中,cb3
在cb2
执行时被选中。但是,当我在OnCb1
例程中设置cb2
的值时,不会触发cb2
复选框事件,并且cb3
复选框保持未选中状态。因此,我想找到一种方法以某种方式手动触发cb2
事件,以便在只选中cb1
时同时选中所有3个框。如果有人给我提示,我会非常感激的。
我不能直接采用nmz787的代码,必须稍微修改一下,但我终于让它为
就像用户点击了它一样。
我想我会把我的代码贴出来,以防其他人也被困在这件事上。由于它只依赖于复选框,而不依赖于调用它的控件,所以我将它分解为一个独立的“顶级”函数,而不是任何类上的方法。
我不知道为什么CommandEvent构造函数需要关键字,或者是否有更合理、更健壮的方法来获取所需的typeId,但这对我有效。
我没有使用wxPython的经验,所以我不能给出一个具体的例子,但是我知道以编程方式设置值不会触发小部件的命令事件。我的假设是,在设置cb2的值之后,必须手动发布它。你可以在这里复习一个类似的问题:wxPython: Calling an event manually
我建议将wx.CheckBox子类化,并创建一个
SetValueWithEvent()
或类似的方法,该方法将同时调用SetValue并发布一个wx. EVT_CHECKBOX
事件。PyQt也有类似的情况,当以编程方式设置小部件上的值时,可能会发出信号,也可能不会发出信号。他们有时会给你一个以上的信号,你可以听,以适应任何一种方式。不幸的是,仅仅基于我对wxPython示例的有限接触,我认为它更原始,而不是Python。所以你似乎得自己多做点事情。
使用wx.PostEvent。。。就像这样:
相关问题 更多 >
编程相关推荐