You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter自定义滚动框架未填满窗口时仍可滚动问题求助

Tkinter可滚动框架滚动异常问题求助

我参考网络教程与解答,尝试用Tkinter/ttk创建可滚动框架以存放支持增删行的表格,遇到一个未解决的问题:当框架还有剩余空间时,表格内容仍能滚动至框架底部;等内容填满框架后,滚动表现反而正常。

暂时无法制作动图,用以下截图说明:

  • 框架未填满,无需滚动:框架未填满,无需滚动
  • 表格仍可滚动:表格仍可滚动
  • 滚动停止时行组件位于框架底部:滚动停止时行组件位于框架底部

以下是我的ScrollableFrame类代码,参考网络标准示例:

class ScrollableFrame(ttk.Frame):

    def __init__(self, parent):

        super().__init__(parent)
        
        self.canvas = tk.Canvas(self)
        self.scrollbar = ttk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.scrollableFrame = ttk.Frame(self.canvas)
        self.canvas.configure(yscrollcommand=self.scrollbar.set)
        
        self.scrollableFrame.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
        self.bind("<Enter>", self.bind_to_mousewheel)
        self.bind("<Leave>", self.unbind_from_mousewheel)
        
        self.scrollbar.pack(side='right', fill='y')
        self.canvas.pack(side='top', expand=0, fill='x')
        self.canvas.create_window((0, 0), window=self.scrollableFrame)
        
    def bind_to_mousewheel(self, event):
        self.canvas.bind_all("<MouseWheel>", self.on_mousewheel)
        
    def unbind_from_mousewheel(self, event):
        self.canvas.unbind_all("<MouseWheel>")

    def on_mousewheel(self, event):
        self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")

表格行由父GUI添加至ScrollableFrame.scrollableFrame组件,我本以为这样能触发绑定到ScrollableFrame.canvas的<Configure>事件,恳请各位提供解决建议?

内容的提问来源于stack exchange,提问作者SPZHunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 09:48:13