Tkinter window contains many inbuilt functionalities and features which can be taken and used for various application development. There might be cases when we have to run a particular part of the application with the help of some key or function. It can be achieved by binding a particular key with the callback that contains the functions for the operation. The key can be anything from Mouse Buttons to Keyboard Keys. We can even bind the callback with Keyboard Key Combinations.
Example
#Import the Tkinter Library from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry of window win.geometry("700x350") #Define a callback function for exit def quit_program(e): win.destroy() #Add a Label widget label = Label(win, text= "Press Ctrl + x to Exit", font= ('Helvetica 15 bold')) label.pack(pady= 40) #Bind the Keyboard shortcut Key win.bind('<Control-x>', quit_program) win.mainloop()
Output
In the above code, we have added a combination of Keys. Pressing the keys will close the window.