tkinter_calc_ii
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tkinter_calc_ii [2024/04/20 00:34] – created appledog | tkinter_calc_ii [2024/04/20 00:53] (current) – appledog | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| = TKInter Calc II | = TKInter Calc II | ||
| + | |||
| + | == Themed Widgets | ||
| + | Use ttk.button() //etc.// whenever possible. That's because ttk is the newest version. tk.button() and others are old, don't use them unless ttk. doesn' | ||
| == Binding | == 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(' | ||
| + | |||
| + | root = tk.Tk() | ||
| + | |||
| + | button1 = ttk.Button(root, | ||
| + | button1.bind('< | ||
| + | |||
| + | |||
| + | button1.focus() | ||
| + | button1.pack(expand=True) | ||
| + | |||
| + | root.mainloop() | ||
| + | </ | ||
| + | |||
| + | === Binding Mouse | ||
| + | Observe carefully that < | ||
| + | |||
| + | < | ||
| + | import tkinter | ||
| + | |||
| + | root = tkinter.Tk() | ||
| + | |||
| + | def right_click_handler(event): | ||
| + | # event.num == 2 is MOUSE. | ||
| + | if event.num == 2: | ||
| + | print(" | ||
| + | |||
| + | root.bind("< | ||
| + | root.bind("< | ||
| + | |||
| + | root.mainloop() | ||
| + | </ | ||
| + | |||
| + | Here, a lambda is used to handle left clicks with < | ||
| + | |||
| + | == Dragging | ||
| + | To capture a click-and-drag event use any of the < | ||
| + | |||
| + | < | ||
| + | import tkinter | ||
| + | |||
| + | root = tkinter.Tk() | ||
| + | label = tkinter.Label(root, | ||
| + | label.pack() | ||
| + | |||
| + | def drag_handler(event): | ||
| + | print(event.x, | ||
| + | |||
| + | label.bind("< | ||
| + | |||
| + | root.mainloop() | ||
| + | </ | ||
| + | |||
| + | === Hovering | ||
| + | Hovering over an element sends out two events in sequence, < | ||
| + | |||
| + | < | ||
| + | import tkinter | ||
| + | |||
| + | root = tkinter.Tk() | ||
| + | |||
| + | label = tkinter.Label(root, | ||
| + | label.bind("< | ||
| + | label.bind("< | ||
| + | label.pack() | ||
| + | |||
| + | root.mainloop() | ||
| </ | </ | ||
tkinter_calc_ii.1713573245.txt.gz · Last modified: 2024/04/20 00:34 by appledog
