Python 在函数执行过程中在帧中添加标签

Python 在函数执行过程中在帧中添加标签,python,multithreading,tkinter,Python,Multithreading,Tkinter,我为实验室编写了一个管道,所以我知道在GUI中插入“控制台”是不可能的,所以我用一个框架制作了它,并在上面贴上了标签 但问题是,我是线程的初学者,我不知道在循环中执行函数后如何使用它将标签放入框架中 这是我的代码(python 3.x): 问题在于循环何时开始于: def start_foo_thread(self): liste = self.typeNP() for i in range(len(liste)): 使用print函数,我看到函数在运行

我为实验室编写了一个管道,所以我知道在GUI中插入“控制台”是不可能的,所以我用一个框架制作了它,并在上面贴上了标签

但问题是,我是线程的初学者,我不知道在循环中执行函数后如何使用它将标签放入框架中

这是我的代码(python 3.x):

问题在于循环何时开始于:

    def start_foo_thread(self):
        liste = self.typeNP()
        for i in range(len(liste)):
使用print函数,我看到函数在运行,但迭代结束时标签不会进入框架。循环完成后,我会在框架中看到我的标签。
在循环中运行函数的同时将标签放在帧中的正确代码是什么?

有时您必须手动更新小部件。您发布的代码太多,无法浏览,因此这个简单的示例应该说明问题所在。按原样运行,直到函数返回,标签才会显示。在未注释update_idletasks()的情况下运行,我认为这是您想要的。还请注意,程序将停止,直到对子进程的调用返回

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

class GraphicalUserInterface():
    def __init__(self):
        #constructor
        self.fen = tk.Tk()
        self.fen.title("Starch Enzyme Pipeline")
        self.console=tk.Frame(self.fen)
        self.console.grid()
        self.liste=["one", "two", "three"]
        tk.Button(self.fen, text="Exit", command=self.fen.quit).grid(row=1)
        self.start_foo_thread()
        self.fen.mainloop()

    def start_foo_thread(self):
        for ctr in range(len(self.liste)):
            lit='Fichier n %s' % (ctr)
            print(lit)
            stringLabel = tk.Label(self.console, text=lit,
                                   bg='black', fg='white')
            stringLabel.grid(row=ctr,sticky="W")
            ##self.console.update_idletasks()
        print("Waiting 2 seconds")
        self.fen.after(2000)  ## wait 2 seconds to show effect
        print("Return from function")

if __name__ == '__main__':
    app = GraphicalUserInterface()
导入系统 如果系统版本信息[0]<3: 将Tkinter作为tk##Python 2.x导入 其他: 将tkinter作为tk##Python 3.x导入 类GraphicalUserInterface(): 定义初始化(自): #建造师 self.fen=tk.tk() self.fen.title(“淀粉酶管道”) self.console=tk.Frame(self.fen) self.console.grid() self.liste=[“一”、“二”、“三”] 按钮(self.fen,text=“Exit”,command=self.fen.quit).grid(行=1) self.start\u foo\u线程() self.fen.mainloop() def start_foo_线程(自): 对于范围内的ctr(len(self.liste)): lit='Fichier n%s'(中心) 打印(发光) stringLabel=tk.Label(self.console,text=lit, 背景为“黑色”,前景为“白色”) stringLabel.grid(row=ctr,sticky=“W”) ##self.console.update_idletasks() 打印(“等待2秒”) self.fen.after(2000)##等待2秒以显示效果 打印(“从函数返回”) 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': app=GraphicalUserInterface()
要读的东西很多。请创建。除了创建tkinter小部件的线程外,您不能从任何线程访问tkinter小部件。通过在本网站上搜索
[tkinter]threading
进行更多研究。这是一个复杂的问题,这可能会使你的问题过于宽泛。非常感谢你的建议,yoanHello!非常感谢你的帮助!在那里我错过了一行代码:
self.console.update_idletasks()
,在我得到我想要的东西之后:)这是我第一篇真正的帖子,我在学习生物信息学,我是法国人,感谢那些更正了信息的人,感谢那些阅读和回复的人,非常感谢你
import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

class GraphicalUserInterface():
    def __init__(self):
        #constructor
        self.fen = tk.Tk()
        self.fen.title("Starch Enzyme Pipeline")
        self.console=tk.Frame(self.fen)
        self.console.grid()
        self.liste=["one", "two", "three"]
        tk.Button(self.fen, text="Exit", command=self.fen.quit).grid(row=1)
        self.start_foo_thread()
        self.fen.mainloop()

    def start_foo_thread(self):
        for ctr in range(len(self.liste)):
            lit='Fichier n %s' % (ctr)
            print(lit)
            stringLabel = tk.Label(self.console, text=lit,
                                   bg='black', fg='white')
            stringLabel.grid(row=ctr,sticky="W")
            ##self.console.update_idletasks()
        print("Waiting 2 seconds")
        self.fen.after(2000)  ## wait 2 seconds to show effect
        print("Return from function")

if __name__ == '__main__':
    app = GraphicalUserInterface()