import tkinter as tk
from tkinter.font import Font
from threading import RLock, Thread
import time
data = {}
max_h = 0
def counter():
global data
global max_h
hour_time = time.localtime().tm_hour
if hour_time in data:
data[hour_time] += 1
max_element = max(data.values())
if max_element > max_h:
max_h = max_element
log_max.configure(text=str(max_h), foreground='Blue')
else:
data[hour_time] = 0
if len(data) > 3:
del_obj=list(data)[0]
del data[del_obj]
result = ' '.join([str(key)+':00 [ '+str(value)+' ]' for key, value in data.items()])
log_mesage.configure(text=result)
def mode_run(action):
global mode
mode = action
if mode:
Thread(target=manager, daemon=True).start()
stop.pack()
start.pack_forget()
else:
start.pack()
stop.pack_forget()
def manager():
while mode:
threads = []
for _id in range(int(value.get())):
threads.append(Thread(target=counter,))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
time.sleep(1)
window = tk.Tk()
window.title("Test")
window.geometry('500x250')
step = tk.IntVar()
step.set(150)
value = tk.Spinbox(window, from_=0, to=1000, increment=10, width=5,
font=Font(family='Helvetica', size=20), textvariable=step)
log_max = tk.Label(window, text=str(max_h), foreground='Blue', font='Times 15', padx=10, pady=10, height=3)
log_mesage = tk.Label(window, text=str(time.localtime().tm_hour)+':00 [ 0 ]', font='Times 15', padx=10, pady=10, height=1)
start = tk.Button(window, text="Start", command=lambda: mode_run(True), width=35,
bg="green",
fg="white")
stop = tk.Button(window, text="Stop", command=lambda: mode_run(False), width=35,
bg="orange",
fg="white")
value.pack()
log_max.pack()
log_mesage.pack()
start.pack()
window.mainloop()