简体   繁体   English

调用 class 方法后继续代码,该方法打开 tkinter window

[英]Continue a code after calling a class method which opens a tkinter window

I have created a class "Node" which creates a binary tree.我创建了一个创建二叉树的 class “节点”。 (I know i can use binarytree module but its a project given to me in DSA subject.) (我知道我可以使用二叉树模块,但它是 DSA 主题中给我的一个项目。)

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.现在,我将代码导入另一个文件,创建 class 的实例,添加一些节点并调用 view() 方法它工作正常,但是 view() 方法之后的代码在我关闭 tkinter Z05B8C74CBD96FBF2DE4ZC1A3524 之前不会运行How can I make the code after view() run without the window being closed?在没有关闭 window 的情况下,如何使 view() 之后的代码运行? Its ok if the window is not able to update.如果 window 无法更新,则可以。

Code where I import and use the node class:我在其中导入和使用节点 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上述代码的Output:图片链接

I tried googling and also viewed following stackoverflow posts:我尝试了谷歌搜索,还查看了以下 stackoverflow 帖子:

But nothing was helpful.但没有任何帮助。 Please help/guide me.请帮助/指导我。 I am new to StackOverflow and Python.我是 StackOverflow 和 Python 的新手。

Thanks in Advance:)提前致谢:)

Method 1: Don't update window方法一:不要更新 window
You could replace the root.mainloop() with root.update() .您可以将root.mainloop()替换为 root.update root.update() This will stop showing any changes to the window.这将停止显示对 window 的任何更改。 The window will only and always close if the program stops running. window 只有在程序停止运行时才会关闭。

Method 2: using threading方法二:使用线程
You could use import threading to run t1.view() in another thread: threading.Thread(target=t1.view).start() .您可以使用import threading在另一个线程中运行t1.view()threading.Thread(target=t1.view).start() This may result in a Tcl_AsyncDelete error.这可能会导致 Tcl_AsyncDelete 错误。

Thanks @Emil105 your answer is working fine.谢谢@Emil105,你的回答很好。

For Method 1, what I did is:对于方法1,我所做的是:

  • replaced the root.mainloop() with root.update()root.mainloop()替换了 root.mainloop root.update()
  • then in the driver code, I added an input() function to halt the execution and its working fine.然后在驱动程序代码中,我添加了一个input() function 来停止执行并使其正常工作。

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:方法 2:它也适用于threading.Thread()作为:

#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)"但引发错误:“Tcl_AsyncDelete:由错误线程删除的异步处理程序中止(核心转储)”

Screenshots of the first window and second window .第一个 window第二个 window的屏幕截图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM