|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk |
| 3 | + |
| 4 | + |
| 5 | +class MyApplication: |
| 6 | + def __init__(self, master): |
| 7 | + self.master = master |
| 8 | + # Want to understand why .master.title was used? |
| 9 | + self.master.title("Hello World") |
| 10 | + |
| 11 | + self.create_widgets() |
| 12 | + |
| 13 | + def create_widgets(self): |
| 14 | + frame = ttk.Frame(self.master) |
| 15 | + frame.pack(padx=20, pady=20) |
| 16 | + # grid and pack are different geometry managers. |
| 17 | + self.label = ttk.Label(frame, text="Hello World!", font=("Arial Bold", 50)) |
| 18 | + self.label.grid(row=0, column=0, padx=20, pady=20) |
| 19 | + |
| 20 | + # Add a button for interaction |
| 21 | + concat_button = ttk.Button( |
| 22 | + frame, text="Click Me!", command=self.on_button_click |
| 23 | + ) |
| 24 | + concat_button.grid(row=1, column=0, pady=10) |
| 25 | + |
| 26 | + remove_button = ttk.Button( |
| 27 | + frame, text="Remove '!'", command=self.on_remove_click |
| 28 | + ) |
| 29 | + remove_button.grid(row=2, column=0, pady=10) |
| 30 | + |
| 31 | + def on_button_click(self): |
| 32 | + current_text = self.label.cget("text") |
| 33 | + # current_text = self.label["text"] |
| 34 | + #! Solve this. |
| 35 | + new_text = current_text + "!" |
| 36 | + self.label.config(text=new_text) |
| 37 | + |
| 38 | + def on_remove_click(self): |
| 39 | + # current_text = self.label.cget("text") |
| 40 | + current_text = self.label["text"] |
| 41 | + #! Solve this. |
| 42 | + new_text = current_text[:-1] |
| 43 | + self.label.config(text=new_text) |
| 44 | + # TODO: Can make a char matching function, to remove the last char, if it is a '!'. |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + root = tk.Tk() |
| 49 | + app = MyApplication(root) |
| 50 | + root.mainloop() |
0 commit comments