WxPython - 在代码中设置其值时触发复选框事件 [英] WxPython - trigger checkbox event while setting its value in the code

查看:181
本文介绍了WxPython - 在代码中设置其值时触发复选框事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

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 事件手动检查所有3个框一次只检查 cb1 。如果有人给我一个提示,我将非常感激。

Here I have 3 checkboxes bound together, so cb2 gets checked when cb1 does and cb3 gets checked when cb2 does. However, when I set the value of cb2 in OnCb1 routine, the cb2 checkbox event is not triggered, and cb3 checkbox remains unchecked. So I'd like to find a way to trigger somehow cb2 event manually to check all 3 boxes at once when checking only cb1. I'd be very grateful if anyone gives me a hint.

推荐答案

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

Use wx.PostEvent... like so:

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)

这篇关于WxPython - 在代码中设置其值时触发复选框事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆