框架内的Tkinter Scrollbar Widget

时间:2017-10-27 08:54:43

标签: python tkinter scrollbar

我需要将一个滚动条添加到一个框架中,该框架应与窗口内的其他框架并排。我只想将滚动条添加到该特定帧,但我遇到了麻烦。我已经读过需要使用tk.Canvas来容纳滚动条小部件,但我仍然弄错了。

Click here for Demo with STR_TO_DATE() function。我想只在左框架上滚动条。

这是我的示例代码:

import tkinter as tk

class Example:
    def __init__(self, master):
        self.master = master

        self.canvas = tk.Canvas(master, borderwidth=0, background="#ffffff")
        self.frame = tk.Frame(self.canvas, background="#ffffff")
        self.vsb = tk.Scrollbar(master, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)

        self.vsb.pack(side="right", fill="y")
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas.create_window((4,4), window=self.frame, anchor="nw", 
                                  tags="self.frame")

        self.frame.bind("<Configure>", self.onFrameConfigure)

        self.populate()


        self.frame1 = tk.Frame(master, background="#ffffff",height=200,width=200)
        self.frame1.pack()

    def populate(self):
        '''Put in some fake data'''
        for row in range(100):
            tk.Label(self.frame, text="%s" % row, width=3, borderwidth="1", 
                     relief="solid").grid(row=row, column=0)
            t="this is the second column for row %s" %row
            tk.Label(self.frame, text=t).grid(row=row, column=1)

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

root=tk.Tk()
app = Example(root)
root.mainloop()

1 个答案:

答案 0 :(得分:-1)

如果我是你,我强烈建议您查找tkinter的Grid布局管理器,以便您可以指定要让窗口小部件完全打开的行和列。您甚至可以指定扩展和缩小行为。