可滚动框架不填充画布

时间:2018-02-03 14:22:14

标签: python tkinter scrollbar tkinter-canvas

我使用Python 2.7并且我有一个可滚动的框架,其中画布不缩小以适合我想要滚动的框架。

我看了这个问题的答案,但是当我运行它时它不起作用: How to resize a scrollable frame to fill the canvas?

当我在画布内打印框架的宽度时,它会显示0。

我还在计算机上运行了这个问题答案的代码: Scrollable Frame does not resize properly using tkinter in Python 但它仍然会在标签的左侧显示白色画布,并且在删除标签时不会调整大小。

看起来像这样:

Canvas not resizing to fit scrollable frame

这是我的代码,基于这个问题的答案: Adding a scrollbar to a group of widgets in Tkinter

from Tkinter import *

class Scrollable_frame(Frame):

    def __init__(self, parent, title, values):

        self.parent = parent

        Frame.__init__(self, self.parent)

        self.canvas = Canvas(self, borderwidth=0, background="#ffffff")
        self.scrollbar = Scrollbar(self, command=self.canvas.yview)
        self.innerFrame = Radiobutton_frame(self.canvas,title,values)

        self.canvas.configure(yscrollcommand=self.scrollbar.set)

        self.canvas.grid(row=0, column=0, sticky= N+S)
        self.scrollbar.grid(row=0, column=1, sticky = N+S)
        self.canvas.create_window((0,0),window = self.innerFrame,anchor="nw")

        self.innerFrame.bind("<Configure>", self.set_canvas_scrollregion)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)


    def set_canvas_scrollregion(self, event):

        width = event.width - 4
        self.canvas.itemconfigure("self.innerFrame ", width=width)
        self.canvas.config(scrollregion=self.canvas.bbox("all"))


class Radiobutton_frame(LabelFrame):

    def __init__(self, parent, title, values):

        """            
        In: parent - Canvas
            title - String
            values - List of Int
        """

        self.radiobuttons = {}
        self.parent = parent

        self.selection = StringVar()
        self.selection.set("init")

        LabelFrame.__init__(self, self.parent, text = title)

        for value in values:
            self.add_radiobutton(value)


    def add_radiobutton(self, value):

        """
        Adds a radiobutton to the frame.

        In: item - String
        """

        # Associate to same variable to make them function as a group
        self.radiobuttons[value] = Radiobutton(master = self,
                                                variable = self.selection,
                                                text = value,
                                                value = value)
        self.radiobuttons[value].pack(anchor=W)

# Usage example

root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
scrollableFrame = Scrollable_frame(root, "Canvas not resizing", range(30))
scrollableFrame.grid(row=0, column=0, sticky=N+S+E+W)

if __name__ == '__main__':
    root.mainloop()

1 个答案:

答案 0 :(得分:1)

我认为上面问题的代码段不适合a Minimal, Complete, and Verifiable example,但至少它可以运行。

与以下相比,您有三个错误:How to resize a scrollable frame to fill the canvas?

其中最重要的是,在链接问题中,OP使用选项tags,而不是。替换:

self.canvas.create_window((0,0),window = self.innerFrame,anchor="nw")

使用:

self.canvas.create_window((0,0),window = self.innerFrame, anchor="nw", tags="my_tag")

另一个错误是你绑定了一个帧调整大小的事件而不是实际的Canvas'调整大小,也在Bryan's comment here中指出。替换:

self.innerFrame.bind("<Configure>", self.set_canvas_scrollregion)

使用:

self.canvas.bind("<Configure>", self.set_canvas_scrollregion)

最后,tkinter似乎不接受带标签的空格字符,替换:

self.canvas.itemconfigure("self.innerFrame ", width=width)

使用:

self.canvas.itemconfigure("my_tag", width=width)

最后,你应该:

from Tkinter import *

class Scrollable_frame(Frame):

    def __init__(self, parent, title, values):

        self.parent = parent

        Frame.__init__(self, self.parent)

        self.canvas = Canvas(self, borderwidth=0, background="#ffffff")
        self.scrollbar = Scrollbar(self, command=self.canvas.yview)
        self.innerFrame = Radiobutton_frame(self.canvas,title,values)

        self.canvas.configure(yscrollcommand=self.scrollbar.set)

        self.canvas.grid(row=0, column=0, sticky= N+S)
        self.scrollbar.grid(row=0, column=1, sticky = N+S)
        self.canvas.create_window((0,0),window = self.innerFrame,anchor="nw",
                                                                tags="my_tag")

        self.canvas.bind("<Configure>", self.set_canvas_scrollregion)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)


    def set_canvas_scrollregion(self, event):

        width = event.width - 4
        self.canvas.itemconfigure("my_tag", width=width)
        self.canvas.config(scrollregion=self.canvas.bbox("all"))


class Radiobutton_frame(LabelFrame):

    def __init__(self, parent, title, values):

        """            
        In: parent - Canvas
            title - String
            values - List of Int
        """

        self.radiobuttons = {}
        self.parent = parent

        self.selection = StringVar()
        self.selection.set("init")

        LabelFrame.__init__(self, self.parent, text = title)

        for value in values:
            self.add_radiobutton(value)


    def add_radiobutton(self, value):

        """
        Adds a radiobutton to the frame.

        In: item - String
        """

        # Associate to same variable to make them function as a group
        self.radiobuttons[value] = Radiobutton(master = self,
                                                variable = self.selection,
                                                text = value,
                                                value = value)
        self.radiobuttons[value].pack(anchor=W)

# Usage example

root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
scrollableFrame = Scrollable_frame(root, "Canvas not resizing", range(30))
scrollableFrame.grid(row=0, column=0, sticky=N+S+E+W)

if __name__ == '__main__':
    root.mainloop()