-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_window.py
151 lines (118 loc) · 4.83 KB
/
main_window.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.uic import loadUi
from PyQt6.QtCore import QTimer, QUrl
from PyQt6.QtMultimedia import QSoundEffect
from PyQt6.QtGui import QPixmap
import time
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("FIA2_Design.ui", self)
# Connect buttons to their respective functions
self.resetScoresButton.clicked.connect(self.reset_scores)
self.resetTimerButton.clicked.connect(self.reset_timer)
self.resetAllButton.clicked.connect(self.reset_all)
self.team1Add1Button.clicked.connect(lambda: self.change_score(1, 1))
self.team1Add2Button.clicked.connect(lambda: self.change_score(1, 2))
self.team1SubtractButton.clicked.connect(lambda: self.change_score(1, -1))
self.team2Add1Button.clicked.connect(lambda: self.change_score(2, 1))
self.team2Add2Button.clicked.connect(lambda: self.change_score(2, 2))
self.team2SubtractButton.clicked.connect(lambda: self.change_score(2, -1))
self.addMinuteButton.clicked.connect(self.add_minute)
self.addSecondButton.clicked.connect(self.add_second)
self.startButton.clicked.connect(self.start_clock)
self.pauseButton.clicked.connect(self.pause_clock)
self.presetsComboBox.currentIndexChanged.connect(self.select_preset)
# Initialize variables
self.team1Score = 0
self.team2Score = 0
self.clock = QTimer()
self.clock.timeout.connect(self.change_time)
self.timeInSeconds = 0
self.presets = []
def reset_scores(self):
self.team1Score = 0
self.team2Score = 0
self.team1ScoreLabel.display(self.team1Score)
self.team2ScoreLabel.display(self.team2Score)
def reset_timer(self):
self.timeInSeconds = 0
self.update_time()
def reset_all(self):
self.create_preset()
self.reset_scores()
self.reset_timer()
def start_clock(self):
self.clock.stop()
self._start_clock_with_sound()
def _start_clock_with_sound(self):
# Play a sound every seccond before starting
for i in range(1, 4):
QTimer.singleShot(i * 1000, self._play_tick_sound)
print(i)
#play a different sound when the timer starts
QTimer.singleShot(4000, self. _play_start_sound)
#Start the timer
QTimer.singleShot(4000, lambda:self.clock.start(1000))
def _play_tick_sound(self):
tick_sound = QSoundEffect()
tick_sound.setSource(QUrl.fromLocalFile("Beep.wav"))
tick_sound.play()
def _play_start_sound(self):
start_sound = QSoundEffect()
start_sound.setSource(QUrl.fromLocalFile("Buzzer.wav"))
start_sound.play()
def change_score(self, team, points):
if team == 1:
self.team1Score += points
self.team1ScoreLabel.display(self.team1Score)
elif team == 2:
self.team2Score += points
self.team2ScoreLabel.display(self.team2Score)
def change_time(self):
self.timeInSeconds -=1
self.update_time()
def update_time(self):
minutes = self.timeInSeconds // 60
seconds = self.timeInSeconds % 60
self.clockLabel.display(f"{minutes:02d}:{seconds:02d}")
def add_minute(self):
self.timeInSeconds += 60
self.update_time()
def add_second(self):
self.timeInSeconds += 1
self.update_time()
def start_clock2(self):
self.clock.start(1000) # Update every second
def pause_clock(self):
self.clock.stop()
# --------- Presets --------- #
def create_preset(self):
preset = {}
preset["Team1Name"] = self.team1name.toPlainText()
preset["Team2Name"] = self.Team2Name.toPlainText()
preset["timeInSeconds"] = self.timeInSeconds
self.presets.append(preset)
self.update_presets_dropdown()
def update_presets_dropdown(self):
self.presetsComboBox.clear()
for preset in self.presets:
time_in_seconds = preset['timeInSeconds']
self.presetsComboBox.addItem(f" Time: {time_in_seconds}s")
def select_preset(self, index):
if index >= 0 and index < len(self.presets):
preset = self.presets[index]
self.team1name.setText(preset["Team1Name"])
self.Team2Name.setText(preset["Team2Name"])
self.update_score_labels()
self.timeInSeconds = preset['timeInSeconds']
self.update_time()
def update_score_labels(self):
self.team1ScoreLabel.display(self.team1Score)
self.team2ScoreLabel.display(self.team2Score)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())