如何避免 python Tkinter 中的键盘冲突?
How can I avoid keyboard conflict in python Tkinter?
我想用Tkinter.The做一个键盘快捷操作命令,键盘事件会调用一个function:when我按'b'
,执行函数'buy'
,当我按's'
,执行函数'sell'
。但是我的GUI.When里面有一个入口,我在这个入口输入一个数字,我会按'b'调用函数'buy'
或者按's'
调用函数'sell'
。当然入口会显示'b'
或's'
。我想按's'
或[调用函数=12=] 和条目只会区分显示 numbers.How 我可以达到这个目的吗?
这是我的代码:
# -*- coding: utf-8 -*-
from Tkinter import *
import tkFont
import datetime
class TradeSystem(object):
"""docstring for TradeSystem"""
def __init__(self):
self.root = Tk()
self.root.geometry('465x180')
self.root.resizable(width=True, height=False)
Label(self.root, text = 'Volume',font = tkFont.Font(size=15, weight='bold')).grid(row=0, column=0)
self.e1_str = StringVar()
self.e1 = Entry(self.root,bg = '#D2E48C',width = 10,textvariable = self.e1_str)
self.e1.grid(row = 1, column = 0)
self.v = IntVar()
self.Cb = Checkbutton(self.root,variable = self.v,text = 'Keyboard active',onvalue = 1,offvalue = 0,command = self.Keyeve)
self.Cb.grid(row = 3,column = 0)
self.currenttime = StringVar()
Label(self.root,textvariable = self.currenttime).grid(row=4, column=0,sticky = NW)
self.t_loop()
self.root.mainloop()
def buy(self,event):
print 'This is buy function.'
def sell(self,event):
print 'This is sell function.'
def rb(self):
self.root.bind('<KeyPress-b>',self.buy)
self.root.bind('<KeyPress-s>',self.sell)
def Keyeve(self):
if self.v.get():
self.rb()
else:
self.root.unbind('<KeyPress-b>')
self.root.unbind('<KeyPress-s>')
def t_loop(self):
self.currenttime.set(datetime.datetime.now().strftime("%Y-%m-%d,%H:%M:%S"))
self.root.after(1000,self.t_loop)
if __name__ == '__main__':
TradeSystem()
我在条目self.e1
中输入一些数字,当keyboard active
为'on'
时,我按'b'
调用函数'buy'
,如:
功能 'buy'
有效。
我只是想要输入区分数字,当我输入数字完成后按'b'
时调用'buy'功能immediately.How我可以实现吗?
使用修改键将文本输入与命令热键分开,例如 Ctrl:
self.root.bind('<Control-b>',self.buy)
self.root.bind('<Control-s>',self.sell)
self.root.bind('<Control-B>',self.buy)
self.root.bind('<Control-S>',self.sell)
注意上面已经绑定了大小写键,所以如果Caps Lock打开它仍然有效。
我想用Tkinter.The做一个键盘快捷操作命令,键盘事件会调用一个function:when我按'b'
,执行函数'buy'
,当我按's'
,执行函数'sell'
。但是我的GUI.When里面有一个入口,我在这个入口输入一个数字,我会按'b'调用函数'buy'
或者按's'
调用函数'sell'
。当然入口会显示'b'
或's'
。我想按's'
或[调用函数=12=] 和条目只会区分显示 numbers.How 我可以达到这个目的吗?
这是我的代码:
# -*- coding: utf-8 -*-
from Tkinter import *
import tkFont
import datetime
class TradeSystem(object):
"""docstring for TradeSystem"""
def __init__(self):
self.root = Tk()
self.root.geometry('465x180')
self.root.resizable(width=True, height=False)
Label(self.root, text = 'Volume',font = tkFont.Font(size=15, weight='bold')).grid(row=0, column=0)
self.e1_str = StringVar()
self.e1 = Entry(self.root,bg = '#D2E48C',width = 10,textvariable = self.e1_str)
self.e1.grid(row = 1, column = 0)
self.v = IntVar()
self.Cb = Checkbutton(self.root,variable = self.v,text = 'Keyboard active',onvalue = 1,offvalue = 0,command = self.Keyeve)
self.Cb.grid(row = 3,column = 0)
self.currenttime = StringVar()
Label(self.root,textvariable = self.currenttime).grid(row=4, column=0,sticky = NW)
self.t_loop()
self.root.mainloop()
def buy(self,event):
print 'This is buy function.'
def sell(self,event):
print 'This is sell function.'
def rb(self):
self.root.bind('<KeyPress-b>',self.buy)
self.root.bind('<KeyPress-s>',self.sell)
def Keyeve(self):
if self.v.get():
self.rb()
else:
self.root.unbind('<KeyPress-b>')
self.root.unbind('<KeyPress-s>')
def t_loop(self):
self.currenttime.set(datetime.datetime.now().strftime("%Y-%m-%d,%H:%M:%S"))
self.root.after(1000,self.t_loop)
if __name__ == '__main__':
TradeSystem()
我在条目self.e1
中输入一些数字,当keyboard active
为'on'
时,我按'b'
调用函数'buy'
,如:
功能 'buy'
有效。
我只是想要输入区分数字,当我输入数字完成后按'b'
时调用'buy'功能immediately.How我可以实现吗?
使用修改键将文本输入与命令热键分开,例如 Ctrl:
self.root.bind('<Control-b>',self.buy)
self.root.bind('<Control-s>',self.sell)
self.root.bind('<Control-B>',self.buy)
self.root.bind('<Control-S>',self.sell)
注意上面已经绑定了大小写键,所以如果Caps Lock打开它仍然有效。