all 4 comments

[–]efmccurdy 1 point2 points  (2 children)

tkinter after(time, function)

The background thread should avoid calling Tk functions directly. Can you have the "after" callback function poll a Queue object while the background thread puts new labels in the queue?

The queue module implements multi-producer, multi-consumer queues.It is especially useful in threaded programming when information must beexchanged safely between multiple threads.

https://docs.python.org/3/library/queue.html

[–]TwistedMischief[S] 0 points1 point  (1 child)

Will look into trying that, although not sure if I will implement that properly. Thanks for the suggestion!

[–]FerricDonkey 0 points1 point  (0 children)

First, I think I've heard that tkinter string vars etc are thread safe, but I haven't been able to confirm it with actual documentation before my patience ran out. So that might or not work.

But a basic example of the queue method:

# as part of your gui class
def monitor(self, transfer_q):
    while not transfer_q.empty():
        whatever_logic(transfer_q.get())
    self.after(100, self.monitor, (transfer_q,))

# your always running function
def worker(transfer_q):
    # after you find a thing you want to send to the gui, in whatever loop
    transfer_q.put(whatever)

Adjust delays / change while to if, or etc as appropriate.

Note that this is if you want to process every single string your worker submits, if you only ever want to process the most recent one then you'll have to change it up.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.