2

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?

Community
  • 1
  • 1
Peng He
  • 2,023
  • 5
  • 17
  • 24
  • 1
    I'd suggest using modifiers, like Ctrl-B instead of just B. – TigerhawkT3 Jan 06 '16 at 14:04
  • That's worked.I'm making a quick order system,so I want to use just one key as far as possible. – Peng He Jan 06 '16 at 14:11
  • If you really don't wont to use `Ctrl-B` then you will have to bind function to `` event in `Entry` to check all keys pressed in entry. – furas Jan 06 '16 at 14:13
  • Yep,when I get 'b' in entry,I just delete it?I have thought about it.But I guess it's not a efficient way... – Peng He Jan 06 '16 at 14:17

1 Answers1

1

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.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Thx. Is there any way to just bind one key? – Peng He Jan 06 '16 at 15:08
  • Well, `b` and `B` are different keys, so if you want both to work the same, bind both. If you want special behavior for Ctrl-Shift-(button), just bind it to the "uppercase" button (like `"" for Ctrl-Shift-backtick). – TigerhawkT3 Jan 06 '16 at 15:15
  • I mean just bind one key,like b or B ,not ''. – Peng He Jan 06 '16 at 15:27
  • @PengHe - If you want to validate input so that only numbers are accepted into the text entry and certain non-accepted keys (like b, B, s, or S) run a function, see [here](http://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter). Using a modifier key is just easier. – TigerhawkT3 Jan 07 '16 at 02:58