|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk |
| 3 | + |
| 4 | + |
| 5 | +# class Player: |
| 6 | +# def __init__(self, name: str, score: int): |
| 7 | +# self.name = name |
| 8 | +# self.score = int(score) # ensure score is an integer |
| 9 | + |
| 10 | +# def add_score(self, value: int): |
| 11 | +# self.score += int(value) |
| 12 | + |
| 13 | +# def __str__(self): |
| 14 | +# return f"Player(name='{self.name}', score={self.score})" |
| 15 | + |
| 16 | +# ----------------------------- |
| 17 | +# Configuration |
| 18 | +# ----------------------------- |
| 19 | + |
| 20 | +START_TIME = 60 # countdown start time in seconds |
| 21 | + |
| 22 | + |
| 23 | +P1_name = str(input("Enter Player 1 Name: ")) |
| 24 | +P2_name = str(input("Enter Player 2 Name: ")) |
| 25 | + |
| 26 | + |
| 27 | +class App(tk.Tk): |
| 28 | + def __init__(self, root): |
| 29 | + self.root = root |
| 30 | + super().__init__() |
| 31 | + |
| 32 | + self.title("Cuthbert Baines Bar Billiards Auto-Score") |
| 33 | + self.geometry("500x400") |
| 34 | + self.resizable(False, False) |
| 35 | + |
| 36 | + # State variables |
| 37 | + self.time_left = START_TIME |
| 38 | + self.P1Score = tk.Var(value=0) |
| 39 | + self.P2Score = tk.Var(value=0) |
| 40 | + |
| 41 | + # Build UI |
| 42 | + self.create_widgets() |
| 43 | + self.update_timer() |
| 44 | + |
| 45 | + # ----------------------------- |
| 46 | + # UI Layout |
| 47 | + # ----------------------------- |
| 48 | + def create_widgets(self): |
| 49 | + |
| 50 | + # Timer display |
| 51 | + self.timer_label = ttk.Label( |
| 52 | + self, text="Time: 60", font=("Arial", 18) |
| 53 | + ) |
| 54 | + self.timer_label.pack(pady=10) |
| 55 | + |
| 56 | + # Score frame |
| 57 | + score_frame = ttk.Frame(self) |
| 58 | + score_frame.pack(pady=10) |
| 59 | + |
| 60 | + ttk.Label(score_frame, text=P1_name, font=("Arial", 12)).grid(row=0, column=0, padx=10) |
| 61 | + ttk.Label(score_frame, textvariable=self.P1Score, font=("Arial", 12)).grid(row=0, column=1) |
| 62 | + |
| 63 | + ttk.Label(score_frame, text=P2_name, font=("Arial", 12)).grid(row=0, column=2, padx=10) |
| 64 | + ttk.Label(score_frame, textvariable=self.P1Score, font=("Arial", 12)).grid(row=0, column=3) |
| 65 | + |
| 66 | + # Input frame |
| 67 | + input_frame = ttk.Frame(self) |
| 68 | + input_frame.pack(pady=20) |
| 69 | + |
| 70 | + # User 1 input |
| 71 | + ttk.Label(input_frame, text=P1_name).grid(row=0, column=0, pady=5) |
| 72 | + self.entry_1 = ttk.Entry(input_frame, width=20) |
| 73 | + self.entry_1.grid(row=1, column=0, padx=10) |
| 74 | + |
| 75 | + self.button_1 = ttk.Button( |
| 76 | + input_frame, text="Submit 1", command=self.on_submit_1 |
| 77 | + ) |
| 78 | + self.button_1.grid(row=2, column=0, pady=5) |
| 79 | + |
| 80 | + # User 2 input |
| 81 | + ttk.Label(input_frame, text=P2_name).grid(row=0, column=1, pady=5) |
| 82 | + self.entry_2 = ttk.Entry(input_frame, width=20) |
| 83 | + self.entry_2.grid(row=1, column=1, padx=10) |
| 84 | + |
| 85 | + self.button_2 = ttk.Button( |
| 86 | + input_frame, text="Submit 2", command=self.on_submit_2 |
| 87 | + ) |
| 88 | + self.button_2.grid(row=2, column=1, pady=5) |
| 89 | + |
| 90 | + # ----------------------------- |
| 91 | + # Timer Logic |
| 92 | + # ----------------------------- |
| 93 | + def update_timer(self): |
| 94 | + if self.time_left > 0: |
| 95 | + self.timer_label.config(text=f"Time: {self.time_left}") |
| 96 | + self.time_left -= 1 |
| 97 | + self.after(1000, self.update_timer) |
| 98 | + else: |
| 99 | + self.timer_label.config(text="Time's Up!") |
| 100 | + |
| 101 | + # ----------------------------- |
| 102 | + # Button Callbacks |
| 103 | + # ----------------------------- |
| 104 | + |
| 105 | + def on_submit_1(self): |
| 106 | + try: |
| 107 | + value = int(self.entry_1.get()) # get and convert input |
| 108 | + print("User 1 entered:", value) |
| 109 | + |
| 110 | + # add entered value to score |
| 111 | + self.P1Score.set(self.P1Score.get() + value) |
| 112 | + |
| 113 | + # clear input box |
| 114 | + self.entry_1.delete(0, tk.END) |
| 115 | + |
| 116 | + except ValueError: |
| 117 | + print("Invalid input: please enter an integer") |
| 118 | + self.entry_1.delete(0, tk.END) |
| 119 | + |
| 120 | + def on_submit_2(self): |
| 121 | + try: |
| 122 | + value = int(self.entry_2.get()) # get and convert input |
| 123 | + print("User 2 entered:", value) |
| 124 | + |
| 125 | + # add entered value to score |
| 126 | + self.P2Score.set(self.P2Score.get() + value) |
| 127 | + |
| 128 | + # clear input box |
| 129 | + self.entry_2.delete(0, tk.END) |
| 130 | + |
| 131 | + except ValueError: |
| 132 | + print("Invalid input: please enter an integer") |
| 133 | + self.entry_2.delete(0, tk.END) |
| 134 | + |
| 135 | + |
| 136 | +# ----------------------------- |
| 137 | +# Run Application |
| 138 | +# ----------------------------- |
| 139 | +if __name__ == "__main__": |
| 140 | + app = App() |
| 141 | + app.mainloop() |
0 commit comments