2013-02-18 102 views
0

我得到了下面的代碼,其中包含一個畫布內的列表框,我試圖把滾動條只放在列表框中,但我得到的是滾動條總是設置在整個形式。你能幫我把滾動條放在列表框中而不是整個畫布上。Tkinter Python在畫布中的列表框內管理滾動條

def __init__(self): 
    self.form = Tk() 
    self.form.title("Admin"); 
    self.form.geometry('608x620+400+50') 
    self.form.option_add("*font",("Monotype Corsiva",13)) 
    self.form.overrideredirect(True) 
    self.form.resizable(width=FALSE, height=FALSE) 
    self.canvas = Canvas(self.form) 
    self.canvas.pack(expand=YES,fill=BOTH) 
    self.photo = PhotoImage(file='src/back3.gif') 
    self.canvas.create_image(-7,-8,image=self.photo,anchor=NW) 

    self.scrollbar = tk.Scrollbar(self.canvas, orient="vertical") 
    self.lb = tk.Listbox(self.canvas, width=78, height=15,yscrollcommand=self.scrollbar.set) 
    self.lb.place(relx=0.04,rely=0.17) 
    self.scrollbar.config(command=self.lb.yview) 
    self.scrollbar.pack(side="right", fill="none") 

回答

1

我建議你創建一個Frame包裹ListboxScrollbar。在這裏,我寫了一個類來做同樣的事,它不適合你的代碼 - 我使用grid()而不是pack() - ,但你明白了。

class ScrollableListbox(tk.Listbox): 
    def __init__(self, master, *arg, **key): 
     self.frame = tk.Frame(master) 
     self.yscroll = tk.Scrollbar(self.frame, orient=tk.VERTICAL) 
     tk.Listbox.__init__(self, self.frame, yscrollcommand=self.yscroll.set, *arg, **key) 
     self.yscroll['command'] = self.yview 

    def grid(self, *arg, **key): 
     self.frame.grid(*arg, **key) 
     tk.Listbox.grid(self, row=0, column=0, sticky='nswe') 
     self.yscroll.grid(row=0, column=1, sticky='ns')