Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
Ambdós costats versió prèvia Revisió prèvia | |||
info:cursos:pue:python-pcpp1:m3:1.6 [23/12/2023 11:50] – mate | info:cursos:pue:python-pcpp1:m3:1.6 [28/12/2023 03:29] (actual) – [Useful events] mate | ||
---|---|---|---|
Línia 102: | Línia 102: | ||
== Useful events | == Useful events | ||
+ | We’ve gathered some of the most usable event names – don’t try to learn them by heart. | ||
+ | {{: | ||
+ | {{: | ||
+ | {{: | ||
+ | {{: | ||
+ | {{: | ||
+ | |||
+ | Don’t be afraid if some of the events look a bit suspicious. You’ll get used to them soon. | ||
+ | |||
+ | Note: | ||
+ | |||
+ | * a callback designed for usage with the command property/ | ||
+ | * a callback intended to cooperate with the '' | ||
+ | * fortunately, | ||
+ | |||
+ | <code python> | ||
+ | def callback(ev=None): | ||
+ | : | ||
+ | : | ||
+ | </ | ||
+ | * the callback will work flawlessly in both of these contexts, and moreover, it’ll give you the chance to identify which one of the two possible styles of launch has just occurred. | ||
+ | |||
+ | We’re going to change our previous example a bit by making it sensitive to more than just one click. | ||
+ | |||
+ | We've provided the newer version of our code in the editor. | ||
+ | |||
+ | <code python> | ||
+ | import tkinter as tk | ||
+ | from tkinter import messagebox | ||
+ | |||
+ | |||
+ | def click(event=None): | ||
+ | tk.messagebox.showinfo(" | ||
+ | |||
+ | |||
+ | window = tk.Tk() | ||
+ | label = tk.Label(window, | ||
+ | label.bind("< | ||
+ | label.pack() | ||
+ | |||
+ | button = tk.Button(window, | ||
+ | button.pack(fill=tk.X) | ||
+ | |||
+ | frame = tk.Frame(window, | ||
+ | frame.bind("< | ||
+ | frame.pack() | ||
+ | |||
+ | window.mainloop() | ||
+ | |||
+ | </ | ||
+ | |||
+ | Pay attention to '' | ||
+ | |||
+ | We encourage you to play with the code – test the behavior of some of the other events. It’ll be fun... we think. | ||
+ | |||
+ | We’ve said previously that an event is actually an object. Let’s shed some light on that. | ||
+ | |||
+ | An event object is an instantiation of the '' | ||
+ | |||
+ | <code python> | ||
+ | class Event: | ||
+ | : | ||
+ | : | ||
+ | </ | ||
+ | |||
+ | Note: not all properties have meaning for every event. If the event is related to some of the mouse actions, the object’s parts referring to the keyboard remain uninitialized, | ||
+ | |||
+ | Let us show you some of the properties. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Let’s modify our code again. We want it to unveil some info coming in with the event object. Look at the code in the editor. | ||
+ | |||
+ | <code python> | ||
+ | import tkinter as tk | ||
+ | from tkinter import messagebox | ||
+ | |||
+ | |||
+ | def click(event=None): | ||
+ | if event is None: | ||
+ | tk.messagebox.showinfo(" | ||
+ | else: | ||
+ | string = " | ||
+ | ", | ||
+ | tk.messagebox.showinfo(" | ||
+ | |||
+ | |||
+ | window = tk.Tk() | ||
+ | label = tk.Label(window, | ||
+ | label.bind("< | ||
+ | label.pack() | ||
+ | |||
+ | button = tk.Button(window, | ||
+ | button.pack(fill=tk.X) | ||
+ | |||
+ | frame = tk.Frame(window, | ||
+ | frame.bind("< | ||
+ | frame.pack() | ||
+ | |||
+ | window.mainloop() | ||
+ | </ | ||
+ | |||
+ | We encourage you again to carry out some experiments with this code. Use it to discover the event’s anatomy in detail. | ||
+ | |||
+ | A callback bound to a certain event may be **unbound** at any moment. | ||
+ | |||
+ | Let’s analyze the process in relation to clickable widgets i.e., those having the '' | ||
+ | |||
+ | If you unbind a callback from an event, the widget **stops reacting** to the event. If you want to reverse this action, you must **bind the callback again**. | ||
+ | |||
+ | We haven’t said a word on modifying a widget’s properties, and we’re going to discuss it thoroughly in the next section, so please forgive us for only doing it briefly now. | ||
+ | |||
+ | If you want to modify a property named '' | ||
+ | <code python> | ||
+ | |||
+ | This means that if you want to unbind your current callback from a '' | ||
+ | <code python> | ||
+ | |||
+ | This binds an empty (i.e., doing absolutely nothing) function to the widget’s callback. | ||
+ | |||
+ | Let’s test it. | ||
+ | |||
+ | Our application creates a window with two buttons in it. The first one works as an on/off switch, while the switch changes the behavior of the second button. When the switch is ON, clicking the second button activates a message box. When the switch if OFF, clicking the second button has no effect. Moreover, the second button’s title changes according to the switch’s state. | ||
+ | |||
+ | Note the method we use to change the button’s title. | ||
+ | |||
+ | <code python> | ||
+ | import tkinter as tk | ||
+ | from tkinter import messagebox | ||
+ | |||
+ | |||
+ | def on_off(): | ||
+ | global switch | ||
+ | if switch: | ||
+ | button_2.config(command=lambda: | ||
+ | button_2.config(text=" | ||
+ | else: | ||
+ | button_2.config(command=peekaboo) | ||
+ | button_2.config(text=" | ||
+ | switch = not switch | ||
+ | |||
+ | |||
+ | def peekaboo(): | ||
+ | messagebox.showinfo("", | ||
+ | |||
+ | |||
+ | def do_nothing(): | ||
+ | pass | ||
+ | |||
+ | |||
+ | switch = True | ||
+ | window = tk.Tk() | ||
+ | buton_1 = tk.Button(window, | ||
+ | buton_1.pack() | ||
+ | button_2 = tk.Button(window, | ||
+ | button_2.pack() | ||
+ | window.mainloop() | ||
+ | </ | ||
+ | |||
+ | The two faces of our window look like this: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Now we’ll do the same trick again, but this time with the non-clickable widget. | ||
+ | |||
+ | To unbind a callback previously bound with the '' | ||
+ | <code python> | ||
+ | |||
+ | The method requires one argument identifying the event being unbound. | ||
+ | |||
+ | Note: the information about a previously used callback is lost. You cannot retrieve it automatically and you must repeat the bind() invocation. | ||
+ | |||
+ | Let’s jump into the code. | ||
+ | |||
+ | <code python> | ||
+ | import tkinter as tk | ||
+ | |||
+ | |||
+ | def on_off(): | ||
+ | global switch | ||
+ | if switch: | ||
+ | label.unbind("< | ||
+ | else: | ||
+ | label.bind("< | ||
+ | switch = not switch | ||
+ | |||
+ | |||
+ | def rhyme(dummy): | ||
+ | global word_no, words | ||
+ | word_no += 1 | ||
+ | label.config(text=words[word_no % len(words)]) | ||
+ | |||
+ | |||
+ | switch = True | ||
+ | words = [" | ||
+ | word_no = 0 | ||
+ | window = tk.Tk() | ||
+ | button = tk.Button(window, | ||
+ | button.pack() | ||
+ | label = tk.Label(window, | ||
+ | label.bind("< | ||
+ | label.pack() | ||
+ | window.mainloop() | ||
+ | </ | ||
+ | |||
+ | The application contains two widgets: one '' | ||
+ | |||
+ | We bind a callback to the '' | ||
+ | |||
+ | This functionality can be turned off and on by clicking the button. As you can see, the '' | ||
+ | |||
+ | Try to modify the code to use a few other events to trigger the switch. | ||
+ | |||
+ | The main tkinter window has a method named '' | ||
+ | |||
+ | There is also a method named '' | ||
+ | |||
+ | <code python> | ||
+ | window.bind_all(event, | ||
+ | window.unbind_all(event) | ||
+ | </ | ||
+ | |||
+ | We used the '' | ||
+ | |||
+ | <code python> | ||
+ | import tkinter as tk | ||
+ | from tkinter import messagebox | ||
+ | |||
+ | |||
+ | def hello(dummy): | ||
+ | messagebox.showinfo("", | ||
+ | |||
+ | |||
+ | window = tk.Tk() | ||
+ | button = tk.Button(window, | ||
+ | button.pack() | ||
+ | label = tk.Label(window, | ||
+ | label.pack() | ||
+ | frame = tk.Frame(window, | ||
+ | frame.pack() | ||
+ | window.bind_all("< | ||
+ | window.mainloop() | ||
+ | |||
+ | </ | ||
+ | |||
+ | Play with the code. Don’t worry, it’s safe. | ||
+ | |||
+ | Now we’re going to take you on a trip to Widget land. It’ll be an exciting journey, we promise. |