@
@
@xpestilent2022-03-14 17:44:29
Python
@xpestilent, 2022-03-14 17:44:29

How to optimize application performance?

How to optimize the work of my timer?) When pressed, the timer becomes very dull and moving around the screen is also not quite smooth. Is there any way to fix this?

import tkinter as tk
import time
import winsound

sec = 0
mn = 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:
            lbl.config(text = f'Time left: Done')
            winsound.Beep(400,1000)
            return
        else:
            mn = (nt - ( t - (t // nt))) // 60 
            sec = (nt - ( t - (t // nt))) - mn*60
            lbl.config(text = f'Time left: {mn} min {sec} sec')
            win.update()

win.title('Timer')
win.resizable(width = False, height = False)
win.attributes('-toolwindow', True)
win.attributes("-topmost",True)

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


btn1 = tk.Button(win, text = "start",
                 font=('Arial',12,'bold'),
                 command = kb)


lbl.grid(row = 0, column = 0)
btn1.grid(row = 0, column = 1)




win.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-03-14
_

Instead of looping and win.update(), learn to use win.after().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question