2012-12-20 93 views
2

我一直在試圖理解二叉樹的繪製。我發現了一個很好的啓動在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中的代碼。

+0

順便說一句,圖形碼是爲[Nodebox](http://nodebox.net/code/index.php/Home)。整潔的工作! – llimllib

回答

1

很難肯定地說,因爲我不能運行你的代碼,因爲缺少依賴關係,但如果我不得不猜測,我會說,這是因爲你不打包框架,它會擴大和合同。你說滾動條「落後」,但我敢打賭帆布也有同樣的問題。

嘗試修改線,其中你包幀是:

frame.pack(side="top", fill="both", expand=True) 
+0

這樣做! Thankyou –

+0

我是新來的。我會很樂意提供依賴關係,但涉及的代碼很多;太多的投入。有沒有某種方法來附加.zip或其他來提供完整的代碼? –

+0

@JohnSchroeder:不,沒有。 –