【发布时间】:2014-08-15 10:12:23
【问题描述】:
我在 wxPython 中有一个大型 GUI 应用程序。每当我按下按钮时,MessageDialog 就会显示一些结果。在对话框中单击 OK 和 X 时,对话框消失,但会再次触发原始按钮的事件。因此,该对话框会再次显示,因此它会无限继续。
我的代码(最小化到相关部分):
import wx
from wx import MessageDialog
class Compiler():
@staticmethod
def compile(code):
dialog = MessageDialog(None, code+'\n\n', caption='Compiler result', style=wx.ICON_ERROR|wx.CENTRE)
dialog.ShowModal()
class GUI ( wx.Frame ):
def __init__( self):
wx.Frame.__init__ ( self, None, id = wx.ID_ANY, title = "Test frame", pos = wx.DefaultPosition, size = wx.Size(200, 300), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.TAB_TRAVERSAL )
theSizer = wx.GridBagSizer( 0, 0 )
self.theButton = wx.Button( self, wx.ID_ANY, "Hit me!", wx.DefaultPosition, wx.DefaultSize, 0 )
theSizer.Add( self.theButton, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
self.SetSizer( theSizer )
self.Layout()
self.Centre( wx.BOTH )
self.theButton.Bind( wx.EVT_LEFT_DOWN, self.execute )
def execute( self, event ):
event.Skip()
print 'Button executed!'
Compiler.compile('any_input');
if __name__ == '__main__':
app = wx.App(False)
GUI().Show() # Launch GUI
app.MainLoop()
点击一次按钮后,点击框架中的任意位置会导致事件再次触发,为什么会出现这种情况?
【问题讨论】:
-
是故意点击任意位置触发事件还是仅仅点击按钮?
-
只有当我点击按钮时
标签: python event-handling wxpython messagedialog