tap/tap.py

59 lines
1.6 KiB
Python
Raw Normal View History

2023-12-20 02:30:29 +00:00
import tkinter as tk
import time
click_times = [] # to store click times
bpm = 0 # to store bpm
running = False # to store if the widget is running
# Function to calculate the BPM
def calculate_bpm():
global click_times, bpm, running
if len(click_times) >= 2:
#calculate difference between first and last click
time_diff = click_times[-1] - click_times[-2]
#calculate bpm based on time difference
2024-02-18 03:36:39 +00:00
if time_diff > 0:
bpm = int(60 / time_diff)
bpm_label.config(text=f"BPM: {bpm:.2f}")
else:
bpm_label.config(text="BPM: Max")
2023-12-20 02:30:29 +00:00
else:
bpm_label.config(text="BPM: N/A")
# Function to handle mouse clicks or space bar taps
def handle_click(event):
global click_times, running
if running:
click_times.append(time.time())
calculate_bpm()
def start_stops():
global running, click_times, bpm
if running:
running = False
click_times = []
bpm = 0
bpm_label.config(text="BPM: N/A")
start_stop_button.config(text="Start")
else:
running = True
start_stop_button.config(text="Stop")
root = tk.Tk()
root.title("BPM Calculator")
root.geometry("300x200")
# Create and place bpm label
2024-07-23 23:27:32 +01:00
bpm_label = tk.Label(root, text="BPM: N/A", font=("Verdana", 20))
2023-12-20 02:30:29 +00:00
bpm_label.pack(pady=20)
# Create and place start/stop button
2024-02-18 03:36:39 +00:00
start_stop_button = tk.Button(root, text="Start", font=("Verdana", 18), command=start_stops)
2023-12-20 02:30:29 +00:00
start_stop_button.pack(pady=20)
2024-02-18 03:36:39 +00:00
root.bind("<Button>", handle_click)
root.bind("<Key>", handle_click)
2023-12-20 02:30:29 +00:00
2024-07-22 17:31:09 +01:00
root.mainloop()