2013-07-27 38 views
2

我在筆記本小部件中有3個選項卡,並且我想在第3個選項卡上有可滾動的框架。我設置了一個畫布和一個滾動條,併爲它們設置了所有的命令以進行交互,但它不起作用。我究竟做錯了什麼?完整的可運行代碼如下:爲什麼我的tkinter滾動條不工作?

import subprocess 
from Tkinter import * 
from ttk import * 
import piper as Piper 
import sqlite3 



def confCanvas(event): 
    global viewKeysCanvas 
    print "ConfCanvasEvent\n"; 
    viewKeysCanvas.configure(scrollregion=viewKeysCanvas.bbox("all")) 



root =Tk() 

sizex = 800 
sizey = 500 
posx = 100 
posy = 100 
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy)) 

root.title('Scroll Test') 

note = Notebook(root) 

tab1 = Frame(note) 
tab2 = Frame(note) 
tab3 = Frame(note) 

tab1.pack() 
tab2.pack() 
tab3.pack(fill=BOTH, expand=True) 



#tab1 
printGroup = Frame(tab1) 
Label(printGroup, text="Test label1").pack() 
printGroup.pack() 

#tab2 
bulkGroup = Frame(tab2) 
Label(bulkGroup, text="Test label2").pack() 
bulkGroup.pack() 


#tab3 
vkFrame = Frame(tab3) 
viewKeysCanvas = Canvas(vkFrame) 
viewKeysGroup = Frame(viewKeysCanvas) 
viewKeysScrollbar = Scrollbar(vkFrame, orient="vertical", command=viewKeysCanvas.yview) 
viewKeysCanvas.configure(yscrollcommand=viewKeysScrollbar.set) 

viewKeysScrollbar.pack(side=RIGHT, fill=Y) 
viewKeysCanvas.pack(fill="both", expand=True) 
viewKeysCanvas.create_window((0,0), window=tab3) 
vkFrame.bind("<Configure>",confCanvas) 
vkFrame.pack() 

for x in range(0, 9): 
    aKeyGroup = LabelFrame(viewKeysGroup, text="number: "+str(x)) 
    buttonFrame = Frame(aKeyGroup) 
    Button(buttonFrame, text="Action 1").pack(padx=10, side=LEFT) 
    Button(buttonFrame, text="Action 2").pack(padx=10, side=LEFT) 
    Label(aKeyGroup, text="Public key: ").pack(side=TOP) 
    Label(aKeyGroup, text="Private key: ").pack(side=TOP) 
    buttonFrame.pack(padx=10, pady=10)  
    aKeyGroup.pack() 



viewKeysGroup.pack(padx=10, pady=10) 


note.add(tab1, text = "Test tab 1") 
note.add(tab2, text = "Test tab 2") 
note.add(tab3, text = "Test tab 3") 


note.pack(expand=True, fill=BOTH) 


root.mainloop() 

我正在使用Python 2.7.3在Debian上使用LXDE。我是一名經驗豐富的程序員,但我是Python的新手,所以請讓我知道如果我做的任何事情都是錯誤的。謝謝你的幫助!

回答

1

爲了讓你的代碼的修改最少的工作, 變化

viewKeysCanvas.create_window((0,0), window=tab3) 

viewKeysCanvas.create_window((0,0), window=viewKeysGroup) 

,並刪除

# viewKeysGroup.pack(padx=10, pady=10) 

我發現了這一點由startin開始g從this example,然後修改它看起來更像您的代碼。由於我逐漸修改它,所以我總是有一個可以滾動的工作畫布。當GUI看起來差不多時,我簡單地比較了不同的線條。以下是我結束的代碼。

對不起,我無法解釋爲什麼這些更改是必要的。我想改變tab3viewKeysGroup是合理的,但爲什麼你不應該包裝viewKeysGroup超出了我。


import Tkinter as tk 
import ttk 

class Tab1(tk.Frame): 
    def __init__(self, root): 
     tk.Frame.__init__(self, root) 
     tk.Label(self, text="Test label1").pack() 

class Tab2(tk.Frame): 
    def __init__(self, root): 
     tk.Frame.__init__(self, root) 
     tk.Label(self, text="Test label2").pack() 

class Tab3(tk.Frame): 
    def __init__(self, root): 
     tk.Frame.__init__(self, root) 
     self.viewKeysCanvas = tk.Canvas(self) 
     self.viewKeysGroup = tk.Frame(self.viewKeysCanvas) 
     self.viewKeysScrollbar = tk.Scrollbar(self, orient="vertical", 
           command=self.viewKeysCanvas.yview) 
     self.viewKeysCanvas.configure(yscrollcommand=self.viewKeysScrollbar.set) 
     self.viewKeysScrollbar.pack(side="right", fill="y") 
     self.viewKeysCanvas.pack(fill="both", expand=True) 
     self.viewKeysCanvas.create_window(0, 0, window=self.viewKeysGroup, anchor="nw") 
     self.viewKeysGroup.bind("<Configure>", self.on_frame_configure) 
     self.populate() 

    def populate(self): 
     for x in range(0, 9): 
      aKeyGroup = tk.LabelFrame(self.viewKeysGroup, text="number: " + str(x)) 
      buttonFrame = tk.Frame(aKeyGroup) 
      tk.Button(buttonFrame, text="Action 1").pack(padx=10, side="left") 
      tk.Button(buttonFrame, text="Action 2").pack(padx=10, side="left") 
      tk.Label(aKeyGroup, text="Public key: ").pack(side="top") 
      tk.Label(aKeyGroup, text="Private key: ").pack(side="top") 
      buttonFrame.pack(padx=10, pady=10) 
      aKeyGroup.pack() 

    def on_frame_configure(self, event): 
     """Reset the scroll region to encompass the inner frame""" 
     self.viewKeysCanvas.configure(scrollregion=self.viewKeysCanvas.bbox("all")) 

root = tk.Tk() 
sizex, sizey, posx, posy = 800, 500, 100, 100 
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy)) 
root.title('Scroll Test') 

note = ttk.Notebook(root) 
tab1 = Tab1(note) 
tab2 = Tab2(note) 
tab3 = Tab3(note) 

note.add(tab1, text="Test tab 1") 
note.add(tab2, text="Test tab 2") 
note.add(tab3, text="Test tab 3") 

note.pack(expand=True, fill="both") 
root.mainloop() 
+0

完美,這正是我需要的。非常感謝你把所有的東西都放進適當的課堂!你真棒! – user1558646