Tkinter GUI 中的多线程,不同类中的线程 [英] Multi threading in Tkinter GUI, threads in different classes

查看:73
本文介绍了Tkinter GUI 中的多线程,不同类中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 Tkinter GUI 编程.我被困在多线程概念的某个地方.尽管这个话题在这里讨论了好几次,我还是无法理解这个概念并将它应用到我的小示例程序中.

I'm currently learning the Tkinter GUI programming. And I'm stuck in somewhere in multi threading concept. Even though this topic is discussed several times here, I couldn't catch the concept and apply it to my small sample program.

下面是我的代码:

from PIL import Image, ImageTk 
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style
from Tkinter import *
import time


class Widgets(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.grid()
        self.parent = parent
        self.initUI(parent)

    def initUI(self, parent):
        self.parent.title("Count Numbers")

        for r in range(10):
            self.parent.rowconfigure(r, weight=1)    
        for c in range(10):
            self.parent.columnconfigure(c, weight=1)        

        self.button1 = Button(parent, text = "count")
        self.button1.grid(row = 1, column = 1, rowspan = 1, columnspan = 2, sticky = W+E+N+S )
        self.button1["command"] = self.countNum

        self.button2 = Button(parent, text = "say Hello")
        self.button2.grid(row = 1, column = 7, rowspan = 1, columnspan = 2, sticky = W+E+N+S) 
        self.button2["command"] = PrintHello(self).helloPrint

    def countNum(self):
        for i in range(10):
            print i
            time.sleep(2)   

class PrintHello(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.grid()
        self.parent = parent

    def helloPrint(self):
        print "Hello"

def main():
    root = Tk()
    root.geometry("300x200")
    app = Widgets(root)
    root.mainloop()

if __name__ == '__main__':
    main() 

输出是一个带有 2 个按钮的 GUI - 第一个打印数字,第二个打印Hello".但是在单击第一个按钮时,GUI 会冻结,同时打印数字.在寻找解决方案时,我发现多线程"可能会有所帮助.但是经过多次尝试,我无法将多线程应用到我给出的示例程序中.

The output is a GUI with 2 buttons- first one prints the numbers and second prints "Hello". But on clicking the first button, the GUI gets frozen while the numbers are getting printed. And while searching for a solution, I found that 'multi threading' may help. But after several attempts, I couldn't apply multi-threading to my sample program given.

推荐答案

对于这么简单的事情,您不需要线程.

You don't need threading for something this simple.

GUI 冻结,因为您将 time.sleep 放入阻塞主线程直到完成的函数中.

The GUI is freezing because you're putting a time.sleep inside the function which is blocking the main thread until it's finished.

只需使用 Tk 内置的 after 方法.将您的功能更改为.

Simply use Tk's built in after method. Change your function to.

def countNum(self, num=0):
    if num < 10:
        print num
        root.after(2000, lambda: self.countNum(num + 1))
    else:
        print "Stopping after call"

after 方法采用以下参数:

after(delay_ms, callback, arguments)

时间以毫秒为单位,1000 毫秒 = 1 秒.因此,我们传递了 2,000 毫秒的 2 秒延迟.

The time is in milliseconds, and 1000 ms = 1 second. So, we pass 2,000 ms for a 2 second delay.

这篇关于Tkinter GUI 中的多线程,不同类中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆