简体   繁体   中英

Binding to frame wxPython

For some reason It is not reading any of the keys pressed I have spent the entire day looking for examples and fixes for this and to no avail I cannot figure out why. Tried using panel to as shown below, but the tutorial I am using to learn about about wxpython is problematic, even the copy paste of the there code doesnt work. there example doesnt use panel but just a self.Bind(wx.EVT_KEY_UP,self.keyPressed). Can anyone lend assistance to this beginner?

 import wx
 import time
 from threading import Thread

 class frame(wx.Frame):
     def  __init__ (self):
             style = (wx.CLIP_CHILDREN | wx.STAY_ON_TOP | wx.FRAME_NO_TASKBAR |    wx.NO_BORDER |wx.FRAME_SHAPED)  
             wx.Frame.__init__(self,None, title='test', style = style, size = (300,150))
             self.panel = wx.Panel(self,-1)
             self.Bind(wx.EVT_KEY_DOWN, self.keyPressed, self.panel)
             self.SetFocus()
             self.SetTransparent(0)
             self.Show(True)

    def keyPressed(self,event=None):
            print "somethine is pressed"
            key = event.GetKeyCode()
            if key == wx.WXK_ESCAPE or key == ord('Q'):
                    self.Close(force=True)
                    self.Show(False)
            else:
                    event.Skip()

    def duration(self, dur):
            start = int(time.time())
            self.closeTime = False
            while not self.closeTime:
                    actDur = int(time.time()) -start
                    if actDur == dur:
                            self.Show(False)
                            self.closeTime = True
   app = wx.App(False)
   f = frame()
   #timing = Thread(target=f.duration, args= (5,))
   #timing.start()
   app.MainLoop()

像这样绑定到面板

self.panel.Bind(wx.EVT_KEY_DOWN, self.keyPressed)

The panel can be hard to bind to as it likes to pass its focus on to its children. The frame does much the same thing. If all you want to do is bind key presses, you might want to look at an AcceleratorTable:

And these might help you too:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM