我想用Tkinter发出键盘快速操作命令。键盘事件将调用一个函数:当我按下'b'
执行功能时'buy'
,当我按下's'
执行功能时'sell'
。但是在GUI中有一个输入。我将按'b'调用函数'buy'
或按's'
调用函数'sell'
。当然,该条目将显示'b'
或's'
。我想在按's'
或时调用函数,'b'
而该条目将仅区分并显示数字。我如何实现这个目的?这是我的代码:
# -*- 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'
在输入数字完成后按时,将立即调用“购买”功能。如何实现?
使用修饰键将命令输入的文本与命令热键分开,例如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启用,它仍然可以使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句