如何在不使滚动条变灰的情况下移动滚动条画布/框架

时间:2014-12-13 22:32:50

标签: python canvas tkinter scrollbar

我正在为我的计算课程编写一些东西,我正在尝试将滚动条纳入其中,此刻我找不到任何真正解释为什么在移动窗口框架后,滚动条停止工作的网站(变为在移动窗口的画布后,画布变得非常小(不是我希望它的大小)。这是一些示例代码:

import Tkinter as tk
from Tkinter import *
master.geometry("800x600+300+150")


class Example(tk.Frame):
def main(self):

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

    self.vsb.pack(side="right", fill="y")
    self.hsb.pack(side="bottom", fill="x")
    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):
    vars1 = StringVar()
    vars1.set("junk")
    for i in range(100):
        tk.Entry(self.frame, text=vars1).grid(row=1*i, column=1)

def OnFrameConfigure(self, event):
    self.canvas.configure(scrollregion=self.canvas.bbox("all"))

dave3 = Example()
dave3.main()


button = Button(master, text="Hi").place(x=0, y=0)

mainloop()

如果我试图移动框架或画布发生我不想要的事情,我只是希望整个框架向下移动,以便按钮不会与画布条目重叠,滚动条仍然可以工作。如果有人能给出解决方案,我会非常感激,欢呼。

感谢Bryan Oakley用他的示例代码让我走到这一步。

1 个答案:

答案 0 :(得分:1)

好吧没关系我只想出来,我需要做的是创建另一个框架并将前一个框架放在那个框架内。在此之后,我只是将该帧配置为我需要它的大小。对于任何其他新手,这里是代码:

import Tkinter as tk
from Tkinter import *
master = Tk()
master.geometry("800x600+300+150")

class Example(tk.Frame):
    def main(self):

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

        self.vsb.pack(side="right", fill="y")
        self.hsb.pack(side="bottom", fill="x")
        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.framee.place(x=0, y=50, width=500, height=500)
        self.populate()

    def populate(self):
        vars1 = StringVar()
        vars1.set("junk")
        for i in range(100):
            tk.Entry(self.frame, text=vars1).grid(row=1*i, column=1)

    def OnFrameConfigure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

dave3 = Example()
dave3.main()


button = Button(master, text="Hi").place(x=0, y=0)

mainloop()