我一直在試圖理解二叉樹的繪製。我發現了一個很好的啓動在Tkinter畫布滾動條不遵循過去某個點的框架
http://billmill.org/pymag-trees/
提供的代碼使用圖形調用,沒有任何Python庫我認識的一部分,所以我已經轉換圖形調用Tkinter的和有運行,除了一個意外的代碼行爲。當我調整框架的大小(使其變大)時,某些點的滾動條會留下。當它重新調整大小時,他們不會留在框架中。下面是代碼:
from tkinter import *
from gen import Tree
from demo_trees import trees
from knuth import layout
r = 30
rh = r*1.5
rw = r*1.5
def drawt(canvas, root, depth):
canvas.create_oval(root.x * rw, depth * rh,
root.x * rw + r, depth * rh + r,
fill = 'white',
width = 2)
for child in root.children:
drawt(canvas, child, depth+1)
def drawconn(canvas, root, depth):
for child in root.children:
canvas.create_line(root.x * rw + (r/2), depth * rh + (r/2),
child.x * rw + (r/2), (depth+1) * rh + (r/2),
width = 2)
drawconn(canvas, child, depth+1)
def main():
root = Tk()
# Create the main frame. The frame will include a
# scrollable canvas.
frame = Frame(root, width=500, height=309, relief=SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
# Add scroll bars
xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=E+W)
yscrollbar = Scrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=N+S)
# Add the canvas
canvas = Canvas(frame, width=500, height=300,
scrollregion=(-20, -20, 500, 300),
xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
xscrollbar.config(command=canvas.xview)
yscrollbar.config(command=canvas.yview)
frame.pack()
# Now draw the tree
t = layout(trees[2])
drawconn(canvas, t, 0)
drawt(canvas, t, 0)
root.mainloop()
if __name__ == '__main__':
main()
繪圖正常滾動,直到幀被調整,一切都很好(大)誰能告訴我,爲什麼滾動條不與幀中移動?
如果有人有興趣,完整的樹形圖代碼來自上面的鏈接,並對應於下載提供的figure2.py中的代碼。
順便說一句,圖形碼是爲[Nodebox](http://nodebox.net/code/index.php/Home)。整潔的工作! – llimllib