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

Tkinter技术求助:缩小主窗口时保持底部标签始终可见

解决Tkinter底部标签窗口缩小时被隐藏的问题

你的代码问题在于没有给上方框架和底部标签设置合适的布局属性,导致窗口缩小后底部标签被挤压隐藏。通过调整pack方法的参数,就能让底部标签始终固定在窗口底部并保持可见:

修改后的完整代码

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry("200x200")
# 可选:设置最小窗口尺寸,防止缩到完全无法显示元素
# root.minsize(120,120)

frame1 = ttk.LabelFrame(root, text='Menu Bar', height=100)
frame2 = ttk.Frame(root)
frame3 = ttk.Frame(root)

# 让上方框架填充剩余空间并随窗口伸缩
frame1.pack(fill=tk.BOTH, expand=True)
frame2.pack(fill=tk.BOTH, expand=True)
frame3.pack(fill=tk.BOTH, expand=True)

button1 = ttk.Button(frame1, text='Button 1')
button2 = ttk.Button(frame1, text='Button 2')
button3 = ttk.Button(frame2, text='Button 3')
Label1 = ttk.Label(root, text="Bottom text: do not hide")

button1.pack(side=tk.LEFT)
button2.pack(side=tk.LEFT)
button3.pack()
# 固定底部标签,横向填充窗口,保持居中显示
Label1.pack(side=tk.BOTTOM, anchor=tk.CENTER, fill=tk.X)

root.mainloop()

关键修改说明

  • 给frame1、frame2、frame3的pack添加fill=tk.BOTH, expand=True:让这些框架占据窗口剩余空间,且能随窗口大小变化自动伸缩,避免挤压底部标签
  • 底部标签Label1的pack增加fill=tk.X:让标签横向填满窗口宽度,防止窗口变窄时标签内容被截断;anchor=tk.CENTER确保标签文字始终居中
  • 建议统一使用tk.前缀调用Tkinter属性(比如tk.LEFT而非LEFT),避免因全局导入导致的命名冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 14:36:17