|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk |
| 3 | + |
| 4 | +# Creating a counter app using tkinter |
| 5 | + |
| 6 | + |
| 7 | +class MyApplication: |
| 8 | + def __init__(self, master): |
| 9 | + self.master = master |
| 10 | + self.master.title("Counter App") |
| 11 | + self.master.geometry("300x300") |
| 12 | + |
| 13 | + self.create_widgets() |
| 14 | + |
| 15 | + def create_widgets(self): |
| 16 | + frame = ttk.Frame(self.master) |
| 17 | + frame.pack(padx=20, pady=20) |
| 18 | + |
| 19 | + self.label = ttk.Label(frame, text="0", font=("Arial Bold", 70)) |
| 20 | + self.label.grid(row=0, column=0, padx=20, pady=20) |
| 21 | + |
| 22 | + add_button = ttk.Button(frame, text="Add", command=self.on_add_click) |
| 23 | + add_button.grid(row=1, column=0, pady=10) |
| 24 | + |
| 25 | + remove_button = ttk.Button(frame, text="Remove", command=self.on_remove_click) |
| 26 | + remove_button.grid(row=2, column=0, pady=10) |
| 27 | + |
| 28 | + def on_add_click(self): |
| 29 | + current_text = self.label.cget("text") |
| 30 | + new_text = int(current_text) + 1 |
| 31 | + self.label.config(text=new_text) |
| 32 | + |
| 33 | + def on_remove_click(self): |
| 34 | + current_text = self.label.cget("text") |
| 35 | + new_text = int(current_text) - 1 |
| 36 | + self.label.config(text=new_text) |
| 37 | + |
| 38 | + |
| 39 | +if __name__ == "__main__": |
| 40 | + root = tk.Tk() |
| 41 | + app = MyApplication(root) |
| 42 | + root.mainloop() |
0 commit comments