Tkinter - Keyboard Input

In this example we have added two new fucntions and attached them to the events of a set key being pressed. This triggers set functions to be ran.
import tkinter as tk

mywindow = tk.Tk()

def buttonPress():
    print("Button Pressed!!")

def textBox():
    print(textb.get())
    
def slideValue():
    print (Slider.get())

#Label
label = tk.Label(mywindow, text="Label Text")
label.grid(row=0,column=1)

#Button
button = tk.Button(mywindow,text='Press',command=buttonPress)
button.grid(row=1,column=1)

#Textbox
textb = tk.Entry(mywindow,text="Entry")
textbutton = tk.Button(mywindow,text="Text Box",command=textBox)
textb.grid(row=2,column=1)
textbutton.grid(row=2,column=2)

#Slider
Slider = (tk.Scale(mywindow,label=('Slider'),variable=0))
SliderButton = (tk.Button(mywindow,text='Slider',command=slideValue))
Slider.grid(row=3,column=1)
SliderButton.grid(row=3,column=2)

def func_return(event):
    print ("You hit return")
mywindow.bind("<Return>", func_return)

def func_w(event):
    print ("You hit w")
mywindow.bind("<KeyPress-w>", func_w)
              
mywindow.mainloop()
This creates the same frame:

But when return or "w" are pressed after each other, the follwoing is returned.
You hit return
You hit w
You hit return
You hit w
You hit return
You hit w
You hit return
You hit w
You hit return






Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Must Read Articles

Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter _ Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines and labels.
Tkinter - Pack vs Grid vs Place - tkinter provides flexibility in how the layout the widgets. 
Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.