在做Wxpython的登录界面,通过一个“登录”Button实现了登录功能,那么如何像其他软件/网页一样实现回车=登录
一、回车执行函数
在登录界面的class里写一个回车执行的函数
def ReturnLogin(self,event):
keycode = event.GetUnicodeKey()
if keycode == wx.WXK_RETURN:
self.LoginButton(event) # 调用前面的登录函数
keycode = event.GetUnicodeKey()
这行代码会获得每一次键盘输入的KeyCode(int),可以自己在下面print一下试试,比如回车键就会print出一个13,一旦keycode==13==wx.WXK_RETURN就通过if判断执行self.LoginButton(event)
GetUnicodeKey(self)
Returns the Unicode character corresponding to this key event.
If the key pressed doesn’t have any character value (e.g. a cursor key) this method will return WXK_NONE . In this case you should use GetKeyCode to retrieve the value of the key.
This function is only available in Unicode build, i.e. when USE_UNICODE is 1.
Return type
int
wx.WXK_RETURN有一个键盘键位的对照表也在官方文档里
二、绑定事件
在登录界面给密码输入框绑定回车的事件
def LoginInterface(self):
self.user_password.Bind(wx.EVT_KEY_UP,self.ReturnLogin)
https://docs.wxpython.org/wx.KeyEvent.html#wx-keyevent.是KeyEvent的文档页面,在里面有两种键盘事件的状态,这里用到的就是up(回车键按下松开)
然后测试一下,大功告成了