Franz wrote:
Hello,
I'm new to wxPython and find myself now confrontated with object-orientated programming. Nevertheless, i try to get some simple things working
My goal is simple to evaluate pressed keys. I have written a small program, which uses wx.KeyEvent. But always when i press a key, nothing really changes.
You can create a KeyEvent, but in that case, you need to setup the keycodes as well. Your program will process an infinite loop and it will never reach app.MainLoop().
Alternatively, wxWidgets can automatically create a new wx.KeyEvent when you press the button. To do this, create a frame. Bind the frame to the event, and call MainLoop(). wxWidgets will create the new event for you at the time when you press the key, and (since you bound it to an event handler) your event handler will be called. Try this example:
import wx
app = wx.PySimpleApp()
class MyFrame(wx.Frame):
def __init__(self,*args,**kwargs):
wx.Frame.__init__(self,*args,**kwargs)
self.Bind(
wx.EVT_KEY_DOWN,
self.OnKey,
self
)
def OnKey(self,event):
KeyCode = event.GetRawKeyCode()
Event = event.GetEventType()
EventObject = event.GetEventObject()
UnicodeKey = event.GetUnicodeKey()
KeyFlags = event.GetRawKeyFlags()
UniChar = event.GetUniChar()
nKeyCode = event.KeyCode()
print (KeyCode, Event, EventObject, UnicodeKey, KeyFlags, UniChar, nKeyCode)
frm = MyFrame(None,-1)
frm.Show(True)
app.MainLoop()
You can notice that:
1. MainLoop contains the infinite loop, but it is very effective because it uses system interrupts to listen to events.
2. The event handler is only called when there is an event. CPU is at 0% if you don't touch anything.
3. You can bind to EVT_KEY_DOWN EVT_KEY_UP and EVT_CHAR
Please find a wxPython tutorial and go over it.
Les