Skip to content

Commit d8abac6

Browse files
committed
Adding toggle between Start/Stop button in keystroke.py file
1 parent 39279a3 commit d8abac6

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

applications/keystroke.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def start_detection(self):
5757
# Detect blinks in the filtered signal
5858
self.detect_blinks(filtered_eog)
5959

60+
def quit_detection(self):
61+
# Quit the detection loop
62+
print("Quitting peak detection...")
63+
self.running = False
64+
6065
def stop_detection(self):
6166
# Stop the detection loop
6267
print("Stopping peak detection...")
@@ -102,10 +107,10 @@ def update_button_color(self):
102107
time.sleep(0.1)
103108
self.blink_button.config(bg="SystemButtonFace")
104109

105-
def stop_action(detector):
110+
def quit_action(detector):
106111
# Action for the Quit button
107112
print("Quit button pressed. Exiting program.")
108-
detector.stop_detection()
113+
detector.quit_detection()
109114
exit()
110115

111116
def keystroke_action():
@@ -119,6 +124,25 @@ def start_action(detector):
119124
detection_thread = threading.Thread(target=detector.start_detection, daemon=True)
120125
detection_thread.start()
121126

127+
def stop_action(detector):
128+
# Action for the Start button
129+
print("Stop button pressed. Stopping the program.")
130+
detection_thread = threading.Thread(target=detector.stop_detection, daemon=True)
131+
detection_thread.stop()
132+
133+
def toggle_action(detector, start_button):
134+
# Toggle between Start and Stop
135+
if start_button.cget("text") == "Start":
136+
print("Start button pressed. Starting the program.")
137+
start_button.config(text="Stop")
138+
detector.running = True
139+
detection_thread = threading.Thread(target=detector.start_detection, daemon=True)
140+
detection_thread.start()
141+
else:
142+
print("Stop button pressed. Stopping the program.")
143+
start_button.config(text="Start")
144+
detector.stop_detection()
145+
122146
def create_popup():
123147
# Create the main GUI window
124148
popup = tk.Tk()
@@ -134,20 +158,20 @@ def create_popup():
134158
blink_button = tk.Button(horizontal_frame, text="Blink Detected", width=12)
135159
blink_button.pack(side=tk.LEFT, padx=10)
136160

137-
# Add Start button
161+
# Add Start/Stop button
138162
start_button = tk.Button(horizontal_frame, text="Start", width=8)
139163
start_button.pack(side=tk.LEFT, padx=10)
140164

141165
# Add Quit button
142-
stop_button = tk.Button(horizontal_frame, text="Quit", width=8)
143-
stop_button.pack(side=tk.LEFT, padx=10)
166+
quit_button = tk.Button(horizontal_frame, text="Quit", width=8)
167+
quit_button.pack(side=tk.LEFT, padx=10)
144168

145169
# Initialize the EOG peak detector
146170
detector = EOGPeakDetector(blink_button, keystroke_action)
147171

148-
# Link Start and Stop buttons to their respective actions
149-
start_button.config(command=lambda: start_action(detector))
150-
stop_button.config(command=lambda: stop_action(detector))
172+
# Link Start/Stop button and Quit button to their respective actions
173+
start_button.config(command=lambda: toggle_action(detector, start_button))
174+
quit_button.config(command=lambda: quit_action(detector))
151175

152176
popup.mainloop()
153177

0 commit comments

Comments
 (0)