|
| 1 | +# exercise 6.1.3 from unit 6 |
| 2 | +''' |
| 3 | +In this exercise you will create basic graphics in Python, by exploring a module from the Python standard library. |
| 4 | +You must write a program that creates a window that looks like this. |
| 5 | +
|
| 6 | +The software window in which the text is written: what's my favorite video? |
| 7 | +And below the text there is a button with the text: click to find out! |
| 8 | +The window must include two elements: text (with a question of your choice) |
| 9 | +and a button. |
| 10 | +When the user clicks the button, the bottom of the screen should open and |
| 11 | +display an image that is actually an answer to the question. for example: |
| 12 | +
|
| 13 | +The software window from the previous step after clicking where you see the |
| 14 | +text + the button and added the image from the favorite video which is actually |
| 15 | +the answer to the question. |
| 16 | +In our case, when you click on the button, a picture of our favorite video |
| 17 | +appears - one that appears in the next.py course, of course... |
| 18 | +
|
| 19 | +Guidelines: |
| 20 | +Read online documentation about a built-in module called tkinter |
| 21 | +and use it to solve the task. |
| 22 | +''' |
| 23 | + |
| 24 | +import tkinter as tk |
| 25 | + |
| 26 | + |
| 27 | +class Application(tk.Frame): |
| 28 | + def __init__(self, master=None): |
| 29 | + super().__init__(master) |
| 30 | + self.master = master |
| 31 | + self.pack() |
| 32 | + self.create_widgets() |
| 33 | + self.image_hidden = True |
| 34 | + |
| 35 | + def create_widgets(self): |
| 36 | + self.question = tk.Label(self, text=" what's my in favorite video?") |
| 37 | + self.question.pack(side="top") |
| 38 | + |
| 39 | + self.quit = tk.Button(self, text="QUIT", fg="red", |
| 40 | + command=self.master.destroy) |
| 41 | + self.quit.pack(side="bottom") |
| 42 | + |
| 43 | + self.show = tk.Button(self, text="SHOW IMAGE", |
| 44 | + fg="blue", command=self.toggle_image) |
| 45 | + self.show.pack(side="bottom") |
| 46 | + |
| 47 | + self.image = tk.PhotoImage(file="favVideo.png") |
| 48 | + self.lable_image = tk.Label( |
| 49 | + self, image=self.image, width=500, height=500) |
| 50 | + |
| 51 | + def toggle_image(self): |
| 52 | + if self.image_hidden: |
| 53 | + self.lable_image.pack() |
| 54 | + else: |
| 55 | + self.lable_image.pack_forget() |
| 56 | + self.image_hidden = not self.image_hidden |
| 57 | + |
| 58 | + |
| 59 | +def main(): |
| 60 | + root = tk.Tk() |
| 61 | + app = Application(master=root) |
| 62 | + app.mainloop() |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
0 commit comments