当将值分配给ComboBox时,它将再次触发同一事件,并且程序将在无限的循环中进行。但是在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()
self.combo.Bind(wx.EVT_COMBOBOX, self.oncombo)
than ant:
self.combo.Bind(wx.EVT_TEXT, self.oncombo)
evt_text会使您陷入持续的事件循环。时段,文本会改变,如果解雇事件,会改变文本,这会发射事件,..........................
我在其他任何地方都找不到有关无限循环的明确答案,所以我对自己进行了一些调查。
似乎是呼叫
EVT_TEXT
Set
的结果。这意味着您必须这样做:
SetValue
self.ignoreEvtText = True
self.Set(matching)
self.ignoreEvtText = True
self.SetValue(textEntered)
self.combo.SetInsertionPoint(len(textEntered))
必须在呼叫后移动SetInsertionPoint
,否则文本将不存在正确设置的插入点。
最终注释SetValue
call the the the the the Taud Taud Taud Taid Relect非常奇怪,至少在Windows 11上的最新WXPYTHON上对我来说。特别是,在下拉动画再次播放时,每个字符后一个令人讨厌的延迟,并且文本被突出显示,并且然后立即取消照明。此外,在某些情况下,失去焦点会导致下拉列表进入现在焦点的应用程序顶部绘制的消失反应循环。结果,我最终不使用它。用户可以自己打开下拉菜单并避免怪异。