WxPython-在cod中设置其值时触发复选框事件

2024-09-30 00:31:50 发布

您现在位置:Python中文网/ 问答频道 /正文

请考虑以下代码:

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个复选框绑定在一起,所以cb2cb1执行时被选中,cb3cb2执行时被选中。但是,当我在OnCb1例程中设置cb2的值时,不会触发cb2复选框事件,并且cb3复选框保持未选中状态。因此,我想找到一种方法以某种方式手动触发cb2事件,以便在只选中cb1时同时选中所有3个框。如果有人给我提示,我会非常感激的。


Tags: selfaddappdefframeevtwxcheckbox
3条回答

我不能直接采用nmz787的代码,必须稍微修改一下,但我终于让它为

  • 更改复选框的状态和
  • 让复选框接收事件

就像用户点击了它一样。

我想我会把我的代码贴出来,以防其他人也被困在这件事上。由于它只依赖于复选框,而不依赖于调用它的控件,所以我将它分解为一个独立的“顶级”函数,而不是任何类上的方法。

def toggleCheckBox(cb):
    evt = wx.CommandEvent(commandType=wx.EVT_CHECKBOX.typeId)
    evt.SetEventObject(cb)
    cb.SetValue( not cb.GetValue())
    wx.PostEvent(cb, evt)

我不知道为什么CommandEvent构造函数需要关键字,或者是否有更合理、更健壮的方法来获取所需的typeId,但这对我有效。

我没有使用wxPython的经验,所以我不能给出一个具体的例子,但是我知道以编程方式设置值不会触发小部件的命令事件。我的假设是,在设置cb2的值之后,必须手动发布它。你可以在这里复习一个类似的问题:wxPython: Calling an event manually

我建议将wx.CheckBox子类化,并创建一个SetValueWithEvent()或类似的方法,该方法将同时调用SetValue并发布一个wx. EVT_CHECKBOX事件。

PyQt也有类似的情况,当以编程方式设置小部件上的值时,可能会发出信号,也可能不会发出信号。他们有时会给你一个以上的信号,你可以听,以适应任何一种方式。不幸的是,仅仅基于我对wxPython示例的有限接触,我认为它更原始,而不是Python。所以你似乎得自己多做点事情。

使用wx.PostEvent。。。就像这样:

class launcherWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='New Window')
        #now add the main body, start with a panel
        panel = wx.Panel(self)
        #instantiate a new dropdown
        self.productDropDown = wx.ComboBox(panel, size=wx.DefaultSize, style = wx.CB_READONLY)

        #get the products and product subtypes
        self.productDict = self.getProductsAndSubtypes()

        #setup subtypes first, just in case, since onProductSelection will reference this
        self.productSubtypeDropDown = wx.ComboBox(panel, size=wx.DefaultSize, style = wx.CB_READONLY)

        #add products
        for product in self.productDict.keys():
            self.productDropDown.Append(product)

        #bind selection event
        self.productDropDown.Bind(wx.EVT_COMBOBOX, self.onProductSelection)

        #set default selection
        self.productDropDown.SetSelection(0)

        #pretend that we clicked the product selection, so it's event gets called
        wx.PostEvent(self.productDropDown, wx.CommandEvent(wx.wxEVT_COMMAND_COMBOBOX_SELECTED))

        #now add the dropdown to a sizer, set the sizer for the panel, fit the panel, etc...

    def onProductSelection(self, event):
        productSelected = self.productDropDown.GetStringSelection()
        productSubtypes = self.productDict[productSelected]

        #clear any existing product subtypes, since each product may have different ones
        self.productSubtypeDropDown.Clear()

        for productSubtype in productSubtypes:
            self.productSubtypeDropDown.Append(productSubtype)

        #select the first item by default
        self.productSubtypeDropDown.SetSelection(0)

相关问题 更多 >

    热门问题