我創建了一個創建二叉樹的類“節點”。(我知道我可以使用二叉樹模塊,但它是 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()
現在,我將代碼匯入另一個檔案,創建一個類的實體,添加一些節點并呼叫 view() 方法,它作業正常,但是 view() 方法之后的代碼在我關閉 tkinter 視窗之前不會運行。如何使 view() 之后的代碼在不關閉視窗的情況下運行?如果視窗無法更新,則可以。
我匯入和使用節點類的代碼:
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.
上述代碼的輸出: 鏈接到影像
我嘗試了谷歌搜索,還查看了以下 stackoverflow 帖子:
- 從 python 中的不同類呼叫 tkinter 應用程式,
- 一段代碼需要繼續……(Python with Tkinter)
- 匯入 Tkinter 檔案后如何繼續運行代碼?
- 很少有其他網站和指南...
但沒有任何幫助。請幫助/指導我。我是 StackOverflow 和 Python 的新手。
提前致謝 :)
uj5u.com熱心網友回復:
方法一:不更新視窗
你可以root.mainloop()用root.update(). 這將停止顯示對視窗的任何更改。只有在程式停止運行時,該視窗才會并始終關閉。
方法2:使用執行緒
您可以使用在另一個執行緒中import threading運行t1.view(): threading.Thread(target=t1.view).start(). 這可能會導致 Tcl_AsyncDelete 錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/407818.html
標籤:
上一篇:如何在頂層使用父類
下一篇:如何在標簽上填充影像的位置?
