Home > OS >  Continue a code after calling a class method which opens a tkinter window
Continue a code after calling a class method which opens a tkinter window

Time:01-09

I have created a class "Node" which creates a binary tree. (I know i can use binarytree module but its a project given to me in DSA subject.)

class Node:
    def __init__(self, data) -> None:
        #initialisation

    def addNode(self, data): 
        # Code to add data.
        

    def traverse(self):
        #traverses the tree and returns a dict

    def view(
        self,
        length=500,
        width=1000,
        yDist=50,
        xDistScale=0.25,
        title='Tree View',
    ):
        # shows the tree in a tkinter window as in the screenshot attached
        tree = self.traverse()
        root = tk.Tk()
        self.root=root
        root.geometry(str(width) 'x' str(length))
        root.title(title)

        # ........ some code which places the nodes on the window using the place() method

        root.mainloop()

Now, I import the code in an another file, create an instance of the class add some nodes and call the view() method it works fine but the code after the view() methd does not run till I close the tkinter window. How can I make the code after view() run without the window being closed? Its ok if the window is not able to update.

Code where I import and use the node class :

t1 = Node(15)
t1.addNode(12)
t1.addNode(27)
t1.addNode(7)
t1.addNode(14)
t1.addNode(20)
t1.addNode(88)
t1.addNode(23)

t1.view()

# The code here does not run until I close the window.

Output of the above code : Link to image

I tried googling and also viewed following stackoverflow posts:

But nothing was helpful. Please help/guide me. I am new to StackOverflow and Python.

Thanks in Advance :)

CodePudding user response:

Method 1: Don't update window
You could replace the root.mainloop() with root.update(). This will stop showing any changes to the window. The window will only and always close if the program stops running.

Method 2: using threading
You could useimport threading to run t1.view() in another thread: threading.Thread(target=t1.view).start(). This may result in a Tcl_AsyncDelete error.

CodePudding user response:

Thanks @Emil105 your answer is working fine.

For Method 1, what I did is :

  • replaced the root.mainloop() with root.update()
  • then in the driver code, I added an input() function to halt the execution and its working fine.

Updated code:

t1 = Node(15)
t1.addNode(12)
t1.addNode(27)
t1.addNode(7)
t1.addNode(14)
t1.addNode(20)
t1.addNode(88)
t1.addNode(23)

t1.view(width=400,title='step 1') # Opens the first window...

t1.addNode(5)
t1.addNode(1)
t1.addNode(0)
t1.addNode(2)
t1.addNode(6)
t1.addNode(91)
t1.addNode(61)
t1.addNode(16)
t1.addNode(25)
t1.addNode(31)
t1.addNode(13)

t1.view(width=600,title='step 2') # Opens the second window...
input()

Method 2 : It also works with the threading.Thread() as:

#initialise and add some nodes
threading.Thread(target=t1.view,kwargs={'width':600,'title':'step 1'}).start() #opens first window
#add some more nodes
threading.Thread(target=t1.view,kwargs={'width':600,'title':'step 2'}).start() #opens second window

But raises error : "Tcl_AsyncDelete: async handler deleted by the wrong thread Aborted (core dumped)"

Screenshots of the first window and second window.

  •  Tags:  
  • Related