|
| 1 | +# Author: Nitkarsh Chourasia |
| 2 | +# Date created: 28/12/2023 |
| 3 | + |
| 4 | +# Import the required libraries |
1 | 5 | import tkinter as tk
|
2 | 6 | from tkinter import ttk
|
3 | 7 |
|
4 |
| -# Creating a counter app using tkinter |
5 |
| - |
6 | 8 |
|
7 | 9 | class MyApplication:
|
| 10 | + """A class to create a counter app.""" |
| 11 | + |
8 | 12 | def __init__(self, master):
|
| 13 | + # Initialize the master window |
9 | 14 | self.master = master
|
| 15 | + # Set the title and geometry of the master window |
10 | 16 | self.master.title("Counter App")
|
11 | 17 | self.master.geometry("300x300")
|
12 | 18 |
|
| 19 | + # Create the widgets |
13 | 20 | self.create_widgets()
|
14 | 21 |
|
| 22 | + # Create the widgets |
15 | 23 | def create_widgets(self):
|
| 24 | + # Create a frame to hold the widgets |
16 | 25 | frame = ttk.Frame(self.master)
|
| 26 | + # Pack the frame to the master window |
17 | 27 | frame.pack(padx=20, pady=20)
|
18 | 28 |
|
| 29 | + # Create a label to display the counter |
19 | 30 | self.label = ttk.Label(frame, text="0", font=("Arial Bold", 70))
|
| 31 | + # Grid the label to the frame |
20 | 32 | self.label.grid(row=0, column=0, padx=20, pady=20)
|
21 | 33 |
|
| 34 | + # Add a button for interaction to increase the counter |
22 | 35 | add_button = ttk.Button(frame, text="Add", command=self.on_add_click)
|
| 36 | + # Grid the button to the frame |
23 | 37 | add_button.grid(row=1, column=0, pady=10)
|
24 | 38 |
|
| 39 | + # Add a button for interaction to decrease the counter |
25 | 40 | remove_button = ttk.Button(frame, text="Remove", command=self.on_remove_click)
|
| 41 | + # Grid the button to the frame |
26 | 42 | remove_button.grid(row=2, column=0, pady=10)
|
27 | 43 |
|
| 44 | + # Add a click event handler |
28 | 45 | def on_add_click(self):
|
| 46 | + # Get the current text of the label |
29 | 47 | current_text = self.label.cget("text")
|
| 48 | + # Convert the text to an integer and add 1 |
30 | 49 | new_text = int(current_text) + 1
|
| 50 | + # Set the new text to the label |
31 | 51 | self.label.config(text=new_text)
|
32 | 52 |
|
| 53 | + # Add a click event handler |
33 | 54 | def on_remove_click(self):
|
| 55 | + # Get the current text of the label |
34 | 56 | current_text = self.label.cget("text")
|
| 57 | + # Convert the text to an integer and subtract 1 |
35 | 58 | new_text = int(current_text) - 1
|
| 59 | + # Set the new text to the label |
36 | 60 | self.label.config(text=new_text)
|
37 | 61 |
|
38 | 62 |
|
39 | 63 | if __name__ == "__main__":
|
| 64 | + # Create the root window |
40 | 65 | root = tk.Tk()
|
| 66 | + # Create an instance of the application |
41 | 67 | app = MyApplication(root)
|
| 68 | + # Run the app |
42 | 69 | root.mainloop()
|
0 commit comments