views:

87

answers:

2

All,

I am writing a wxPython app in which I want (at the moment) to print the name of the key that was pressed. I have a dictionary that maps, for example, the WXK_BACK to "back" which seems a sane. However, which file must I import (include?) to get the definition of WXK_BACK ?

I have the import wx statement, but am unsure which specific file holds the secrets -- help?

All help appreciated.

Thanks!

bp

A: 

You only need to import wx for the WXK_BACK symbol. Code that looks something like the following should work.

import wx

class MyClass( wx.Window ):
    def __init__(self):
        self.Bind(wx.EVT_CHAR, self.OnChar)
    def OnChar(self, evt):
        x = evt.GetKeyCode()
        if x==wx.WXK_BACK:
            print "back"
tom10
Thanks! I am still a bit new to this python/wxpython, and the wx. prefix eluded me. (I was using WXK_BACK rather than wx.WXK_BACK).Thank you very much for your help.:bp:
Billy Pilgrim
You're welcome.
tom10
A: 

also you do not need to generate key to name map by hand you can do something like this, to generate keycode to name mapping automatically

import wx

keyMap = {}
for varName in vars(wx):
    if varName.startswith("WXK_"):
        keyMap[varName] = getattr(wx, varName)

print keyMap

then in OnChar you can just do this

def OnChar(self, evt):
    try:
        print keyMap[evt.GetKeyCode()]
    except KeyError:
        print "keycode",evt.GetKeyCode(), "not found"
Anurag Uniyal
Thank you!That is easier, AND I learned something. I appreciate your all your help.:bp:
Billy Pilgrim