samedi 9 août 2014

Multithreading - affichage contenu d'une liste dans le Widget texte [Tkinter] - Stack Overflow


I am trying to create a program that constantly shows the content of a list.


I am using thread since I will make the program be ran by multiple users.


The content of the list will be updated so I used while True:


The ff. code is not working and the Tkinter just hangs.


What is wrong?


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()



The code just works fine without the Text widget.


Can anyone teach me what went wrong and how to fix it?


I found out that the reason behind the freezing is due to theContent.insert....


But I still do not know how to fix it.



  • I am looking for a solution to create Text widget based on the length of the self.content in such that when for x in self.content, x is splitted, the x[0] goes to top and x[1] will be put in the Text widget



I am trying to create a program that constantly shows the content of a list.


I am using thread since I will make the program be ran by multiple users.


The content of the list will be updated so I used while True:


The ff. code is not working and the Tkinter just hangs.


What is wrong?


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()



The code just works fine without the Text widget.


Can anyone teach me what went wrong and how to fix it?


I found out that the reason behind the freezing is due to theContent.insert....


But I still do not know how to fix it.



  • I am looking for a solution to create Text widget based on the length of the self.content in such that when for x in self.content, x is splitted, the x[0] goes to top and x[1] will be put in the Text widget


0 commentaires:

Enregistrer un commentaire