【发布时间】:2021-12-23 17:10:14
【问题描述】:
在事件中将值分配给组合框时,它将再次触发相同的事件并且程序进入无限循环。但是相同的代码在 wxpython 版本 4.0.7 中工作。如果我做错了什么,请纠正我。
import wx
class Mywin(wx.Frame):
def __init__(self, parent: object, title: object) -> object:
super(Mywin, self).__init__(parent, title=title, size=(300, 200))
self.languages = ['C', 'C++', 'Python', 'Java', 'Perl']
panel = wx.Panel(self)
box = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(panel, label="Your choice:", style=wx.ALIGN_CENTRE)
box.Add(self.label, 0, wx.EXPAND | wx.ALL, 20)
cblbl = wx.StaticText(panel, label="Combo box", style=wx.ALIGN_CENTRE)
box.Add(cblbl, 0, wx.EXPAND | wx.ALL, 5)
self.combo = wx.ComboBox(panel, choices=self.languages)
box.Add(self.combo, 1, wx.EXPAND | wx.ALL, 5)
box.AddStretchSpacer()
self.combo.Bind(wx.EVT_TEXT, self.oncombo)
self.ignoreEvtText = False
panel.SetSizer(box)
self.Centre()
self.Show()
def oncombo(self, event):
if self.ignoreEvtText:
self.ignoreEvtText = False
return
textEntered = event.GetString()
self.label.SetLabel("You selected" + self.combo.GetValue() + " from Combobox" + textEntered)
if textEntered:
self.ignoreEvtText = True
matching = [s for s in self.languages if textEntered in s]
self.combo.Set(matching)
self.combo.SetInsertionPoint(len(textEntered))
self.combo.SetValue(textEntered)
else:
self.combo.Set(self.languages)
self.combo.Popup()
app = wx.App()
Mywin(None, 'ComboBox and Choice demo')
app.MainLoop()
【问题讨论】: