wxpython:为什么我要输入绑定函数两次?

2024-05-19 12:34:45 发布

您现在位置:Python中文网/ 问答频道 /正文

代码只用于编写一个简单的UI(窗口上只有一个文本框),并将事件wx.EVT_KEY_DOWN绑定到函数OnKeyDown,但是当我按下Esc键时,窗口将弹出EscTest,然后是另一个EscTest,最后它将在四个消息框之后退出,为什么?我只在wx.WXK_ESCAPE绑定中定义了两个消息框。在

# -*- coding: utf-8 -*-

import wx

class Command(wx.Frame):

    def __init__(self, parent, title):
        super(Command, self).__init__(parent, title=title, 
            size=(600, 500))

        self.InitUI()
        self.Centre()
        self.Show()     

    def InitUI(self):

        pnl = wx.Panel(self)
        self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)
        hbox = wx.BoxSizer(wx.HORIZONTAL)

        self.__tc_command = wx.TextCtrl(pnl, style=wx.TE_MULTILINE)

        self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)

        hbox.Add(self.__tc_command, proportion=1, flag=wx.ALL|wx.EXPAND, border=15)
        pnl.SetSizer(hbox)

    def OnKeyDown(self, evt):
        """Enter to send data, Esc to exit."""
        key = evt.GetKeyCode()
        if key == wx.WXK_ESCAPE:

            ##################Only two MessageBox, but pop up four##################
            wx.MessageBox("Esc")
            wx.MessageBox("Test")
            ##################Only two MessageBox, but pop up four##################

            self.Close()
        if key == wx.WXK_RETURN:
            wx.MessageBox("Enter")
        evt.Skip()

if __name__ == '__main__':

    app = wx.App(redirect=False)
    Command(None, title='Command')
    app.MainLoop()

Tags: keytestselftitledefcommandevtwx