Python EVT_集_焦点
当我运行这段代码并专注于选择时,它会引发一个错误。我关闭 这条消息,但它是回来了。我只想看一次 这条消息。我该怎么做?我的代码中有什么错误Python EVT_集_焦点,python,wxpython,Python,Wxpython,当我运行这段代码并专注于选择时,它会引发一个错误。我关闭 这条消息,但它是回来了。我只想看一次 这条消息。我该怎么做?我的代码中有什么错误 #! -*- coding:utf-8 -*- import wx class MyPanel(wx.Panel): def __init__(self, parent, *args, **kwargs): wx.Panel.__init__(self, parent, *args, **kwargs) sizer = w
#! -*- coding:utf-8 -*-
import wx
class MyPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
wx.Panel.__init__(self, parent, *args, **kwargs)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.my_choice = wx.Choice(self, wx.NewId())
self.my_button = wx.Button(self, wx.NewId(), label = "Procces")
self.my_button.SetFocus()
sizer.AddMany([(self.my_choice, 0, wx.ALL, 5),
(self.my_button, 0, wx.ALL, 5)])
self.SetSizer(sizer)
self.my_choice.Bind(wx.EVT_SET_FOCUS, self.my_choice_on_focus)
self.my_button.Bind(wx.EVT_BUTTON, self.my_button_on_clicked)
def my_choice_on_focus(self, evt):
try:
self.my_choice.Clear()
print "Input some items in my_choice"
raise RuntimeError
except RuntimeError:
dlg = wx.MessageDialog(self, "test EVT_SET_FOCUS", "Error", wx.ICON_ERROR|wx.OK )
dlg.ShowModal()
dlg.Destroy()
raise
evt.Skip()
def my_button_on_clicked(self, evt):
print "Procces my choice value"
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
frame = wx.Frame(None, title = "Test")
panel = MyPanel(frame)
frame.Show()
self.SetTopWindow(frame)
return True
if __name__ == '__main__':
app = MyApp(redirect = False)
app.MainLoop()
错误对话框在显示时获得焦点。关闭错误对话框时,焦点将返回到选择控件,再次触发事件处理程序,从而再次弹出错误对话框,等等
为了避免多次调用事件处理程序,一种解决方案是解除wx.EVT_SET_焦点的绑定。另一种方法是根本不绑定wx.EVT_设置_FOCUS,并在其他地方和/或在另一个时间点执行您希望在“我的选择”中执行的操作。我用以下代码解决了我的问题。 谢谢大家的帮助
#! -*- coding:utf-8 -*-
import wx
import wx.lib.evtmgr as em
class MyPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
wx.Panel.__init__(self, parent, *args, **kwargs)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.my_choice = wx.Choice(self, wx.NewId())
self.my_choice.Append("Make your selection")
self.my_choice.Select(0)
self.my_button = wx.Button(self, wx.NewId(), label = "Procces")
self.my_button.SetFocus()
sizer.AddMany([(self.my_choice, 0, wx.ALL, 5),
(self.my_button, 0, wx.ALL, 5)])
self.SetSizer(sizer)
em.eventManager.Register(self.my_choice_set_focus, wx.EVT_SET_FOCUS, self.my_choice)
em.eventManager.Register(self.my_choice_on_select, wx.EVT_CHOICE, self.my_choice)
em.eventManager.Register(self.my_button_on_clicked, wx.EVT_BUTTON, self.my_button)
def my_choice_on_select(self, evt):
if evt.GetSelection() <= 0:
print "Procces false selection"
em.eventManager.Register(self.my_choice_set_focus,
wx.EVT_SET_FOCUS,
self.my_choice)
self.SetFocusIgnoringChildren()
else:
print "Procces true selection"
evt.Skip()
def my_choice_set_focus(self, evt):
try:
self.my_choice.Clear()
self.my_choice.Append("Make your selection")
raise RuntimeError
except RuntimeError:
em.eventManager.DeregisterListener(self.my_choice_set_focus)
dlg = wx.MessageDialog(None, "test EVT_SET_FOCUS", "Error",
wx.ICON_ERROR|wx.OK )
dlg.ShowModal()
dlg.Destroy()
self.my_choice.Clear()
self.my_choice.Append("Try again...")
self.my_choice.Select(0)
evt.Skip()
evt.Skip()
def my_button_on_clicked(self, evt):
print "Procces my choice value"
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
frame = wx.Frame(None, title = "Test")
panel = MyPanel(frame)
frame.Show()
self.SetTopWindow(frame)
return True
if __name__ == '__main__':
app = MyApp(redirect = False)
app.MainLoop()
#!-*-编码:utf-8-*-
导入wx
将wx.lib.evtmgr作为em导入
类别MyPanel(wx.Panel):
定义初始化(自、父、*args、**kwargs):
wx.Panel.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
sizer=wx.BoxSizer(wx.HORIZONTAL)
self.my_choice=wx.choice(self,wx.NewId())
self.my_choice.Append(“做出选择”)
self.my_choice.Select(0)
self.my_button=wx.button(self,wx.NewId(),label=“Procces”)
self.my_button.SetFocus()
sizer.AddMany([(self.my_choice,0,wx.ALL,5),
(self.my_按钮,0,wx.ALL,5)])
自整定器(施胶器)
em.eventManager.Register(self.my\u choice\u set\u focus,wx.EVT\u set\u focus,self.my\u choice)
em.eventManager.Register(self.my\u choice\u on\u select,wx.EVT\u choice,self.my\u choice)
em.eventManager.Register(点击self.my\u按钮,wx.EVT\u按钮,self.my\u按钮)
在选择上定义我的选择(自我,evt):
如果evt.GetSelection()