@
@
@xpestilent2022-03-06 21:06:03
Python
@xpestilent, 2022-03-06 21:06:03

How to create two simultaneous loops in Tkinter?

I wanted to make myself a timer that would count down the right time for me when I press the button. Everything works well with one button, but when you press another button, the first one stops. How can I make them work at the same time?

import tkinter as tk
import time


sec = 0
mn = 0
sec2 = 0
mn2 = 0

win = tk.Tk()

def kb():
    global mn
    global sec 
    nt = 240
    for t in range(0,1000):
        time.sleep(1)
        if t > 0 and t % nt == 0:
            print('Done')
            return
        else:
            mn = (nt - ( t - (t // nt))) // 60 
            sec = (nt - ( t - (t // nt))) - mn*60
            print(mn, 'min', sec, 'sec')
            lbl.config(text = f'Time left: {mn} min {sec} sec')
            win.update()

def mail():
    global mn2
    global sec2
    nt = 605
    for t in range(0,1000):
        time.sleep(1)
        if t > 0 and t % nt == 0:
            print('Done')
            return
        else:
            mn2 = (nt - ( t - (t // nt))) // 60 
            sec2 = (nt - ( t - (t // nt))) - mn2*60
            print(mn, 'min', sec, 'sec')
            print(mn2, 'min', sec2, 'sec')
            lbl2.config(text = f'Time left: {mn2} min {sec2} sec')
            win.update()


win.title('Timer')
win.resizable(width = False, height = False)
win.overrideredirect(False)

b1 = tk.Button(win, font=('Arial',12,'bold'),
               text = 'START', command = kb).grid(row = 0,column = 0)

lbl = tk.Label(win, font=('Arial',12,'bold'),
               text = f'Time left: {mn} min {sec} sec ')
lbl.grid(row = 1,column = 0)

b2 = tk.Button(win, font=('Arial',12,'bold'),
               text = 'START', command = mail).grid(row = 0,column = 1)

lbl2 = tk.Label(win, font=('Arial',12,'bold'),
               text = f'Time left: {mn} min {sec} sec ')
lbl2.grid(row = 1,column = 1)


b1.pack()
lbl.pack()
b2.pack()
lbl2.pack()

win.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kvason, 2022-03-06
@Kvason

Multithreading https://thecode.media/python-time-2/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question