在Text Widget [Tkinter]中显示列表的内容

时间:2014-03-28 06:25:12

标签: multithreading python-2.7 tkinter

我正在尝试创建一个不断显示列表内容的程序。

我正在使用thread,因为我会让程序由多个用户运行。

列表的内容将会更新,因此我使用了while True:

ff。代码不起作用,Tkinter只是挂起。

有什么问题?

from Tkinter import *
import time,thread

class Show():
    def loop(self):
        dummy = []
        while True:
            for index,x in enumerate(self.content):
                x = x.split("-")
                if x[0] not in dummy:
                    firstF = Frame(self.root)
                    secondF = Frame(self.root)
                    firstF.pack()
                    secondF.pack()

                    words = Label(firstF,text="Title:")
                    words.pack(side=LEFT)
                    theLabel = Label(firstF,text=x[0])
                    theLabel.pack(side=LEFT)
                    theContent = Text(secondF)
                    theContent.insert(INSERT, x[1])
                    theContent.configure(state=DISABLED)
                    theContent.pack()
                    dummy.append(x[0])
                else:
                    pass
            time.sleep(1)
    def run(self):
        self.root = Tk()
        self.root.title("EXPERIMENT")
        self.root.grid()

        self.content = ["Hello-1","Foo-2","Bar-3"]
        self.a = thread.start_new_thread(self.loop,())
        self.root.mainloop()

A = Show()
A.run()

代码在没有Text小部件的情况下工作正常。

任何人都可以教我出了什么问题以及如何解决它吗?

我发现冻结背后的原因是theContent.insert...

但我仍然不知道如何解决它。

  • 我正在寻找根据Text widget的长度创建self.content的解决方案,以便在for x in self.content分割x时,x[0]会转到顶部x[1]将放入Text widget

0 个答案:

没有答案