【问题标题】:Continuous event firing wxPython连续事件触发 wxPython
【发布时间】: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


【解决方案1】:

代码中的真正错误:

def execute( self, event ):
    event.Skip()
    print 'Button executed!'
    Compiler.compile('any_input');

event.Skip()。它所做的是不断传播事件。因此,该事件在没有任何其他事件处理程序的情况下继续传播,并由该事件处理程序在循环中连续处理和传播。去掉这条线就可以了!

查看documentation 了解更多信息

【讨论】:

  • 完美运行!不敢相信我没想到! :)
猜你喜欢
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 2014-05-23
  • 1970-01-01
相关资源
最近更新 更多