wxpython事件的触发,WxPython - 在代码中设置其值时触发复选框事件

Consider the following piece of code:

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()

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.

解决方案

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值