tkinter_calc_ii
This is an old revision of the document!
Table of Contents
TKInter Calc II
Binding
For example, you may wish to bind a key to a button. This helps you streamline the control of an application. If you just have two input methods there could be a problem with things like arrows and backspace or delete messing up the calculator window.
import tkinter as tk from tkinter import ttk def press_eq(event): print('Equals key pressed.') root = tk.Tk() button1 = ttk.Button(root, text='=') button1.bind('<Return>', press_eq) button1.focus() button1.pack(expand=True) root.mainloop()
Binding Mouse
import tkinter root = tkinter.Tk() def right_click_handler(event): # event.num == 2 is MOUSE. if event.num == 2: print("RIGHT CLICK") root.bind("<Button-1>", lambda x: print("LEFT CLICK")) root.bind("<Button>", right_click_handler) root.mainloop()
Here, a lambda is used to handle left clicks with <Button-1> while the more general <Button> handles both left and right, but uses
event.num == 2
to determine right click vs. left click, so it could handle both if needed.
tkinter_calc_ii.1713573743.txt.gz · Last modified: 2024/04/20 00:42 by appledog