Peng He
Peng He

Reputation: 2213

How can I avoid keyboard conflict in python Tkinter?

I want to make a keyboard quick operation command with Tkinter.The keyboard event will invoke a function:when I press 'b',executive function 'buy',and when I press 's',executive function 'sell'.But there is a entry in my GUI.When I input a number in this entry,I'll press 'b' to invoke function 'buy' or press 's' to invoke function 'sell'.Of course the entry will display 'b' or 's'.I want to invoke function when I press 's' or 'b' and the entry will just distinguish and display numbers.How can I achieve this purpose ? Here is my code:

# -*- 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()

I input some numbers in entry self.e1,and when the keyboard active is 'on',I press 'b' to invoke function 'buy',like:

enter image description here

And fucntion 'buy' worked.

enter image description here

I just want the entry to distinguish numbers and when I press 'b' after I complete input numbers function 'buy' is invoked immediately.How can I achieve that?

Upvotes: 2

Views: 462

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49318

Separate the text input from command hotkeys by using a modifier key, like 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)

Note that the above has bound both the uppercase and lowercase keys, so that it still works if Caps Lock is on.

Upvotes: 1

Related Questions