Python Forum
[Tkinter] Control the application using the keyboard - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Control the application using the keyboard (/thread-21266.html)



Control the application using the keyboard - Christina - Sep-21-2019

Hello,
I have a problem with key control in my tkinter app. After pressing the key ("Enter", →, ←, +, Ctrl+S) I want to do my function. How do I set this control after pressing these keys? For example here is the scheme of the program (not my app):
from tkinter import*
app = Tk()

def MyFunction(x):   #I want to do this after pressing "Enter"
    print(x)

def MyFunction2(y):   #I want to do this after pressing "→"
    y = y + 1
    return y

def MyFunction3(z):   #I want to do this after pressing "Ctrl+S"
    return z**2

def MyFunction4(a):   #I want to do this after pressing "+"
    print(z*2)

app.mainloop()



RE: Control the application using the keyboard - Larz60+ - Sep-21-2019

There are excellent sections on keyboard events in this manual: https://www.it.uu.se/edu/course/homepage/scriptprog/st07/material/tkinterref
This is John Shipman's manual which used to be available at New Mexico Tech, but no longer.
It's rather dated, but still the best tkinter doc available (In my opinion)
see section 54 starting on page 157, all of the keyboard scan codes are in the table beginning on page 161
How to bind them in writeup prior to table