Замораживание / зависание tkinter Gui в ожидании завершения потока - PullRequest
0 голосов
/ 10 декабря 2018

Мой интерфейс зависает при нажатии кнопки.Я использую потоки, но я не уверен, почему все еще висит.Любая помощь будет оценена.Заранее спасибо

class magic:
    def __init__(self):
        self.mainQueue=queue.Queue()

    def addItem(self,q):
        self.mainQueue.put(q)

    def startConverting(self,funcName):
        if(funcName=="test"):
            while not self.mainQueue.empty():
                t = Thread(target = self.threaded_function)
                t.start()
                t.join()

    def threaded_function(self):

        time.sleep(5)
        print(self.mainQueue.get())

m=magic()
def helloCallBack():
   m.addItem("asd")
   m.startConverting("test")  //this line of code is freezing

B = tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack()
top.mainloop()

1 Ответ

0 голосов
/ 10 декабря 2018

Вот рецепт для выполнения асинхронной задачи с графическим интерфейсом на основе tkinter.Я адаптировал его по рецепту в цитируемой книге.Вы должны быть в состоянии изменить его так, чтобы он делал то, что вам нужно.

Чтобы поддерживать отзывчивость GUI, не нужно вмешиваться в его mainloop(), выполняя что-то вроде join() фонового потока - что приводит к зависанию GUI"пока нить не закончится.Это достигается с помощью универсального метода виджета after() для опроса Queue через равные промежутки времени.

# from "Python Coobook 2nd Edition", section 11.9, page 439.
# Modified to work in Python 2 & 3.
from __future__ import print_function

try:
    import Tkinter as tk, time, threading, random, Queue as queue
except ModuleNotFoundError:   # Python 3
    import tkinter as tk, time, threading, random, queue

class GuiPart(object):
    def __init__(self, master, queue, end_command):
        self.queue = queue
        # Set up the GUI
        tk.Button(master, text='Done', command=end_command).pack()
        # Add more GUI stuff here depending on your specific needs

    def processIncoming(self):
        """ Handle all messages currently in the queue, if any. """
        while self.queue.qsize():
            try:
                msg = self.queue.get_nowait()
                # Check contents of message and do whatever is needed. As a
                # simple example, let's print it (in real life, you would
                # suitably update the GUI's display in a richer fashion).
                print(msg)
            except queue.Empty:
                # just on general principles, although we don't expect this
                # branch to be taken in this case, ignore this exception!
                pass


class ThreadedClient(object):
    """
    Launch the main part of the GUI and the worker thread. periodic_call()
    and end_application() could reside in the GUI part, but putting them
    here means that you have all the thread controls in a single place.
    """
    def __init__(self, master):
        """
        Start the GUI and the asynchronous threads.  We are in the main
        (original) thread of the application, which will later be used by
        the GUI as well.  We spawn a new thread for the worker (I/O).
        """
        self.master = master
        # Create the queue
        self.queue = queue.Queue()

        # Set up the GUI part
        self.gui = GuiPart(master, self.queue, self.end_application)

        # Set up the thread to do asynchronous I/O
        # More threads can also be created and used, if necessary
        self.running = True
        self.thread1 = threading.Thread(target=self.worker_thread1)
        self.thread1.start()

        # Start the periodic call in the GUI to check the queue
        self.periodic_call()

    def periodic_call(self):
        """ Check every 200 ms if there is something new in the queue. """
        self.master.after(200, self.periodic_call)
        self.gui.processIncoming()
        if not self.running:
            # This is the brutal stop of the system.  You may want to do
            # some cleanup before actually shutting it down.
            import sys
            sys.exit(1)

    def worker_thread1(self):
        """
        This is where we handle the asynchronous I/O.  For example, it may be
        a 'select()'.  One important thing to remember is that the thread has
        to yield control pretty regularly, be it by select or otherwise.
        """
        while self.running:
            # To simulate asynchronous I/O, create a random number at random
            # intervals. Replace the following two lines with the real thing.
            time.sleep(rand.random() * 1.5)
            msg = rand.random()
            self.queue.put(msg)

    def end_application(self):
        self.running = False  # Stops worker_thread1 (invoked by "Done" button).

rand = random.Random()
root = tk.Tk()
client = ThreadedClient(root)
root.mainloop()
...