Threading With Tkinter - Python Tkinter GUI Tutorial #97

  Рет қаралды 77,420

Codemy.com

Codemy.com

Күн бұрын

In this video I'll talk about threading. What happens when your program hangs or lags because some function is taking too long to run? Threading solves that very easily!
Asynchronous threading is something people ask me about all the time. With Tkinter, it's actually incredibly easy to use threading. We'll just import threading and then start a thread whenever we run a function that we know will stall our program.

Пікірлер: 406
@Codemycom
@Codemycom 4 жыл бұрын
▶️ Watch Entire Tkinter Playlist ✅ Subscribe To My KZbin Channel: bit.ly/2UFLKgj bit.ly/2IGzvOR ▶️ See More At: ✅ Join My Facebook Group: Codemy.com bit.ly/2GFmOBz ▶️ Learn to Code at Codemy.com ✅ Buy a Codemy T-Shirt! Take $30 off with coupon code: youtube1 bit.ly/2VC9WUN ▶️ Get The Code bit.ly/3fLFQ8p
@codewithahmed9806
@codewithahmed9806 4 жыл бұрын
Nice Video
@Planet_Xplorer
@Planet_Xplorer 3 жыл бұрын
Greetings from your Egyptian fans! I think it will be a good idea if you post the code of the video somewhere so that people can easily try it for themselves. Thanks ☺️
@Codemycom
@Codemycom 3 жыл бұрын
@@Planet_Xplorer Of course I do that. The link is in the pinned comment under each video. The pinned comment you just posted under lol
@Planet_Xplorer
@Planet_Xplorer 3 жыл бұрын
@@Codemycom My bad! lol Thanks dude and keep the good work!
@peterpacsay8358
@peterpacsay8358 3 жыл бұрын
You have great tutorials! In my humble opinion If you do not want to start 5 second counting automatically I think you need: my_button1 = Button(root,text = '5 seconds',command= lambda: threading.Thread(target=five_seconds).start()).
@jordanstein892
@jordanstein892 3 жыл бұрын
I can't tell you how much I appreciate this... I went to the comment section specifically to find someone talking about this.
@peterpacsay8358
@peterpacsay8358 3 жыл бұрын
@@jordanstein892 You can find another solution in the the next video: Tutorial#98
@emanuelojea6412
@emanuelojea6412 2 жыл бұрын
@@jordanstein892 totally agree!!! thx so much @Peter Pacsay
@adrianojose7172
@adrianojose7172 2 жыл бұрын
@@jordanstein892 hi there, can you please tell me why y'all want to use lambda? I'm new to this so. Thank you
@linasmariusnasvytis7600
@linasmariusnasvytis7600 2 жыл бұрын
Legend!
@evanlavizadeh4032
@evanlavizadeh4032 3 жыл бұрын
I think it's better to use command=(threading.Thread(target=five_seconds).start) instead of command=(threading.Thread(target=five_seconds).start()) because the latter will run five_seconds in this very line while the former will not. Most cases, you don't want the command to start until the button is pressed. edit: Also this doesn't work when you try to press the "Wait 5 sec" button twice. You will get: RuntimeError: threads can only be started once. It feels almost as though you hid that fact by only demoing the functionality by closing and re-starting the program instead of clicking the button multiple times? Feels like an unfinished tutorial.
@rockmero373
@rockmero373 3 жыл бұрын
I was stuck with this issue. You saved me. Thanks!
@evanlavizadeh4032
@evanlavizadeh4032 3 жыл бұрын
@@rockmero373 you're welcome! I was stuck with it too haha
@abhinavs4875
@abhinavs4875 Жыл бұрын
yeaa soo do you know how to make my button function continuously without getting the runtime error??
@ashyy6305
@ashyy6305 Жыл бұрын
You're the best, I've been stuck with this issue for days D:
@evanlavizadeh4032
@evanlavizadeh4032 Жыл бұрын
@@abhinavs4875 I did fix it but it was too long ago to remember sorry.
@holycow4889
@holycow4889 3 жыл бұрын
Man, i literally find everything i'm looking for on your channel, thank your very much for this video.
@Codemycom
@Codemycom 3 жыл бұрын
hey awesome!
@danilodelucio
@danilodelucio 3 жыл бұрын
You are amazing, man, thank you so much! If someone is having a problem where the function "start" starts before, just put the lambda before threading. This is my situation: enter_button = Button(frameRadio, text="ENTER", bd=0, bg="#005C7A", fg="white", font=(font_type, 10, "normal"), command=lambda: threading.Thread(target=enterButton).start())
@Codemycom
@Codemycom 3 жыл бұрын
Thanks!
@Planet_Xplorer
@Planet_Xplorer 3 жыл бұрын
That's a life saver dude!! Thanks for the suggestion :-)
@dataclips
@dataclips 3 жыл бұрын
Thanks Danilo!
@mikewyrick8715
@mikewyrick8715 3 жыл бұрын
wish I would have read this sooner! lol
@frankrogowski9595
@frankrogowski9595 11 ай бұрын
Wow this is excellent! I am developing a FinTech payment processing application and I am at the point where threading is very necessary. So simple. Great teacher-instructor. Thank you! Going to to apply this to my notebook/tabs where my application is split out logically and may hang the rest of the program if a single threaded. Love these series of Tkinter. Wish I would have found these a 1yr ago. I am now a Tkinter junkie.
@Codemycom
@Codemycom 11 ай бұрын
Awesome, happy to hear it!
@eddw123
@eddw123 2 жыл бұрын
I think he didn’t notice ! the 5 seconds button was automatically pressed by the >> .start() >>command=lambda: threading.Thread(target=five_seconds).start())
@raoulalfonze2192
@raoulalfonze2192 3 жыл бұрын
Incredibly easy way to get this done! I've been looking for a way to get a tkinter GUI to stop freezing on long tasks, and this does it perfectly! Thank you!
@parthchopra315
@parthchopra315 2 жыл бұрын
did it worked dude? coz my GUI crashes after every 2-3 hours running extrnsively. HELP BROOO
@joelmanning249
@joelmanning249 3 жыл бұрын
Don't know if you encountered this rather peculiar feature but: When wrapping my button event to my thread I noticed that: target=("function").start() will cause the event to run on the start up on the program However if I change it to the following: target=("function").start then the event will now run on the button click
@selimahmedov8591
@selimahmedov8591 3 жыл бұрын
Had the same issue - on start-up the function would start running without pressing the button. Removing the () from start solved the issue. Thanks a lot.
@VascoCC95
@VascoCC95 2 жыл бұрын
He himself explains that on one of the first episodes of this series... 😅
@nicolaspoyet3548
@nicolaspoyet3548 2 жыл бұрын
Thanks !
@64bitMario
@64bitMario 8 күн бұрын
Great guide! Now I'll never forget to give a widget pady=20 to push it down a little bit.
@Codemycom
@Codemycom 5 күн бұрын
ha! nice
@fernandocorenstein6531
@fernandocorenstein6531 3 жыл бұрын
Best channel for python on KZbin. Why aren’t people subscribing to this lovely channel?
@Codemycom
@Codemycom 3 жыл бұрын
tell your friends ;-)
@darshanpanchal3046
@darshanpanchal3046 4 жыл бұрын
You are great sir....you are awesome........thank you thank you so much......i was just stuck there for about 2 weeks.....love you sir.......
@Codemycom
@Codemycom 4 жыл бұрын
Thanks!
@RyanDanielG
@RyanDanielG 3 жыл бұрын
LOL the last few days i have been writing a program with a while loop in it and it caused me ALL KINDS of problems. In the end i restructured the entire thing so I didn't need the loop... I was only 3 vids away from this video in the playlist ;| Wish I had watched this first. Woulda saved me about 12 hours. Google sent me to StackO the posts I saw mentioning threading mostly said 'dont do it, its complicated, work around it'. Great tool and now I understand it. Thanks!!
@Codemycom
@Codemycom 3 жыл бұрын
Ha, awesome
@roni5245
@roni5245 3 жыл бұрын
Hi! I have a question. I'm making a game using the turtle module which I know was created with tkinter. I'm trying to use a thread in order to be able to do two things simultaneously. But, I'm getting the following error: RuntimeError: main thread is not in main loop. Do you know how to fix this by any chance?
@andrejvujic
@andrejvujic 4 жыл бұрын
That iconic: and we'll give this a pady of 20 just to push it down a little
@Codemycom
@Codemycom 4 жыл бұрын
ha!
@insertx2k_dev
@insertx2k_dev 2 жыл бұрын
Thank you for your time to help us small developers improve our software, you are a real hero.
@Codemycom
@Codemycom 2 жыл бұрын
Thanks :-)
@uninho1985
@uninho1985 4 жыл бұрын
Omg I was searching so many forums for this solution.... thanks thank thanks
@Codemycom
@Codemycom 4 жыл бұрын
Glad I could help!
@uninho1985
@uninho1985 4 жыл бұрын
@@Codemycom I have one question please. I created a tool that uses selenium for web automation. I have created threads for each button that calls a function (long running loops). I have e. g. a Start and a Pause button. The pause button should break the loop. To get this, I have created a bol (start_process = True/False). Pressing Pause button will change start_process to False and then the tool stops the loop. All good so far, that works great. But the threads keep running background once stared and that leads to an error, because selenium tries to find a button which is currently not available after I pressed Pause. Basically, you can imagine like that, when I start the process, my tool will keep clicking and waiting for some XPATH's. When I press pause, the tool will jump put of the mask and switch to another window. So Selenium of course cant find the XPATHs anymore.>> error. Is there a way, that I can like terminate the main thread after I pressed pause? I know there is this "daemon" thing, but cant get it running since I dont know where and how to implement it. Would be great if you would have a solution for my problem on hand. I love you videos! I really learned a lot as a hobby newbie coder ;-)
@DanielSilva-fv8br
@DanielSilva-fv8br 4 жыл бұрын
@@uninho1985 I have a similar problem. Did you find out how to kill the thread?
@uninho1985
@uninho1985 4 жыл бұрын
@@DanielSilva-fv8br Yes, I have tried a round and found that creating a thread for each button that calls a function was the key. Then I have created booleans like bol_start = True which gets changed to bol_start = False after pressing the pause button. It will finish the loop that was started already, and stops after that. In my personal case, since I did not want to wait for the loop to get finished, I also created an "idle" function that interrupts the running loop by clicking on a different navigation button on the web app.
@u2gilles
@u2gilles 2 жыл бұрын
This technique works only if TKinter is thread-safe as you modify a label in the second thread. Can you confirm that it is ? because I read conflicting information about it.
@michaeltennyson7564
@michaeltennyson7564 2 жыл бұрын
your videos and website have helped me learn so much. thanks john!
@Codemycom
@Codemycom 2 жыл бұрын
Very happy to hear it!
@nebular-nerd
@nebular-nerd Жыл бұрын
A super simple explanation that works, top stuff!
@Codemycom
@Codemycom Жыл бұрын
Thanks!
@captainmacro5904
@captainmacro5904 2 жыл бұрын
The best video about treading i ever seen
@Codemycom
@Codemycom 2 жыл бұрын
Happy to hear it!
@ruioliveira4149
@ruioliveira4149 3 жыл бұрын
You don´t know how much you saved my life with this. Thank you 100000000 times, really really thanks :)
@Codemycom
@Codemycom 3 жыл бұрын
Glad it helped you!
@bobzarrabi0024
@bobzarrabi0024 4 жыл бұрын
Great videos. Thank you. I just replicated your code but in my case, the function starts automatically before I click the Button. what am I missing !! start = Button(frame, text="Start", command=threading.Thread(target=m.main).start())
@bobzarrabi0024
@bobzarrabi0024 4 жыл бұрын
Actually I found the answer, just going to post it if anyone else encounters the same issue: stackoverflow.com/questions/49085244/tkinter-button-command-getting-executed-before-clicking-the-button
@Codemycom
@Codemycom 4 жыл бұрын
yep
@simonebollati5285
@simonebollati5285 4 жыл бұрын
@@bobzarrabi0024 Thx man !!!
@samdan87
@samdan87 3 жыл бұрын
@@bobzarrabi0024 THANK YOU
@danilodelucio
@danilodelucio 3 жыл бұрын
Thank you so much!
@elielberra2867
@elielberra2867 2 жыл бұрын
Thank you very much for all the tutorials on Tkinter you have provided, they were very useful!!
@Codemycom
@Codemycom 2 жыл бұрын
Very welcome!
@maksimantonio4732
@maksimantonio4732 4 жыл бұрын
Great tutorial ! One question: are you sure its thread safety, i mean if you put lots widget configure in thread function, will it still lead to main window stuck ?
@elysonpanolino7522
@elysonpanolino7522 2 жыл бұрын
My screen is now broken because I smashed the like button
@Codemycom
@Codemycom 2 жыл бұрын
Awesome!
@tpz1474
@tpz1474 4 жыл бұрын
very nice tutorial, easy to follow, direct & to the point! thank you very much for this.
@Codemycom
@Codemycom 4 жыл бұрын
Glad it was helpful!
@leomena7679
@leomena7679 3 жыл бұрын
my thread is somehow starting by itself, Once ir runs the first time (right after its drawiing) it will not execute again, regardless of whether you click the button.. Any thoughts why?
@Codemycom
@Codemycom 3 жыл бұрын
Not sure...it seems you've done something wrong...
@Victor_Marius
@Victor_Marius 3 жыл бұрын
@@Codemycom the example is wrong. When passing the command argument the thread is stated because it is calling the .start() method. You either omit the () to pass in the .start method and not the return of .start(), or you use lambda function, command = lambda: threading.Thread(target=...)
@100percentTrading
@100percentTrading 2 жыл бұрын
Awesome. This is exactly what I was searching for. You've saved my day.
@Codemycom
@Codemycom 2 жыл бұрын
Happy to hear it!
@SaxeZz1
@SaxeZz1 4 жыл бұрын
Hello, i have a question. Do you know, by any chance, a way to stop/kill/terminate a Thread just created in the command variant? I want to start a calculation by button but also want to be able to terminate the calculation while the calculation. Unfortunately, i also can not restart it as you do in the video. For example, the Thread is done and I click "start" again it won't start and says RuntimeError: threads can only be started once. Does anyone know what to do?
@amritsingh978
@amritsingh978 4 жыл бұрын
bro he dont expalined it wisely ,i am very dissapointed . you can use multiprocessing instead
@venkatvenky0079
@venkatvenky0079 3 жыл бұрын
Same problem here. If someone know, pls explain the solution
@myatthurawin2276
@myatthurawin2276 3 жыл бұрын
same problem here please tell me if you find solution
@myatthurawin2276
@myatthurawin2276 3 жыл бұрын
@@venkatvenky0079 same problem here please tell me if you find solution
@venkatvenky0079
@venkatvenky0079 3 жыл бұрын
@@myatthurawin2276 use lamda command in button command. It helps for me
@RyanDanielG
@RyanDanielG 3 жыл бұрын
Smash that like button guys n gals! its free! great python tkinter tutorial, Codemy. Thanks
@myatthurawin2276
@myatthurawin2276 3 жыл бұрын
Very Thanks. I have been searching for the whole night!!!!!
@Codemycom
@Codemycom 3 жыл бұрын
Very cool!
@aldopereira3649
@aldopereira3649 4 жыл бұрын
when I start my tkinter program all functions are activated with this metod :C Help
@aldopereira3649
@aldopereira3649 4 жыл бұрын
command=lambda:threading.Thread(target=five_seconds).start() #add lambda before to threading to all function
@PolosPhotography
@PolosPhotography 3 жыл бұрын
THANKS
@rodrigoportillo2747
@rodrigoportillo2747 3 жыл бұрын
Thank you worked for me!
@mvpetrone
@mvpetrone 3 жыл бұрын
ratio'd lol
@nero_ap6093
@nero_ap6093 3 жыл бұрын
@@aldopereira3649 Thank you very much! 💜
@tamasvida6454
@tamasvida6454 Жыл бұрын
many thanks dude FINALLY i was able to fix my code after 2 month of trying D:
@Codemycom
@Codemycom Жыл бұрын
Happy to hear it!
@spugged9800
@spugged9800 2 жыл бұрын
I needed this so bad, I was making a game timer for when the mobs respawn and this was killing me!
@savinseneviratne3266
@savinseneviratne3266 2 жыл бұрын
sir, if i may ask, lets say that there is a button in tkinter that run a threaded function, and once the function is over, i want to press the button again and re run the same function without freezing the gui. how can i do that?...
@sammorganmoore
@sammorganmoore 2 жыл бұрын
great video, I want something running on a thread that just autostarts is not activated by a button (example flashing button red 5s, blue 5seconds) that starts flashing when the window is opened and continues flashing while one does other stuff?
@rehmankhan-ve9vo
@rehmankhan-ve9vo 3 жыл бұрын
Thanks a lot sir , You have saved my time . I was searching for this solution and you solved this . Thanks a lot ❤️❤️❤️❤️❤️
@Codemycom
@Codemycom 3 жыл бұрын
Glad you liked it!
@Dhruv_Jadhav
@Dhruv_Jadhav 4 жыл бұрын
Good job on the video! I have a question though. So I started a thread to take care of a function that was freezing my program like crazy. So the function starts when I press a button but that particular function starts before I click on the button. Is this an error or what should I do to fix it? Thanks!
@Codemycom
@Codemycom 4 жыл бұрын
Why does the function start before you click the button? You should set up your threading to start when the function starts....
@Dhruv_Jadhav
@Dhruv_Jadhav 4 жыл бұрын
Lemme check my code
@Codemycom
@Codemycom 4 жыл бұрын
​@@rgsoussama try leaving off the () from .start()
@Dhruv_Jadhav
@Dhruv_Jadhav 4 жыл бұрын
@@Codemycom It worked!!! Thanks.
@Codemycom
@Codemycom 4 жыл бұрын
@@Dhruv_Jadhav Glad to hear it!
@DanielSilva-fv8br
@DanielSilva-fv8br 4 жыл бұрын
Hello, can someone tell me how to kill a thread? I'm using thread for a function with a while loop and it works but i don't know how to kill the thread when i close the program.
@michaelfuchsluger6343
@michaelfuchsluger6343 3 жыл бұрын
I think Threads are unkillable. They end when they end. You have to kill the process where the thread is running in.
@richardspahn1320
@richardspahn1320 2 жыл бұрын
This question has been asked several times. Let's encourage John @Codemy to PLEASE do a follow up video addressing how to kill a thread.
@mobinwb
@mobinwb 2 жыл бұрын
I wanted to run FuncAnimation along with another function in a thread. but cant pull off. Stackoverflow couldnt solve it. Any idea, how can I do this?
@xreed8
@xreed8 8 ай бұрын
You could definitely use time.sleep() in a pyqt/tkinter/etc program. For example, typically web scraping projects will, at some point no matter what you do, require you to use time.sleep() in order to accommodate unexpected connection issues, HTML DOM lag, network lag, etc.
@Codemycom
@Codemycom 8 ай бұрын
Sure...but this is not a pyqt video...?
@xreed8
@xreed8 8 ай бұрын
@@Codemycom Meant for General GUI frameworks. Edited my comment😆
@chr_isso
@chr_isso 3 жыл бұрын
Why are there so many complaints on the net that the mainloop freezes? I can't adapt this simple piece of code because my mainloop freezes aswell. I can't do a single thing once I start my thread. I think tkinter is a mess ... *frustrated*
@adrisdrsglt6164
@adrisdrsglt6164 4 жыл бұрын
Okay guys if you have a problem when using start() and in the next video he said that you can remove the "()" and just type func.start and it is supposed to work but it still doesn't than check if your function involves global variables
@elmerv1934
@elmerv1934 3 жыл бұрын
en mi caso sin necesidad de apretar el botón se ejecutaba el bucle five_seconds(), y al quitar los paréntesis todo funciona normal pero solo puedo ejecutarlo solo una vez sino la consola me vota error
@jugen123123
@jugen123123 2 жыл бұрын
How could I restart or stop the origninal threading while I click the "5 seconds" button again ?
@julicojotase
@julicojotase Жыл бұрын
Thank you so much i had a headache with opencv, trying to get still the camera frame
@ztech3401
@ztech3401 Жыл бұрын
hey can you solve this problem please RuntimeError: There is no current event loop in thread 'Thread-1'.
@shanril
@shanril 3 жыл бұрын
How to effectively use multiprocessing with Tkinter?
@jaipreetsingh304
@jaipreetsingh304 Жыл бұрын
Thank you for this video, but what if I want to send parameters like root(which is tkinter object) into five_seconds(root). Will it behave the same, I tried to do so but it is not running together. I tried args but it is not able to send root object to other function. Kindly Help
@icodeint2780
@icodeint2780 3 жыл бұрын
am getting an error that i can't get fixed, threading as no attribute 'Thread' due to circular import,
@leedaluyo
@leedaluyo 3 жыл бұрын
same here
@icodeint2780
@icodeint2780 3 жыл бұрын
Well fix it, make sure you have no variable name threading or anything similar, no a .py named threading.
@lamnguyentrong275
@lamnguyentrong275 3 жыл бұрын
does the thread end after the function has finished. ? normally i see some people has to use threading.join() to finish.
@gajendrasonare
@gajendrasonare 3 жыл бұрын
I used threading for Tkinter but the problem is matplotlib is thread incompatible. What should I use now instead of threads so that I can work with matplotlib without freezing the GUI?
@elentu1269
@elentu1269 3 жыл бұрын
Can be used asyncIO (asynchronous programming) to solve the freezing?
@abhinavmane60
@abhinavmane60 2 жыл бұрын
I have a big code fucntion with some loops in it so my tkinter window just hangs for a bit is there a different approach for this problem please respond it will be very helpful of you to respond.
@yuemeng8557
@yuemeng8557 2 жыл бұрын
你好Hello!Thank you so much for the video tutorial. I wrote a while loop in the thread to refresh the control.I want to use another button to terminate the thread or exit the thread.What should I do?
@filipemarques4091
@filipemarques4091 Жыл бұрын
How can i use threading to mix song 1 and song 2, decreasing the volume of song 1 in the end and increasing the volume of song 2 in the begining?
@BWNL_BILL
@BWNL_BILL Жыл бұрын
i am getting a problem with my code whenever i run my code it will immediately run the thread freezing my code until its done waiting
@rgsoussama
@rgsoussama 4 жыл бұрын
Thanks you ! that helps a lot But idk why the function starts when i run my program (i don't even click on the button), and the button won't do anything when i press it after that
@rgsoussama
@rgsoussama 4 жыл бұрын
Solution: threading.Thread(target=myfunc).start instead of command=threading.Thread(target=myfunc).start() .
@Codemycom
@Codemycom 4 жыл бұрын
Interesting!
@ntrasla
@ntrasla 4 жыл бұрын
@@rgsoussama thanks
@lloydthomas8962
@lloydthomas8962 3 жыл бұрын
this worked for me too, maybe its a python 2.7 thing? welcome to the stone age!!
@venkatvenky0079
@venkatvenky0079 3 жыл бұрын
I used command=threading.Thread(target=function).start But once i click the button it works.after the run finished,, if i click the same button it is not doing anything. Im using python 2.7.
@benitez9rh
@benitez9rh 3 жыл бұрын
even with threading, I have a function that is taking a lot longer in tkinter than it does just running a python script off of Spyder. Any idea why that is?
@ncclasses7471
@ncclasses7471 3 жыл бұрын
I am trying this for a button having function which is using lambda and entry both... And when I use threading for this it doesn't work
@step_freeskills101
@step_freeskills101 2 жыл бұрын
Hi John as always nice video...by the way got a question, what is the difference between threading and after method?
@kingsleyjide4486
@kingsleyjide4486 7 ай бұрын
what about for multiprocessing, why does it create new instances of the app
@furkank5614
@furkank5614 3 жыл бұрын
Hi nice video, i am working on a Voice Asisstant code. When i click the button command it's Get Audio. I tried apat this "thread" to my program but when i run it it, gets the audio before anything appears. After that Tkinter appears when i click the button it does nothing. I wish you can help.
@BrendonWilliams
@BrendonWilliams 3 жыл бұрын
This code is bugged. command=threading.Thread(target=five_seconds).start() needs to be command=threading.Thread(target=five_seconds).start or command=lambda: threading.Thread(target=five_seconds).start(). With how it is now, you're starting the thread when the button is created, not when it's clicked. Also, creating a thread on every button click isn't ideal. A thread pool would be a better alternative to allow reuse of threads.
@ramonschmeitz51
@ramonschmeitz51 3 жыл бұрын
ty so much my program wasn't working because of this
@BrendonWilliams
@BrendonWilliams 3 жыл бұрын
@@ramonschmeitz51 No problem. I know about it because a user on Stack Overflow had a problem as well (likely the same problem as you).
@НиколайТест-у8ф
@НиколайТест-у8ф 4 жыл бұрын
would be cool if you could show how to handle the thread running, especially how to stop it again.
@pengxiangyu7391
@pengxiangyu7391 4 жыл бұрын
Great Video! I just have a question though, it seems we can only press the button once. It will cause 'RuntimeError: threads can only be started once' if pressing the second time. Any idea on how to solve that?
@Codemycom
@Codemycom 4 жыл бұрын
Disable the button till after the thread finishes then enable the button again
@chukk
@chukk 4 жыл бұрын
Codemy.com Hi, I’m in the same situation of needing to use a button/thread multiple times. How do you go about doing what you suggested?
@chukk
@chukk 4 жыл бұрын
I found an answer in subclassing threads. Including this in the video would be super helpful. I’m surprised it’s not talked about more.
@xinpengdu3815
@xinpengdu3815 3 жыл бұрын
I had exactly the same issue. Here is the solution: don't open a thread in the command. just use the command in the original way, such as command=func. Then in func, define a sub-function. Put your original code in the sub-function, and use the func to open and start a thread. Something like this: def func(): def sub_func(): Your code threading.Thread(target=sub_func).start() button_1 = Button(root, text="YourText", command=func) button_1.pack()
@andresdeinnocentiis6498
@andresdeinnocentiis6498 3 жыл бұрын
@@xinpengdu3815 Statues should be made in your name
@neonsloth
@neonsloth 2 жыл бұрын
For me, any command I put into a button runs immediately as soon as TK opens. I used a lambda function in order to fix that. But now even the new thread freezes TK.
@mustafaaldulaimi9804
@mustafaaldulaimi9804 3 жыл бұрын
the only youtuber who is really knowing what he is doing thank you so much
@tobi5465
@tobi5465 3 жыл бұрын
how to stop the thread? and how to stop correctly, when the window is just closed?
@richardspahn1320
@richardspahn1320 2 жыл бұрын
Exactly. For example, if you click on the [X] button in the top-right of the window to close the window after the "5 Seconds" button has been clicked and before it completes, an exception error is raised( Exception in thread Thread-1) because the thread is still running. Can you PLEASE do a follow up video on how to kill a thread?
@jeremyayers3977
@jeremyayers3977 4 жыл бұрын
When I switch my function to run as a thread, my tkinter input fields aren't recognized. I'm trying to myEntry.get() from within the threaded function and I get an error saying myEntry is not defined. If I run my code without threading it works fine.
@Codemycom
@Codemycom 4 жыл бұрын
Hm...weird
@Dhruv_Jadhav
@Dhruv_Jadhav 4 жыл бұрын
I was reading about threading and came across the Queuing module. This module was used in a lot of answers in Stack Overflow. Could you also make a video about queuing events in Tkinter. Thanks!
@Codemycom
@Codemycom 4 жыл бұрын
we'll see
@hussamcheema
@hussamcheema 3 жыл бұрын
@@Codemycom yes man, please make a video on queuing module.
@screensauce
@screensauce 3 жыл бұрын
Amazing video love it. Made it really clear but Can you use this to thread a while true loop that's is blocking the root. mainloop() pls reply
@benjimanguapo
@benjimanguapo 4 жыл бұрын
Wow that is super cool, I love your videos. Like from México.
@Codemycom
@Codemycom 4 жыл бұрын
Thanks!
@martincastello9866
@martincastello9866 3 жыл бұрын
Hi! i could not be able to use threads in a gui structured in a class as the documentation recomended, can you give me some leads? the error is this: RuntimeError: main thread is not in main loop. Thanks for the answer!
@johnbonhomme2232
@johnbonhomme2232 3 жыл бұрын
I am having the same problem, but I may have found a solution (I haven't had the chance to try it yet). Check out tkthread
@wyattmurphy6217
@wyattmurphy6217 4 жыл бұрын
These videos are so helpful!!! I'm working on a project now where I think threading could be used. I was wondering: If two separate threads were created which called the same function, that had a variable; would each thread iteration change the variable def for each other or would the variable changes be contained to each thread? (i.e. thread A changes x to 1 and thread B changes x to 2. Would thread A, now read an x value of 2 on its next iteration?!)
@Codemycom
@Codemycom 4 жыл бұрын
Try it and see :-P It depends on how you write the code
@premgarg5534
@premgarg5534 4 жыл бұрын
What type of project you are working on? I am sure i can help you there
@Player-ix7rx
@Player-ix7rx 4 жыл бұрын
Great video, one question tho, I created a tkinter gui with quite a lot of functions inside of it that checks some live data (it grabs around 30-40 live data, each one with its own function and some math inside...), I use the '.after(1000)' function for the refresh, but after a while will freeze, should I use threading for each function? Thanks!
@Codemycom
@Codemycom 4 жыл бұрын
Without seeing your code, I couldn't say. But try it and see.
@chiefmiester3801
@chiefmiester3801 3 жыл бұрын
To those of you who's function gets executed before the button is clicked. Try this command=lambda:threading.Thread(target="___________").start()).pack()
@haxzor25
@haxzor25 3 жыл бұрын
Thank you soo much i was searching the exact fix.
@Codemycom
@Codemycom 3 жыл бұрын
glad it helped
@Planet_Xplorer
@Planet_Xplorer 3 жыл бұрын
My program takes few minutes when I press the ok button...I've applied the threating but the form still freezes...is there any 'threading' setting that could solve the problem?
@Codemycom
@Codemycom 3 жыл бұрын
No, you likely just didn't set up the threading correctly
@Planet_Xplorer
@Planet_Xplorer 3 жыл бұрын
@@Codemycom ok thanks
@andreamarchisio9608
@andreamarchisio9608 3 жыл бұрын
When launching the program the target function of the program gets executed once even though I do not click on the button..Why is that? Also, how do create a window from a thread without getting "main thread is not in main loop" error? Thanks
@andreamarchisio9608
@andreamarchisio9608 3 жыл бұрын
found the answer to the first question in this comment section..Please still consider my second question :)
@jokarjapan
@jokarjapan 2 жыл бұрын
This is what i'm looking for, thank you
@Codemycom
@Codemycom 2 жыл бұрын
to hear it!
@jokarjapan
@jokarjapan 2 жыл бұрын
Hhhhhhhhhh nice return (^_^)
@Iqbalsharib4
@Iqbalsharib4 2 жыл бұрын
Getting error when trying to run multiple times "thread can only be started once"
@alirezarezaie6148
@alirezarezaie6148 3 жыл бұрын
nice really awsome but how to stop thread?
@Planet_Xplorer
@Planet_Xplorer 3 жыл бұрын
I got it working...but what if you want to pass the number of seconds as a parameter to the five_seconds def so that you could choose the number of seconds? I tried a small edit like five_seconds (5) but once the form opens shows ""5 Seconds Is Up!" even thought I didn't click the button...any thoughts about that? thanks
@merijns3446
@merijns3446 3 жыл бұрын
You can can use lambda for this, the code would look like: command= lambda: threading.Thread(target= five seconds, args=[amountOfSeconds]).start
@Planet_Xplorer
@Planet_Xplorer 3 жыл бұрын
@@merijns3446 great, thanks!
@merijns3446
@merijns3446 3 жыл бұрын
@@Planet_Xplorer no problem, happy to help!
@-eckes-w.3574
@-eckes-w.3574 3 жыл бұрын
very cool, but one question: you wrote --- my_label2.config(text = ƒ"Zufallszahl : {randint(1, 100)}") --- but this won't run on my machine, so i change it to --- my_label2.config(text = f"Zufallszahl : {randint(1, 100)}") ---- and it works. Seems to be the same, but the differece is the "ƒ" and "f". I don't know how to make this function-sign - i copieed from google search. The normal f runs.
@Codemycom
@Codemycom 3 жыл бұрын
Use a normal f...I have no idea what that other thing is or why you're using it.
@shlokshah3522
@shlokshah3522 2 жыл бұрын
Hi great video, how could I run a function with threading that allows arguments to be passed into that function
@Codemycom
@Codemycom 2 жыл бұрын
use a lambda in the normal way
@harshdeepsingh4578
@harshdeepsingh4578 3 жыл бұрын
Hi, I used threading with Tkinter and it worked great but I faced a problem when I had to get a return from the function being called. for example, def test(): return 5 I want to be able to use and store that value and I have not been able to. part 2: I am also displaying live data using matplotlib.animation, this also freezes my app. However, when I use threading the live graphs I display disappear. Noth sure why this is. Could you give some advice?
@michaelfuchsluger6343
@michaelfuchsluger6343 3 жыл бұрын
I think I have a similar problem: I made a simple game and I want to run the GUI-update on a different thread than the game mechanics in order to speed up the game. But for some reason, when I start a new thread, it says "error: main thread is not in main loop". import turtle import threading wn = turtle.Screen()... ... def window_updating(): while True: wn.update() threading.Thread(target=window_updating).start() Without a new thread it works fine but if I start a new one, I get the error I mentioned before. My objects don't get updated on the screen, but the game mechanics are stil running. Maybe someone could help me too with this problem.
@parasgtr1984
@parasgtr1984 Жыл бұрын
how to make button that stops the thread?
@muhanad102
@muhanad102 2 жыл бұрын
Is there a problem is I updated the UI using a non-main thread?
@Codemycom
@Codemycom 2 жыл бұрын
Sorry, I don't know what you mean.
@flydr2
@flydr2 Жыл бұрын
Many thanks.... very well explained
@Codemycom
@Codemycom Жыл бұрын
Happy to help!
@mememachine3020
@mememachine3020 2 жыл бұрын
This tutorial was very helpful!, though im not so sure on how to stop a thread
@joedavies6918
@joedavies6918 2 жыл бұрын
Did you ever figure this out with tkinter? I'm currently stuck on the same thing!
@premgarg5534
@premgarg5534 4 жыл бұрын
Hi, i have a query I want a python script to run at boot time but not the whole program just one thread, SO can anyone please help me it that
@MrRagishtin
@MrRagishtin 3 жыл бұрын
you're great man .. I really can't thank you enough ..
@Codemycom
@Codemycom 3 жыл бұрын
Glad to help
@savinseneviratne3266
@savinseneviratne3266 2 жыл бұрын
sir, I encounted a problem, it says that a thread can only be started once. so how to restart that same thread in tkinter. please be kind enough to reply...
@Codemycom
@Codemycom 2 жыл бұрын
I don't think you restart a thread...just start a new one.
@HTWwpzIuqaObMt
@HTWwpzIuqaObMt 2 жыл бұрын
I am new to tkinter and i wanna execute code at the same time as the tk mainloop. Is threading a good solution for that?
@Codemycom
@Codemycom 2 жыл бұрын
Depends on what you're trying to do.
@Dennie1_
@Dennie1_ 4 жыл бұрын
Hi, I like your Videos and I'm learning some Python too. My Question is, is it possible to make an Game Launcher (like Steam, where you can start Games) in Tkinter? Have a nice day!
@FlynnsForge
@FlynnsForge 4 жыл бұрын
you could have buttons that run exe files os.startfile might work
@suzum0978
@suzum0978 4 жыл бұрын
yes u can ! and that s a brilliant idea to make a shortcuts program
@Codemycom
@Codemycom 4 жыл бұрын
Sorry, I don't know what that is.
@Dennie1_
@Dennie1_ 4 жыл бұрын
Do you know Steam, Uplay, Epic Games and stuff like this? They're game launchers, in other words, you download a game from steam and then you can launch a game out of the launcher. So you execute a .exe file. Like your mp3 Player you did, you executed a mp3 file, but in the launcher you execute a .exe file. I hope you understood this, my english is not so good :)
@Codemycom
@Codemycom 4 жыл бұрын
@@Dennie1_ No I don't know steam, I'm not a gamer. That's what I meant when I said "I don't know what that is" lol
@krishnanandshenoy2841
@krishnanandshenoy2841 4 жыл бұрын
Thank you for the video🙏 ..... Is there a limit to creating threads?
@kyleyoung4974
@kyleyoung4974 3 жыл бұрын
yes it is based on the processor chip in your computer
@alexvillegas8155
@alexvillegas8155 Жыл бұрын
You are a genius ! Thank you so much !
@Codemycom
@Codemycom Жыл бұрын
Welcome
@fernandohood5542
@fernandohood5542 4 жыл бұрын
How do you stop the thread or does it stop once the task is completed.
@Codemycom
@Codemycom 4 жыл бұрын
It stops when the task is completed
@tylerperry3607
@tylerperry3607 3 жыл бұрын
Try to ask a string inside a thread. If you're doing that. I'll consider learning your courses
@Codemycom
@Codemycom 3 жыл бұрын
funny
Spinboxes With TKinter - Python Tkinter GUI Tutorial #98
8:26
Codemy.com
Рет қаралды 18 М.
Adding a Full Screen ScrollBar - Python Tkinter GUI Tutorial #96
15:08
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
VAMPIRE DESTROYED GIRL???? 😱
00:56
INO
Рет қаралды 8 МЛН
Fake watermelon by Secret Vlog
00:16
Secret Vlog
Рет қаралды 22 МЛН
She's very CREATIVE💡💦 #camping #survival #bushcraft #outdoors #lifehack
00:26
Timers and Clocks with TKinter - Python Tkinter GUI Tutorial #79
12:30
Classes with tKinter - Python Tkinter GUI Tutorial #43
9:20
Codemy.com
Рет қаралды 107 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 317 М.
Read And Write To Text Files - Python Tkinter GUI Tutorial #100
14:33
threading vs multiprocessing in python
22:31
Dave's Space
Рет қаралды 586 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 408 М.
Tkinter - Multithreading
38:10
JobinPy
Рет қаралды 6 М.
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18