【问题标题】:What techniques are there to allow multiple threads in a tkinter program?有哪些技术可以允许 tkinter 程序中的多个线程?
【发布时间】:2020-04-07 04:15:24
【问题描述】:

我发现了这个简单的 Hello World tkinter 程序:

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")
        self.hi_there["text"] = "Hello World\n(click me again)"

root = tk.Tk()
app = Application(master=root)
app.mainloop()

如果我想让say_hi() 方法执行长时间运行的任务,同时偶尔更新 GUI,该怎么办?

如果我试试这个:

    def say_hi(self):
        print("hi there, everyone!")
        self.hi_there["text"] = "Hello World\n(wait...)"

        sleep(2)  # pretend to do something long-running

        self.hi_there["text"] = "Hello World\n(click me again)"

然后 GUI 在睡眠期间锁定,我从未看到按钮更改为:Hello World\n(wait...)

【问题讨论】:

  • 如果您在sleep 之前添加root.update(),那么您会在按钮上看到此文本。
  • @furas 这是一个示例,但可能需要对 GUI 进行多次更新,同时希望 GUI 变得生动。

标签: python multithreading tkinter


【解决方案1】:

一种可能的解决方案是修改下面的Application 类,它使用queue 和新方法poll() 作为消息泵来操作Thread 希望运行的代码项。

from time import sleep
from queue import Queue
from threading import Thread

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()
        self.queue = Queue()
        self.poll()        

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.start_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        def do_wait():
            self.hi_there["text"] = "Hello World\n(wait...)"
        def do_again():
            self.hi_there["text"] = "Hello World\n(click me again)"

        print("hi there, everyone!")
        self.queue.put(do_wait)
        sleep(2)  # pretend to do something long-running
        self.queue.put(do_again)

    def start_hi(self):
        thread = Thread(target = self.say_hi)
        thread.start()

    def poll(self):
        if not self.queue.empty():
            item = self.queue.get()
            item()
        self.master.after(100, self.poll)

如果您使用这个版本的类,那么您会注意到hi_there Button 释放并且按钮上的文本按预期更改为wait...

caveat emptor: 此代码还允许您创建大量 Thread 实例,但 poll() 方法仅每 100 毫秒轮询一次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    相关资源
    最近更新 更多