如何将滚动条应用到画布中的框架

发布于 2024-12-04 15:22:23 字数 2163 浏览 0 评论 0 原文

我很难在 tkinter 中实现漂亮的 GUI 布局。我的目标是左侧的布局,但没有比右侧的布局更进一步:

enter image description在这里>>
<img src=

我感觉完全迷失在所有不同的布局管理器中。到目前为止,这是我的代码:

from Tkinter import *

class ScrolledCanvas:
    def __init__(self, master, width=500, height=350):
        Label(master, text="top of master frame").pack(side=TOP)
        self.control=Frame(master)
        self.control.pack(side=BOTTOM, fill=X, padx=2)
        Label(self.control, text="bottom of master frame").pack()
        self.grid = Frame(master, bd=2, bg="red")
        self.canvas = Canvas(master, relief=SUNKEN, borderwidth=2, 
                             scrollregion=('-11c', '-11c', '50c', '20c')) 
        self.vscroll = Scrollbar(master, command=self.canvas.yview) 
        self.canvas.configure(yscrollcommand=self.vscroll.set) 

        self.grid.pack(expand=YES, fill=BOTH, padx=1, pady=1)
        self.grid.rowconfigure(0, weight=1, minsize=0)
        self.grid.columnconfigure(0, weight=1, minsize=0)

        self.canvas.grid(padx=1, in_=self.grid, pady=1, row=0,
                         column=0, rowspan=1, columnspan=1, sticky='news')
        self.vscroll.grid(padx=1, in_=self.grid, pady=1, row=0,
                          column=1, rowspan=1, columnspan=1, sticky='news')
        self.oldFill = None
        self.canvas = self.fillCanvas(self.canvas)

    def fillCanvas(self, theCanvas):
        for i in range(10):
            frame = Frame(theCanvas)
            Label(frame, text="text for inner frame (%s)" % i).pack(side=TOP)
            Button(frame, text="button %s.1" % i).pack(side=LEFT)
            Button(frame, text="button %s.2" % i).pack(side=LEFT)
            frame.pack()
        return theCanvas

if __name__ == '__main__':
    root = Tk()
    scroll = ScrolledCanvas(root)
    root.mainloop()

由于某种原因,滚动区域扩展到完整大小,使所有滚动条操作都过时(此代码基于滚动条示例,其中画布填充了一些矩形,并且所有内容都滚动得非常好)。我可能错过了一些关于调整大小的基本知识,因为大多数组件似乎完全忽略了它们的固定宽度和高度。

我正在寻求有关如何让这个示例与 tkinter 一起使用的帮助。我知道还有其他布局技术,并且我知道其他模块提供开箱即用的可滚动小部件。但除非完全不可避免,否则我真的很喜欢坚持这个例子,到目前为止我至少花了 3 个晚上......

I'm having a really hard time trying to achieve a decdent GUI layout in tkinter. I'm aiming for the layout on the left, but didn't get any further than the layout on the right:

enter image description here
enter image description here

I feel completely lost in all that different layout managers. This is my code so far:

from Tkinter import *

class ScrolledCanvas:
    def __init__(self, master, width=500, height=350):
        Label(master, text="top of master frame").pack(side=TOP)
        self.control=Frame(master)
        self.control.pack(side=BOTTOM, fill=X, padx=2)
        Label(self.control, text="bottom of master frame").pack()
        self.grid = Frame(master, bd=2, bg="red")
        self.canvas = Canvas(master, relief=SUNKEN, borderwidth=2, 
                             scrollregion=('-11c', '-11c', '50c', '20c')) 
        self.vscroll = Scrollbar(master, command=self.canvas.yview) 
        self.canvas.configure(yscrollcommand=self.vscroll.set) 

        self.grid.pack(expand=YES, fill=BOTH, padx=1, pady=1)
        self.grid.rowconfigure(0, weight=1, minsize=0)
        self.grid.columnconfigure(0, weight=1, minsize=0)

        self.canvas.grid(padx=1, in_=self.grid, pady=1, row=0,
                         column=0, rowspan=1, columnspan=1, sticky='news')
        self.vscroll.grid(padx=1, in_=self.grid, pady=1, row=0,
                          column=1, rowspan=1, columnspan=1, sticky='news')
        self.oldFill = None
        self.canvas = self.fillCanvas(self.canvas)

    def fillCanvas(self, theCanvas):
        for i in range(10):
            frame = Frame(theCanvas)
            Label(frame, text="text for inner frame (%s)" % i).pack(side=TOP)
            Button(frame, text="button %s.1" % i).pack(side=LEFT)
            Button(frame, text="button %s.2" % i).pack(side=LEFT)
            frame.pack()
        return theCanvas

if __name__ == '__main__':
    root = Tk()
    scroll = ScrolledCanvas(root)
    root.mainloop()

For some reason the scrollregion expands to full size, making all scrollbar actions obsolete (This code bases on a scrollbar example where the canvas is filled with some rectangles, and everything scrolls really nice). I probably missed something fundamental about resizing, because most components seem to completely ignore their fixed width and height.

I'm looking for help on how to get this example to work with tkinter. I know that there are other layout technologies, and I know that other modules offer scrollable widgets out of the box. But unless completely unavoidable I really like to stick to this example, which cost me at least 3 nights so far...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2024-12-11 15:22:23

您不能将小部件打包(或网格或放置)到画布中并期望它们滚动。相反,您必须使用 create_window 方法。

You cannot pack (or grid, or place) widgets into the canvas and expect them to scroll. Instead, you must use the create_window method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文