gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:24:21 25 4
gpt4 key购买 nike

考虑以下代码:

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 个复选框绑定(bind)在一起,所以 cb2cb1 时被检查确实和cb3cb2 时被检查做。但是,当我设置 cb2 的值时在 OnCb1常规,cb2未触发复选框事件,cb3复选框保持未选中状态。所以我想找到一种以某种方式触发的方法cb2仅选中 cb1 时手动事件一次选中所有 3 个框.如果有人给我提示,我将不胜感激。

最佳答案

像这样使用 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)

关于python - WxPython - 在代码中设置其值时触发复选框事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9765718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com