我寫了一個腳本,它接受格式的檔案
handgun: bullets magazines
bullets: labor ore
magazines:
ore:
labor:
bombs: ore
turret: bullets
串列中的每一行代表一個“父”節點及其“子”節點串列。到目前為止,我已經撰寫了一個腳本,使這些節點像樹一樣列印出來,現在我想嘗試在這些節點之間畫線/箭頭。我當前的python腳本如下:
from ast import Delete
from dataclasses import replace
from os import link, remove
from tkinter import Canvas, Frame
import string
import tkinter as tk
import re
def fileReadIn():
f = open("list.txt", "r")
links = []
for line in f:
if re.search('.*:.*', line) != None:
parentAndChild = re.split(':', line)
childStr = parentAndChild[1]
childStr = childStr.replace('\n', '')
newChilStr = childStr.split(' ')
newChilStr.remove('')
newChilStr.remove('')
parentAndChild[1] = newChilStr
links.append(parentAndChild)
f.close()
return links
#read in the masterlist from the file as a list, and links as dictionary, then and
generate the tree
links = fileReadIn()
#open the gui window
window = tk.Tk()
window.geometry('800x800')
cur_row = []
pos_x = 300
pos_y = 0
#generate the top row
for i in links:
isChild = False
#make a node for the parent
nodeparent = tk.Label(text=str(i[0]), background='black', name=str(i[0]))
#check to see if node is a child of any other node - if not, place at top of window
for m in links:
for k in m[1]:
if str(k) == str(i[0]):
isChild = True
if isChild == False:
nodeparent.place(x=pos_x, y=pos_y)
pos_x = 80
cur_row.append(str(i[0]))
#make the rest of the rows
treeFinished = False
pos_y = 80
pos_x = 300
children = []
temp_row = []
while treeFinished == False:
treeFinished = True
for i in cur_row:
for j in links:
if i == j[0]:
if len(j[1]) != 0:
treeFinished = False
for i in cur_row:
for j in links:
if i == j[0]:
children = j[1]
for k in children:
if k in temp_row:
continue
else:
node = tk.Label(text=k, background='black', name=k).place(x=pos_x, y=pos_y)
temp_row.append(k)
pos_x = 80
for i in temp_row:
for j in temp_row:
for k in links:
if j == k[0]:
for m in k[1]:
if i == m:
window.children[i].destroy()
temp_row.remove(i)
cur_row = temp_row
temp_row = []
pos_y = 80
pos_x = 300
window.mainloop()
我試過使用畫布來做 create_line 但沒有成功。也許我只是遺漏了一些明顯的東西,但是有人對如何解決這個問題有任何建議嗎?
uj5u.com熱心網友回復:
更新:我解決了這個問題!事實證明,我只需要將畫布縮放到與我的視窗相同的大小。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/518303.html
上一篇:tkinter把東西放在一起
