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

Tkinter记事本底部按钮不显示问题求助

解决Tkinter记事本底部按钮不显示的问题

问题原因

你的代码里Text组件使用默认的pack()方法时,会占据窗口内剩余的所有垂直空间,导致后续添加的按钮被挤出了窗口可视区域。而把按钮放在顶部时,Text会填充按钮下方的空间,所以按钮能正常显示。

修复方案

方案1:直接调整pack参数

修改Text和按钮的pack配置,让Text填充除底部按钮外的所有空间:

# 修改Text的pack参数
text.pack(fill=BOTH, expand=True)

# 按钮指定放在底部
fonts = ttk.Button(root, text='Size and Font', style='Accent.TButton')
fonts.pack(side=BOTTOM, pady=2)

方案2:用Frame管理底部工具栏(推荐,便于扩展)

创建专门的底部框架来存放按钮,布局更清晰,后续添加新按钮更方便:

# 创建底部工具栏框架
bottom_frame = ttk.Frame(root)
bottom_frame.pack(side=BOTTOM, fill=X, pady=2)

# 将按钮添加到底部框架
fonts = ttk.Button(bottom_frame, text='Size and Font', style='Accent.TButton')
fonts.pack(side=LEFT, padx=5)

# 让Text填充剩余空间
text.pack(fill=BOTH, expand=True)

完整修复后的代码

from tkinter import *
from tkinter import ttk

root = Tk()
root.title('Notepad')
root.iconbitmap('C:/notes.ico')
root.geometry("500x500")

root.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
root.tk.call("set_theme", "dark")

def change_theme():
    if root.tk.call("ttk::style", "theme", "use") == "azure-dark":
        root.tk.call("set_theme", "light")
    else:
        root.tk.call("set_theme", "dark")

style=ttk.Style()
style.theme_use('azure-dark')
style.configure("Vertical.TScrollbar", background="grey", bordercolor="black", arrowcolor="white")

scroll = ttk.Scrollbar(root, orient='vertical')
scroll.pack(side=RIGHT, fill='y')

text=Text(root, font=("Georgia, 24"), yscrollcommand=scroll.set, bg='#292929')
scroll.config(command=text.yview)

# 方案2的实现(推荐)
bottom_frame = ttk.Frame(root)
bottom_frame.pack(side=BOTTOM, fill=X, pady=2)
fonts = ttk.Button(bottom_frame, text='Size and Font', style='Accent.TButton')
fonts.pack(side=LEFT, padx=5)

text.pack(fill=BOTH, expand=True)

root.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 08:25:01