带小部件的可滚动tkinter画布仅占框架的一半

时间:2019-07-12 13:40:49

标签: python-3.x tkinter tkinter-canvas

我尝试在tkinter中执行可滚动的canvas方法,但是从顶部开始,画布仅填充屏幕的一半,而将屏幕填充到侧面。

class Application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        tk.Tk.iconbitmap(self, default = "icon.ico")
        tk.Tk.wm_title(self, "name")
        tk.Tk.geometry(self, "1024x576")

        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)

        self.frames = {}

        for F in (Frame1, Frame2, ...):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(Frame1)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class Frame2(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff")
        self.frame = tk.Frame(self.canvas, background="#ffffff")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)
        self.vsb.pack(side="right", fill="y", expand=False)
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas.create_window((0,0), window=self.frame, anchor="nw", tags="self.frame")
        self.frame.bind("<Configure>", self.onFrameConfigure)

    for i in range(n):
        "a bunch of widgets".grid(column = 0, row = i)

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

1 个答案:

答案 0 :(得分:0)

grid不会自动调整窗口小部件的大小。

您可能会通知container(作为使用frame的{​​{1}}的父代)网格中的哪些行和列可以使用更多空间并调整窗口小部件的大小。 (不同的行/列可能具有不同的frame.grid

weight

或者您可以使用 container.rowconfigure(0, weight=1) container.columnconfigure(0, weight=1) for F in (Frame2, ): frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="news") 代替frame.pack()

frame.grid()