Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| info:cursos:pue:python-pcpp1:m3:1.8 [28/12/2023 13:07] – creat mate | info:cursos:pue:python-pcpp1:m3:1.8 [28/12/2023 19:35] (actual) – mate | ||
|---|---|---|---|
| Línia 1: | Línia 1: | ||
| = 1.8 Interacting with widget methods | = 1.8 Interacting with widget methods | ||
| + | == Widget methods | ||
| + | Widgets have **methods** – you’ve met some of them already. Now we’re going to show you a few more of them, and we’ll start with two which seem to be very specific. We can even say that the sense of their existence is very closely bound to the unique features of **event programming**. | ||
| + | |||
| + | The methods are named (assuming that '' | ||
| + | |||
| + | <code python> | ||
| + | Widget.after(time_ms, | ||
| + | |||
| + | Widget.after_cancel(id) | ||
| + | </ | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | Seems confusing? Not at all. The example will shed more light on it than telling you a long and winding story. | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | |||
| + | def blink(): | ||
| + | global is_white | ||
| + | if is_white: | ||
| + | color = ' | ||
| + | else: | ||
| + | color = ' | ||
| + | is_white = not is_white | ||
| + | frame.config(bg=color) | ||
| + | frame.after(500, | ||
| + | |||
| + | |||
| + | is_white = True | ||
| + | window = tk.Tk() | ||
| + | frame = tk.Frame(window, | ||
| + | frame.after(500, | ||
| + | frame.pack() | ||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | The code we’ve written to demonstrate how the '' | ||
| + | |||
| + | There’s a function named '' | ||
| + | |||
| + | Note: there is no **explicit invocation** of the function inside the code. Moreover, it isn’t assigned as a callback. The question is – who invokes it? | ||
| + | |||
| + | The event managers do, because: | ||
| + | |||
| + | * we initially encourage it to make the invocation before the frame widget is packed into the main window; | ||
| + | * we continue to encourage it every time the '' | ||
| + | |||
| + | Try to change the delay time (the first method’s argument) and check how the application works then. | ||
| + | |||
| + | == The destroy() method | ||
| + | The '' | ||
| + | |||
| + | <code python> | ||
| + | Widget.destroy() | ||
| + | </ | ||
| + | |||
| + | Do you remember? We used this method to close the main window and to stop the whole application. You can use the method in a less devastating manner to get rid of unnecessary widgets while keeping the application alive. | ||
| + | |||
| + | Note: if the widget you want to destroy has **children** (when other widgets are embedded inside it, which can happen with frames) the children will be destroyed, too (this rule works **recursively**). | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | |||
| + | def suicide(): | ||
| + | frame.destroy() | ||
| + | |||
| + | |||
| + | window = tk.Tk() | ||
| + | frame = tk.Frame(window, | ||
| + | button = tk.Button(frame, | ||
| + | button.place(x=10, | ||
| + | frame.after(5000, | ||
| + | frame.pack() | ||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | The example prepares a window filled with a **frame** that is the parent of one Button. Note – we’ve ordered the event manager to activate the '' | ||
| + | |||
| + | The function destroys the frame, but first it destroys all the frame’s children and its children’s children and … okay, let’s end the story here . It’s infinite – telling it will take too much time. | ||
| + | |||
| + | Run the code and check how it works. | ||
| + | |||
| + | As you already know, widgets may or may not have the **focus**. At most one widget can have the focus. When you use a keyboard to interact with the application, | ||
| + | <code python> | ||
| + | wi.focus_get() | ||
| + | wi.focus_set() | ||
| + | </ | ||
| + | * the '' | ||
| + | * the '' | ||
| + | |||
| + | Look at the code we've provided in the editor. This simple sample application shows how the focus can be **moved** between two buttons and uses the '' | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | |||
| + | def flip_focus(): | ||
| + | if window.focus_get() is button_1: | ||
| + | button_2.focus_set() | ||
| + | else: | ||
| + | button_1.focus_set() | ||
| + | window.after(1000, | ||
| + | |||
| + | |||
| + | window = tk.Tk() | ||
| + | button_1 = tk.Button(window, | ||
| + | button_1.pack() | ||
| + | button_2 = tk.Button(window, | ||
| + | button_2.pack() | ||
| + | window.after(1000, | ||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | Try to add one or more buttons to the window and change the '' | ||
| + | |||
| + | We’ll say “goodbye” to widgets now, as we’re going to place the **observable variables** under our magnifying glass. | ||