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

Tkinter父窗口放大时PandasTable框架上方留白问题咨询

问题:Tkinter窗口放大时按钮与PandasTable框架间出现空白的解决方法

原始代码

# Class called by createWidgets

class dispRadio(Frame):
    def __init__(self, parent=None, **options):
        Frame.__init__(self, parent, **options)
        self.parent = parent
        self.pack(expand=NO, side=TOP)
        
        # other stuff done here
 
# Class called by main body

class NewMenu(Frame):                                   # an extended frame
    def __init__(self, parent=None):    # attach to top-level
        self.parent = parent
        Frame.__init__(self, parent)
        self.pack(side=TOP, expand=YES, fill=BOTH)
        self.main = self.master
        
        # other stuff done here, e.g. set variable for pandastable dataframe
        
        self.createWidgets()                       # attach frames/widgets
        
        self.PandasFrame = Frame(self.main, highlightbackground="green", highlightthickness=3) 
        self.PandasFrame.pack(side=TOP, fill=BOTH, expand=YES)
        
        self.pt = Table(self.PandasFrame, dataframe = category_receipts, width=1500, maxcellwidth=1500)
        self.pt.show()   
        
    def createWidgets(self):    
        self.mainWin = dispRadio(self, highlightbackground="blue", highlightthickness=1)
        self.smallWin = dispRadio2(self, highlightbackground="red", highlightthickness=1)

# Main Body

root = Tk()
nm = NewMenu(root)

root.mainloop()

问题描述

上述代码创建了按钮控件及包含PandasTable的框架。拖动主窗口底部放大时,PandasTable框架底部会增加显示行数,但按钮控件与PandasTable框架之间出现空白区域。期望PandasTable框架顶部保持在按钮控件下方,仅底部扩展,请问为何父窗口放大时,按钮与PandasTable框架间会出现空白而非被PandasTable填充?

已尝试调整pack参数(fill、expand、side等)及框架打包顺序,也查阅了PandasTable文档,但未找到解决方法。

原因分析

核心问题是PandasFrame的父容器设置错误:你将PandasFrame的父容器设为self.main(即根窗口root),但按钮所在的dispRadio控件是打包在NewMenu这个Frame内部的。此时NewMenu和PandasFrame是同级组件,都直接挂载在根窗口下。当窗口放大时,NewMenu的expand=YES, fill=BOTH会占据部分可用空间,PandasFrame在它下方也尝试扩展,最终导致两者之间出现空白。

解决方案

把PandasFrame的父容器改为self(即NewMenu自身),让它和按钮控件处于同一个父容器内,保证按钮在上方固定,PandasFrame在下方填充剩余空间。同时保持按钮的pack参数expand=NO,避免按钮占用多余空间。

修改后的关键代码片段:

class NewMenu(Frame):
    def __init__(self, parent=None):
        self.parent = parent
        Frame.__init__(self, parent)
        self.pack(side=TOP, expand=YES, fill=BOTH)
        self.main = self.master
        
        # other stuff done here
        
        self.createWidgets()
        
        # 修改父容器为self(NewMenu),而非self.main
        self.PandasFrame = Frame(self, highlightbackground="green", highlightthickness=3) 
        self.PandasFrame.pack(side=TOP, fill=BOTH, expand=YES)
        
        self.pt = Table(self.PandasFrame, dataframe = category_receipts, width=1500, maxcellwidth=1500)
        self.pt.show()   

调整后,窗口放大时NewMenu会填满根窗口,内部按钮固定在顶部,PandasFrame会占据按钮下方的所有剩余空间,不会出现空白区域。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 18:22:53