2

I'm using python and tkinter to build a visualization tool that can refresh and visualize an updating object. Right now, the object can't change because the threading is not working. Any help or general knowledge would be appreciated. I'm relatively new to threading and tkinter.

example object I want to ingest

class color1:
    def __init__(self, color):
       self.color = color

    def change_col(self, new_color):
        self.color = new_color

    def pass_col(self):
        return(self)

my visualization code

class my_visual(threading.Thread):

    def __init__(self, col1):
        threading.Thread.__init__(self)
        self.start()
        self.col1 = col1

    def viz(self):
        self.root = Tk()
        btn1 = Button(self.root, text = 'Refresh', command = self.refresh)
        btn1.pack()
        frame = Frame(self.root, width = 100, height = 100, bg = self.col1.color)
        frame.pack()
        btn2 = Button(self.root, text = 'Close', command = self.exit)
        btn2.pack()
        self.root.mainloop()

    def refresh(self):
        self.root.quit()
        self.root.destroy()
        self.col1 = self.col1.pass_col()
        self.viz()


    def exit(self):
        self.root.quit()
        self.root.destroy()

Code that works

c = color1('RED')
test = my_visual(c)
test.viz()

Code that doesn't work

In this version, the refresh works, but the threading doesn't. When the threading is working, the refresh won't pick up that the object has changed.

c.change_col('BLUE')
2
  • Why do you need threading? That seems to add a bunch of overhead without providing any benefit. Commented Feb 13, 2020 at 23:32
  • Although your class have inherited from threading.Thread class, but the derived class did not include a run() function. That means start() will do nothing. For your case, using thread is not necessary. Commented Feb 14, 2020 at 7:46

2 Answers 2

2

If you extend the threading.Thread class you need to override the run() method with your custom functionality. With no run method, the thread dies immediately. You can test whether a thread is alive with my_visual.is_alive().

Sign up to request clarification or add additional context in comments.

1 Comment

I definitely need to investigate this more. I think i'm going to move the threading outside of the class since the project has pivoted a bit. Thank you for the response
1

The problem is that your test.viz() is an infinite loop because of self.root.mainloop(), so you cannot do anything once you called that function. The solution is to use a thread for test.viz(), and your thread for the class my_visual is no more necessary.

I added a time.sleep of 2 seconds before the refresh makes it blue, otherwise the color is blue at beginning.

Here you go :

import threading
from tkinter import *
import time

class color1:
    def __init__(self, color):
       self.color = color

    def change_col(self, new_color):
        self.color = new_color

    def pass_col(self):
        return(self)

class my_visual():

    def __init__(self, col1):
        self.col1 = col1

    def viz(self):
        self.root = Tk()
        btn1 = Button(self.root, text = 'Refresh', command = self.refresh)
        btn1.pack()
        frame = Frame(self.root, width = 100, height = 100, bg = self.col1.color)
        frame.pack()
        btn2 = Button(self.root, text = 'Close', command = self.exit)
        btn2.pack()
        self.root.mainloop()

    def refresh(self):
        self.root.quit()
        self.root.destroy()
        self.col1 = self.col1.pass_col()
        print("self.col1", self.col1, self.col1.color)
        self.viz()


    def exit(self):
        self.root.quit()
        self.root.destroy()

c = color1('RED')
test = my_visual(c)
t2 = threading.Thread(target = test.viz)
t2.start()

time.sleep(2)
print('Now you can change to blue when refreshing')
c.change_col('BLUE')

1 Comment

This was really helpful, and i think this is the direction the functionality will be moving in. Thank you for the info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.