【问题标题】:Tkinter refresh/update scrollbar to fit canvas contentTkinter 刷新/更新滚动条以适应画布内容
【发布时间】:2020-04-11 18:56:12
【问题描述】:

在画布中加载新框架后,滚动条不适合内容。
如果我手动调整窗口大小,滚动条会适合内容。
加载框架时如何强制滚动条适应内容?
这是重现我的问题的代码:

from tkinter import *

class MyApp:
    def __init__(self):
        self.root = Tk()
        self.mainframe = Frame(self.root)
        self.mainframe.pack(fill=BOTH, expand=True)
        self.mainframe.columnconfigure(0, weight=1)

        self.contentCanvas = Canvas(self.mainframe)
        self.contentCanvas.grid(sticky=N + S + W + E)

        self.horizontalBar = Scrollbar(self.mainframe, orient=HORIZONTAL)
        self.horizontalBar.grid(rowspan=2, sticky=W + E + S)
        self.horizontalBar.config(command=self.contentCanvas.xview)
        self.contentCanvas.config(xscrollcommand=self.horizontalBar.set)

        self.contentFrame = Frame(self.contentCanvas)
        self.content = Frame(self.contentFrame)
        self.content.grid()
        Button(self.content, text="Load Long Content", command=self.loadLongContent).grid()

        self.contentCanvas.bind("<Configure>", lambda e: self.contentCanvas.configure(scrollregion=self.contentFrame.bbox("all")))
        self.contentCanvas.create_window((0, 0), window=self.contentFrame, anchor="nw")


    def run(self):
        self.root.mainloop()

    def loadContentFrame(self, frame):
        self.content.grid_forget()
        self.content.destroy()
        self.content = frame
        self.content.grid()

    def loadLongContent(self):
        longFrame = Frame(self.contentFrame)
        for i in range(100):
            Label(longFrame, text=i*i).grid(row=0, column=i)
        self.loadContentFrame(longFrame)

if __name__ == "__main__":
    app = MyApp()
    app.run()

图片:
加载新内容之前:

加载新内容后:

稍微调整窗口大小后:

【问题讨论】:

  • 如果调整大小触发了更正,您可以调用:self.root.geometry("500x500") 强制调整大小,只需调整合适的高度和稍微增加宽度

标签: canvas tkinter scrollbar


【解决方案1】:

我想出了一个可行的解决方案(基于 Helder Sepulvedas 的想法)。
我在 init 中添加了这个:
self.expand = False
这是在 loadFrame 中添加的:

        width = self.root.winfo_width()
        height = self.root.winfo_height()
        if self.expand:
            self.root.geometry('{width}x{height}'.format(width=width + 1, height=height + 1))
            self.expand = False
        else:
            self.root.geometry('{width}x{height}'.format(width=width - 1, height=height - 1))
            self.expand = True

窗口大小不变的解决方案仍然很好。
另外我建议在init中设置root的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 2016-10-22
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多