Tkinter - Multithreading

Поделиться
HTML-код
  • Опубликовано: 21 июл 2024
  • I'll show you how to use multi-threading with Tkinter. We'll look at how to communicate with the main GUI thread from a second thread. Multi-threading is an important topic for Tkinter applications that have any kind of network feature.
    Please consider supporting my work with your donations on Ko-fi:
    ko-fi.com/jobinpy
    Multithreading with Tkinter.
    0:00 Intro
    1:30 Demo application
    2:55 The issue
    3:36 Create a window
    5:24 Create a frame
    8:08 Create a progress label
    8:55 Download method
    10:33 Frozen window
    12:23 Create a separate thread
    15:46 Demo of separate thread
    17:17 Communicate from second thread to first thread
    21:00 Create ticket queue
    26:49 Notify main thread to check queue
    29:12 Check queue method
    32:26 when= argument for event_generate()
    33:25 Continue checking queue
    34:03 Demo application
    35:32 Showing a download has finished
    37:17 Closure
    #tkinter #tkintertutorial #multithread #pythongui #pythonguitutorial
    event_generate()
    tcl.tk/man/tcl8.5/TkCmd/event....
  • НаукаНаука

Комментарии • 47

  • @kharaktur
    @kharaktur 9 месяцев назад +2

    Excellent tutorial, thanks to you I now have responsive tkinter apps with long running tasks. I was never able to figure this out and I just couldn't get my head around other multi-threading tutorials, but you made this subject crystal clear. You are a superb teacher.

    • @jobinpy
      @jobinpy  9 месяцев назад

      Thanks for your kind words!

  • @kychemclass5850
    @kychemclass5850 Год назад +1

    Nice! Really useful and helpful. Thank you!!

  • @johnstoddart5473
    @johnstoddart5473 Год назад +3

    Great video, Thanks for sharing the knowledgs

  • @idopshik
    @idopshik 4 месяца назад

    So good. Your channel will be very popular.

  • @smittyIT
    @smittyIT 10 месяцев назад +2

    This video helped me immensely when writing my first multi-threaded application with customtkinter and queue. Much appreciated. I will be subscribing for future content.

    • @jobinpy
      @jobinpy  9 месяцев назад +1

      Thanks for watching!

  • @daniliser2894
    @daniliser2894 7 месяцев назад

    Briliiant, well explained - well paced. thank you Jobin !

    • @daniliser2894
      @daniliser2894 7 месяцев назад

      you have a new subscriber

    • @jobinpy
      @jobinpy  6 месяцев назад

      Glad it was helpful!

  • @el_munoz
    @el_munoz Год назад +1

    Maybe the application works like the video shows, but when you close the app, is very frequent that the kernel crashes, showing a message with something like "another thread try to change something in the main thread" because GUI is no thread safe. This is a very good solution to this issue, and is another good example of why the OOP approach is the best way when your application needs to do interesting things.

  • @FufaTujuba
    @FufaTujuba 8 месяцев назад +1

    I was trying to convert audios from video files. That means converting music videos to mp3 media files in bulk. So I have to display extracted mp3 names and total number of processed files as it goes. Your tutorials helped me achieve this goal. Thank you very much.

    • @jobinpy
      @jobinpy  8 месяцев назад

      Thanks for watching!

  • @georgewairobi5410
    @georgewairobi5410 6 месяцев назад

    Good work sir🎉.
    You have earned a new subscriber!
    I especially love the way you follow foundamental coding standards and best practices. You are teaching us to do it the write way, unlike most of the other tutorials.
    This is important because most of us are self taught and not CS graduates.

    • @jobinpy
      @jobinpy  6 месяцев назад

      Thanks a lot! I'm glad the tutorials are useful to you.

  • @fredflintstone8048
    @fredflintstone8048 10 месяцев назад

    Another great tutorial clearly taught. Thanks for sharing.
    The comment at the end is so true in that most any GUI development will present this problem.
    The problem is the same for example with developing GUIs in Windows using things like MS Visual studio creating GUI applications. Whether using WPF, or the old Winforms approach one needs to implement threading in order to deal with this problem of long running processes freezing up the GUI and all it's controls until the process completes.

    • @jobinpy
      @jobinpy  9 месяцев назад

      Thanks for watching!

  • @ET237-ff1ry
    @ET237-ff1ry 8 месяцев назад

    I love you man.. very very nice tutorials.. thanks big time..

    • @jobinpy
      @jobinpy  8 месяцев назад

      Thanks for your kind words!

  • @asrajan55
    @asrajan55 16 дней назад

    Very helpful video, I have been looking for how to do this, this info is probably found nowhere else .. in a video format.

    • @jobinpy
      @jobinpy  14 дней назад

      I'm glad you liked it!

  • @tech-preneurshipwithjeff5745
    @tech-preneurshipwithjeff5745 Год назад +1

    beautifully done.

  • @bastionjordan556
    @bastionjordan556 6 месяцев назад

    This tutorial moves Tkinter from a cute GUI tool to something incredibly useful for real world applications. I need to utilize grid geometry, so it's going to take me a bit to modify your tutorials from using the pack method to grid, so hopefully I won't go down a rabbit hole. If you have any pointers on grid geometry and threaded applications, I'd appreciate it!

    • @jobinpy
      @jobinpy  6 месяцев назад

      I'm glad you found the video useful. Tkinter is a great GUI toolkit and professional applications can be built using it.

  • @scotteebee
    @scotteebee 9 месяцев назад

    Great tutorial. It’s exactly what I’ve been looking for guidance on. But, if tkinter is not thread safe, what makes it safe to send the event from the child thread? Should the “after” method be used to poll the queue from the main thread instead?

    • @jobinpy
      @jobinpy  8 месяцев назад

      Hi, thanks for watching.I've checked the tcl/tk documentation to see what makes event_generate() different from other methods in tk, as far as thread safety, and didn't find a detailed technical reason. I believe the reason for its safety is because it simulates a window event, instead of actually being from a window event. The following two links are good reads about this topic: tkdocs.com/tutorial/eventloop.html#threads and also www.tcl.tk/man/tcl/TkCmd/event.html#M7
      Polling the queue using 'after' is also another workaround, but I personally prefer event_generate() without 'after' because it's worked well for me.

  • @WhiteError37
    @WhiteError37 5 месяцев назад

    How would I redirect sys.stdout and sys.stderr to another text widget in the program. Would I have to make a new class or could I use the ticket system

  • @SaharSahar-uh8fr
    @SaharSahar-uh8fr 3 месяца назад

  • @tech-preneurshipwithjeff5745
    @tech-preneurshipwithjeff5745 Год назад

    what editor are you using?

  • @bhagyeshwargavekar891
    @bhagyeshwargavekar891 7 месяцев назад

    @jobinpy i am trying to create GUI using tkinter but i am facing very much GUI crashing issue. Please do video on this topic. Thanks in advance.

  • @andromilk2634
    @andromilk2634 4 месяца назад

    I'm a beginner who learned a lot from this but there was one thing which I was wondering : you used auto to generate a type for the tickets in your queue, if I understand correctly, but this value what is it exactly? Is it always a string? Thanks

    • @jobinpy
      @jobinpy  4 месяца назад

      I use auto() for the value in an Enum when the actual value is not important in my code. I could set the value to 1,2,3,4, etc but that would take long. Using auto() takes care of this. The ticket type (name) is the only thing that's useful in this situation, so auto() allows me to forget about the value. For details, visit: docs.python.org/3/library/enum.html#enum.auto

  • @legion_prex3650
    @legion_prex3650 8 месяцев назад +1

    you have got a new subscriber! You have really nice teaching skills! But there is one thing, i don't really get. Why are you creating a new Ticket Instance for every loop? Shouldn't this be only one object with states updated? Maybe i am missing something or i don't get the purpose of the program. Otherwise, great stuff!

    • @jobinpy
      @jobinpy  8 месяцев назад

      Hi, thanks for watching the video. I think you're right. It's possible and better to use one Ticket instance instead of creating a new object on each iteration in the for-loop; good catch!

    • @legion_prex3650
      @legion_prex3650 8 месяцев назад

      @@jobinpy Hi! Thanks for your reply! I really like that you show advanced topics in your videos. The Queue Datatype is so good and it was a joy to see one using it! Thanks again and have a nice day!

  • @MrPragmaticLee
    @MrPragmaticLee 9 месяцев назад

    Is this code available for download anywhere? It would make a great starting point for a project I'm working on.

    • @jobinpy
      @jobinpy  9 месяцев назад

      I don't have the code anywhere except in the video, sorry. Thanks for watching!

  • @mrkalvinbs1424
    @mrkalvinbs1424 Год назад +1

    How would I make a button to cancel the download (cancel the secondary thread)?

    • @jobinpy
      @jobinpy  Год назад +2

      When downloading a file with a progress bar, it's usually done using a for-loop. The for-loop needs to check for a Cancel (bool) flag on each iteration. The cancel button would simply set the cancel flag (is_canceled=True) and the for-loop would read and then see is_canceled==True and break out of the loop. Then use event_generate("",...) to notify the main thread (GUI) that the download has been cancelled.

    • @mrkalvinbs1424
      @mrkalvinbs1424 Год назад

      @@jobinpy What if my thread didn't have a loop. Example: a tk program runs an automation with selenium webdriver in a secondary thread (where a log with the automation steps would be written in the tk gui [tk.Text]). Would it be possible for a 'cancel button' in the tk gui to stop this secondary thread and close the webdriver?

    • @jobinpy
      @jobinpy  Год назад +1

      @@mrkalvinbs1424 Hi, I don't think there is an elegant way to close a secondary thread from the main GUI thread. However, there is a module called 'multiprocessing' and it has a terminate() method. That might be more suitable for your situation, instead of the 'threading' module. docs.python.org/3/library/multiprocessing.html

  • @ibrahemelgammal343
    @ibrahemelgammal343 Год назад +2

    I love u bro thank u keep the good work
    but please make the code more simlpe maybe at least put OOP aside so we ca be more focused on the main method

    • @jobinpy
      @jobinpy  Год назад +1

      Hi, thanks for watching. I think using classes and objects makes it easier to see what's happening in the code. I'll consider the suggestion for future videos, thanks again.

    • @mrkalvinbs1424
      @mrkalvinbs1424 Год назад +2

      @@jobinpy Using oop despite being more complex is the correct way to work with gui. So I think you should continue the explanations using oop

  • @tech-preneurshipwithjeff5745
    @tech-preneurshipwithjeff5745 Год назад

    what editor are you using?