From cb05d52c4dce4737f3a232811c434f431c4b024a Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 28 Dec 2023 01:11:51 +0530 Subject: [PATCH] add: hello world with a !++ and !-- Two buttons to add and remove exclamatory to the sentence. --- .../hello_world_label.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 nitkarshchourasia/GUI_apps/tkinter_apps/hello_world_excla_increment_app/hello_world_label.py diff --git a/nitkarshchourasia/GUI_apps/tkinter_apps/hello_world_excla_increment_app/hello_world_label.py b/nitkarshchourasia/GUI_apps/tkinter_apps/hello_world_excla_increment_app/hello_world_label.py new file mode 100644 index 00000000000..acdd66f44c7 --- /dev/null +++ b/nitkarshchourasia/GUI_apps/tkinter_apps/hello_world_excla_increment_app/hello_world_label.py @@ -0,0 +1,50 @@ +import tkinter as tk +from tkinter import ttk + + +class MyApplication: + def __init__(self, master): + self.master = master + # Want to understand why .master.title was used? + self.master.title("Hello World") + + self.create_widgets() + + def create_widgets(self): + frame = ttk.Frame(self.master) + frame.pack(padx=20, pady=20) + # grid and pack are different geometry managers. + self.label = ttk.Label(frame, text="Hello World!", font=("Arial Bold", 50)) + self.label.grid(row=0, column=0, padx=20, pady=20) + + # Add a button for interaction + concat_button = ttk.Button( + frame, text="Click Me!", command=self.on_button_click + ) + concat_button.grid(row=1, column=0, pady=10) + + remove_button = ttk.Button( + frame, text="Remove '!'", command=self.on_remove_click + ) + remove_button.grid(row=2, column=0, pady=10) + + def on_button_click(self): + current_text = self.label.cget("text") + # current_text = self.label["text"] + #! Solve this. + new_text = current_text + "!" + self.label.config(text=new_text) + + def on_remove_click(self): + # current_text = self.label.cget("text") + current_text = self.label["text"] + #! Solve this. + new_text = current_text[:-1] + self.label.config(text=new_text) + # TODO: Can make a char matching function, to remove the last char, if it is a '!'. + + +if __name__ == "__main__": + root = tk.Tk() + app = MyApplication(root) + root.mainloop()