User Tools

Site Tools


tkinter_calc_ii

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tkinter_calc_ii [2024/04/20 00:42] appledogtkinter_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't work.
  
 == Binding == Binding
Line 14: Line 17:
  
 button1 = ttk.Button(root, text='=') button1 = ttk.Button(root, text='=')
-button1.bind('<Return>', press_eq)+button1.bind('<KeyPress-Return>', press_eq)
  
  
Line 24: Line 27:
  
 === Binding Mouse === Binding Mouse
 +Observe carefully that <Button> and <Button-1> events are both the same event, but one has a specifier. Events such as <KeyPress> and <KeyPress-Enter> are discussed on some sites such as [[https://www.pythontutorial.net/tkinter/tkinter-event-binding/]].
 +
 <Code:Python> <Code:Python>
 import tkinter import tkinter
Line 40: Line 45:
 </Code> </Code>
  
-Here, a lambda is used to handle left clicks with <Button-1> while the more general <Button> handles both left and right, but uses <Code>event.num == 2</Code> to determine right click vs. left click, so it could handle both if needed.+Here, a lambda is used to handle left clicks with <Button-1> while the more general <Button> handles both left and right, but uses <Code:Python>event.num == 2</Code> to determine right click vs. left click, so it could handle both if needed. 
 + 
 +== Dragging 
 +To capture a click-and-drag event use any of the <B1-Motion> (left-click), <B2-Motion> (right click) or <B3-Motion> (middle mouse button click). Note that Note that when dragging outside of an element the event still fires. 
 + 
 +<Code:Python> 
 +import tkinter 
 + 
 +root = tkinter.Tk() 
 +label = tkinter.Label(root, text="HEY"
 +label.pack() 
 + 
 +def drag_handler(event): 
 +print(event.x, event.y) 
 + 
 +label.bind("<B1-Motion>", drag_handler) 
 + 
 +root.mainloop() 
 +</Code> 
 + 
 +=== Hovering 
 +Hovering over an element sends out two events in sequence, <Enter> and <Leave> 
 + 
 +<Code:Python> 
 +import tkinter 
 + 
 +root = tkinter.Tk() 
 + 
 +label = tkinter.Label(root, text="HELLO", bg="white", fg="navy"
 +label.bind("<Enter>", lambda event: event.widget.config(bg="navy", fg="white")) 
 +label.bind("<Leave>", lambda event: event.widget.config(bg="white", fg="navy")) 
 +label.pack() 
 + 
 +root.mainloop() 
 +</Code>
tkinter_calc_ii.1713573743.txt.gz · Last modified: 2024/04/20 00:42 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki