我在 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()
单击按钮一次后,单击框架中的任何位置都会导致事件再次触发,为什么会发生这种情况?