在Tkinter中使用滚动条创建一个修复按钮

时间:2017-08-14 20:09:35

标签: python button tkinter scrollbar

我运行了一个基于滚动条here的示例。但是我想在框架上添加一个按钮(上一个),当滚动条向下移动时,此按钮仍然保留在那里,而其他内容随滚动条一起移动。

enter image description here

这是我的代码:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, root):

        tk.Frame.__init__(self, root)
        self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
        self.frame = tk.Frame(self.canvas, background="#ffffff")
        self.vsb = tk.Scrollbar(root, 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()

    def populate(self):

        button = tk.Button(self.frame, text='Prev')
        button.grid(row=0, columns=1)

        for row in range(1,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"))

if __name__ == "__main__":
    root=tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

任何人都可以帮我解决这个问题吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

您也可以使用pack()执行此操作,但我更喜欢使用grid()

您需要做的是将按钮放在第0行的root中,并将类框放在root的第1行。这应该可以解决你的问题。

使用grid()时,我们需要管理行和列的权重,以便在调整窗口大小时获得增长和缩小的行为。我们可以使用rowconfigute()columnconfigute()执行此操作。对于这个程序,我们需要配置root的权重。

要让按钮保持在左侧,我们可以使用sticky = "w"内的grid()来选择要坚持的一面。

看看下面的代码:

import tkinter as tk

class Example(tk.Frame):
    def __init__(self, root):

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

        self.vsb.grid(row = 1, column = 1, sticky = "nsew")
        self.canvas.grid(row = 1, column = 0, sticky = "nsew")
        self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame")

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

        self.populate()

    def populate(self):

        button = tk.Button(root, text='Prev')
        button.grid(row=0, columns=1, sticky = "w")

        for row in range(1,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"))

if __name__ == "__main__":
    root=tk.Tk()
    root.columnconfigure(0, weight = 1)
    root.rowconfigure(1, weight = 1)
    Example(root).grid(row = 1)
    root.mainloop()