+ 1

hello someone can help

I built stop watch app from tutorials and i write there "time.sleep(0.7)" and because of that whole application is lagging. import time from tkinter import * from tkinter.ttk import * from threading import Thread sc = 0 mn = 0 hr = 0 stp = 0 def start(): global sc, mn, hr time.sleep(0.7) sc = sc + 1 if sc == 60: mn = mn + 1 sc = 0 if mn == 60: hr = hr + 1 mn = 0 if stp == 0: lbl = Label(root, text='%i:%i:%i' % (hr, mn, sc), font=('arial', 30, 'bold'), foreground='white', background="black", width=10) lbl.after(300, start) lbl.place(x=200, y=60) def stop(): global stp stp = 1 def res(): global sc, mn, hr, stp sc = 0 mn = 0 hr = 0 stp = 0 root = Tk() style = Style() root.title("stopwatch") root.geometry("500x500") root.resizable(False, False) root.configure(bg="black") style.configure('Tbutton', font=('arial', 10, 'bold'), borderwidth='5') button1 = Button(root, text="start", command=start).place(x=10, y=10) button2 = Button(root, text="stop", command=stop).place(x=410, y=10) button3 = Button(root, text="reset", command=res).place(x=220, y=10) root.mainloop()

4th Mar 2021, 11:39 AM
Michal
Michal - avatar
9 ответов
+ 2
I'm not an expert on this, but since I'm the first one to answer, I hope it's better than nothing. As far as I can see the application is lagging because time.sleep() "pauses" your entire script and waits for the given time to pass. A good way to work around this is to use multithreading (which you already imported but never used). It allows you to run multiple tasks simultaneously. I would advice you to read into the basics of multithreading and you should be able to implement it for your application. Here is an example source but you can also just google for it. www.geeksforgeeks.org/multithreading-JUMP_LINK__&&__python__&&__JUMP_LINK-set-1/
4th Mar 2021, 12:05 PM
flo
flo - avatar
+ 1
you may be experiencing lag because your system is detecting that the window is not responding, and is calling up emergency procedures during the delay. It’s just a theory. To make this app work flawlessly, I would recommend using threading.
4th Mar 2021, 3:24 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Tkinter is really slow to use in general, from my experience just programming 4 procedures so when each button is pressed the background changes to that colour. The result is a program that is 40 lines, takes at least 6 seconds to load and by that point most Web users would of left the page. Prehaps consider importing a module that might already have built-in stopwatch functions or research other python GUI options.
6th Mar 2021, 12:47 AM
Gemma W
Gemma W - avatar
0
there is a web version of Tkinter? I have never heard of such.
6th Mar 2021, 12:48 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Not to my knowledge I don't think so, have you considered using visual basic. Net Each project can be exported into an. Exe file and the user form is built on drag and drop so you can choose exactly how you want the program to look.
6th Mar 2021, 12:50 AM
Gemma W
Gemma W - avatar
0
With tkinter the user form is so limited and for the program to run python must be install on the target machine whereas vb. Net can run as long as the executionable file is downloaded
6th Mar 2021, 12:53 AM
Gemma W
Gemma W - avatar
0
the day i trade tkinter (which always has worked for me) for something provided by visual studio (which had like three download-installers in a row each longer than the last, and then for all that work it was on a hidden 30 day trial period and wouldn’t uninstall unless they re-updated the gigantic installer system) is there a day I think will never occur. Ever.
6th Mar 2021, 1:08 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Visual studio is different
6th Mar 2021, 1:10 AM
Gemma W
Gemma W - avatar
0
 TK should have all you need for a stopwatch GUI, and as far as I know, a fancier IDE like visual studio wont provide a GUI system for python automatically anyway.
6th Mar 2021, 1:10 AM
Wilbur Jaywright
Wilbur Jaywright - avatar