user9219312
user9219312

Reputation: 107

Error while trying to change tkinter label from another thread

I have thread1. and a main GUI thread. I am trying to change a label on the GUI thread, form the thread1. I pass the variable of the label on the thread as an argument. But I get the following error:

UnpickleableError: Cannot pickle objects

How can I change GUI elements outside the main GUI Thread/Class

class MyFirstGUI:
    communicationQueue=Queue()
    def __init__(self, master):
      thisLabel = Label(master, text="Test")
      thisLabel.pack()
      tempThread=testThread(thisLabel)
      tempThread.start()

class testThread(Thread):
    def __init__(self, label):
      label["text"]="something"

Upvotes: 0

Views: 869

Answers (1)

Joshua Nixon
Joshua Nixon

Reputation: 1425

import tkinter as tk
import threading, random, time

class MyFirstGUI:
    def __init__(self, master):
      self.label = tk.Label(master, text = "Test")
      self.label.pack()

    def update(self):
        while True:
            self.label["text"] = random.randint(1, 1000)
            time.sleep(1)


root = tk.Tk()

GUI = MyFirstGUI(root)

loop = threading.Thread(target = GUI.update).start()

root.mainloop()

I think this is what you are asking for.

Upvotes: 1

Related Questions