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.5 [23/12/2023 20:07] – creat mate | info:cursos:pue:python-pcpp1:m3:1.5 [23/12/2023 20:33] (actual) – mate | ||
|---|---|---|---|
| Línia 20: | Línia 20: | ||
| Our window looks like this one for now: | Our window looks like this one for now: | ||
| + | {{ : | ||
| + | There' | ||
| + | |||
| + | Our new friend is called '' | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | |||
| + | label = tk.Label(window, | ||
| + | label.pack() | ||
| + | |||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | As you can see, we're using the '' | ||
| + | |||
| + | Let's welcome Label into our window: | ||
| + | {{ : | ||
| + | |||
| + | Note: '' | ||
| + | |||
| + | Our next companion will be '' | ||
| + | |||
| + | A '' | ||
| + | |||
| + | Let's check it out. | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | |||
| + | label = tk.Label(window, | ||
| + | label.pack() | ||
| + | |||
| + | frame = tk.Frame(window, | ||
| + | frame.pack() | ||
| + | |||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | This is how the Frame manifests its presence: | ||
| + | {{ : | ||
| + | |||
| + | Make our window great again. | ||
| + | |||
| + | Now we invite a '' | ||
| + | |||
| + | Our '' | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | |||
| + | label = tk.Label(window, | ||
| + | label.pack() | ||
| + | |||
| + | frame = tk.Frame(window, | ||
| + | frame.pack() | ||
| + | |||
| + | button = tk.Button(window, | ||
| + | button.pack(fill=tk.X) | ||
| + | |||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | This is what our window looks like now: | ||
| + | {{ : | ||
| + | |||
| + | Our next component is completely **invisible**. You won't find it in the window area. | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | |||
| + | label = tk.Label(window, | ||
| + | label.pack() | ||
| + | |||
| + | frame = tk.Frame(window, | ||
| + | frame.pack() | ||
| + | |||
| + | button = tk.Button(window, | ||
| + | button.pack(fill=tk.X) | ||
| + | |||
| + | switch = tk.IntVar() | ||
| + | switch.set(1) | ||
| + | |||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | It's the switch variable. Can't you see it? It's set to hold an object of the '' | ||
| + | |||
| + | **No, we can' | ||
| + | |||
| + | If you want such an object to store an integer value, you can't use the assignment operator. The class offers a dedicated method for that purpose, and the method is named '' | ||
| + | |||
| + | Note: we've used the method to store a value of 1 inside the object. | ||
| + | |||
| + | As the window' | ||
| + | |||
| + | The next step adds a brand new widget to our window – it’s a '' | ||
| + | |||
| + | It’s a **small square** which can be filled with a **tick mark**, or which can be empty. | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | win = tk.Tk() | ||
| + | |||
| + | label = tk.Label(win, | ||
| + | label.pack() | ||
| + | |||
| + | frame = tk.Frame(win, | ||
| + | frame.pack() | ||
| + | |||
| + | button = tk.Button(win, | ||
| + | button.pack(fill=tk.X) | ||
| + | |||
| + | switch = tk.IntVar() | ||
| + | switch.set(1) | ||
| + | |||
| + | checkbutton = tk.Checkbutton(win, | ||
| + | checkbutton.pack() | ||
| + | |||
| + | win.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | The '' | ||
| + | |||
| + | * the **ON** state when the '' | ||
| + | * the **OFF** state when the'' | ||
| + | |||
| + | Take a look at the '' | ||
| + | |||
| + | Yes, it’s a variable argument. Note – it’s set to the previously created switch object. The assignment creates a **bidirectional link** between the object and the widget. How does it work? | ||
| + | |||
| + | * If you check or uncheck the '' | ||
| + | * If you change the state of the '' | ||
| + | |||
| + | Look, the '' | ||
| + | |||
| + | Let's check it. | ||
| + | |||
| + | This is what our window looks like now: | ||
| + | {{ : | ||
| + | |||
| + | Okay, it definitely looks as expected, but how can we be sure that the '' | ||
| + | |||
| + | We’ll show you soon. Stay tuned. | ||
| + | |||
| + | Now we add a very important, and internally an extremely complicated, | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | |||
| + | label = tk.Label(window, | ||
| + | label.pack() | ||
| + | |||
| + | frame = tk.Frame(window, | ||
| + | frame.pack() | ||
| + | |||
| + | button = tk.Button(window, | ||
| + | button.pack(fill=tk.X) | ||
| + | |||
| + | switch = tk.IntVar() | ||
| + | switch.set(1) | ||
| + | |||
| + | checkbutton = tk.Checkbutton(window, | ||
| + | checkbutton.pack() | ||
| + | |||
| + | entry = tk.Entry(window, | ||
| + | entry.pack() | ||
| + | |||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | Entry is designed to let the user enter simple, one-line data, like single numbers, names, addresses, etc. | ||
| + | |||
| + | We’ve added one to our window. It creates an **input field** 30 characters wide. You can play with it if you want, but it’s completely inoperative as far. We only want to show you what it looks like. | ||
| + | |||
| + | Isn’t our window lovely? | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | We’ve added two widgets at once – look! | ||
| + | |||
| + | These are the '' | ||
| + | |||
| + | How do we achieve such an effect? The switch object will help us with it. | ||
| + | |||
| + | <code python> | ||
| + | >import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | |||
| + | label = tk.Label(window, | ||
| + | label.pack() | ||
| + | |||
| + | frame = tk.Frame(window, | ||
| + | frame.pack() | ||
| + | |||
| + | button = tk.Button(window, | ||
| + | button.pack(fill=tk.X) | ||
| + | |||
| + | switch = tk.IntVar() | ||
| + | switch.set(1) | ||
| + | |||
| + | checkbutton = tk.Checkbutton(window, | ||
| + | checkbutton.pack() | ||
| + | |||
| + | entry = tk.Entry(window, | ||
| + | entry.pack() | ||
| + | |||
| + | radiobutton_1 = tk.Radiobutton(window, | ||
| + | radiobutton_1.pack() | ||
| + | radiobutton_2 = tk.Radiobutton(window, | ||
| + | radiobutton_2.pack() | ||
| + | |||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | Our two '' | ||
| + | |||
| + | * The '' | ||
| + | * The '' | ||
| + | |||
| + | The communication through the '' | ||
| + | |||
| + | * selecting one of the '' | ||
| + | * simultaneously, | ||
| + | |||
| + | As the '' | ||
| + | |||
| + | Do you want to check it? Go ahead! | ||
| + | |||
| + | We hope we’ve lived up to your expectations: | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | There is something more you should note. As the '' | ||
| + | |||
| + | Check this carefully! | ||
| + | |||
| + | If you check/ | ||
| + | |||
| + | Now you’re ready to dive deeper into '' | ||
| + | |||
| + | Let’s dive together. | ||
| + | |||
| + | Okay, let’s provide some serious work to our '' | ||