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

Tkinter Frame无法填充窗口剩余空间问题求助

问题解决:让Tkinter的Frame填充窗口剩余区域

你的问题出在标题栏Frame的pack配置上——你给title_bar设置了expand=True,这会让它抢占窗口中额外的垂直空间,导致下方的mainframe只能分到剩下的部分。

修改方法很简单,把title_bar.pack里的expand=True改成expand=False:

title_bar.pack(anchor=N, expand=False, fill=X)

这样标题栏只会占据自身需要的高度,剩下的所有空间都会交给设置了expand=True, fill=BOTH的mainframe,它就能完全填充窗口剩余区域了。

完整修改后的代码:

from tkinter import *

class CredsWindow:

    def __init__(self, root):

        root.title("Greenbox")
        root.geometry("300x200")
        root.attributes('-topmost', True)

        self.drag_offsetx = IntVar()
        self.drag_offsety = IntVar()

        def drag_window_start(v):
            self.drag_offsetx = v.x
            self.drag_offsety = v.y

        def drag_window(v):
            x = root.winfo_pointerx()-self.drag_offsetx
            y = root.winfo_pointery()-self.drag_offsety
            root.geometry(f'+{x}+{y}')

        title_bar = Frame(root, bg="#252525")
        # 修改expand参数为False
        title_bar.pack(anchor=N, expand=False, fill=X)
        title_bar.bind("<Button-1>", drag_window_start)
        title_bar.bind("<B1-Motion>", drag_window)

        title_label = Label(title_bar, text="Greenbox", bg="#252525", fg="white")
        title_label.pack(side=LEFT, pady=3, padx=2)

        close_button = Button(title_bar, text="X", bg="#252525", fg="white", relief="flat", command=root.quit)
        close_button.pack(side=RIGHT, pady=3, padx=2)

        
        mainframe = Frame(root, bg='#171717')
        mainframe.pack(side=TOP, expand=True, fill=BOTH)


root = Tk()
CredsWindow(root)
root.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 20:57:53