Searene
Searene

Reputation: 27654

wxpython: Why do I enter the binding function twice?

The code is only used to write a simple UI( there is only a textbox on the window), and bind the event wx.EVT_KEY_DOWN to the function OnKeyDown, but when I pressed the Esc key, the window will pop up Esc, Test, then another Esc, Test, finally it will exit after the four message box, why? I only define two message box int the wx.WXK_ESCAPE bindings.

# -*- 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()

Upvotes: 0

Views: 310

Answers (1)

Elazar
Elazar

Reputation: 21685

You are calling self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown) twice in your code.

Upvotes: 1

Related Questions