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

Tkinter Text组件宽度无法匹配两个Button组件总宽度的问题求助

Tkinter Text组件宽度无法匹配两个Button组件总宽度的问题求助

我现在在做一个Tkinter的GUI项目,遇到个头疼的问题:想让黑色的Text组件(message_entry)的宽度刚好等于两个横向排列的按钮的总宽度。我本来是通过屏幕尺寸的比例来计算组件宽度的,但在不同分辨率或者DPI的屏幕上,这个Text组件要么比两个按钮的总宽度宽,要么更窄,完全对不上。

⚠️ 重点提醒:我绝对不想使用expand=True或者fill这类布局参数,因为我不想让Text组件拉伸填满所有可用空间,就只想让它刚好匹配那两个按钮的总宽度而已!

下面是我目前的完整代码:

import tkinter as tk
from tkinter import ttk

root = tk.Tk(className="Chat")
root.title("Chat")

# 最大化窗口
root.wm_state('zoomed')

# 获取屏幕宽高
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

def proportion_height(p):
    return int(screen_height * p)

def proportion_box_width(p):
    box_width = int(screen_width * p)
    print("Box width is ", box_width)

    box_width = int(box_width / 2)
    print("Box width after devide is ", box_width)
    return box_width

# 比例计算函数
def proportion_width(p):
    return int(screen_width * p)

frame1 = tk.Frame(root, bg='white')
frame1.pack(side='bottom', padx=(proportion_width(0.22),0), pady=(proportion_height(0.1), 1), anchor='w')

frame2 = tk.Frame(root, bg='white')
frame2.pack(
            before=frame1,
            side='bottom',
            padx=(proportion_width(0.22),0),
            pady=(proportion_height(0), 5),
            anchor='w'
        )

# 容纳输入框和按钮的容器
frame_for_widgets = tk.Frame(root, bg='white')
frame_for_widgets.pack(
            padx=(proportion_width(0.22),0),
            anchor='w',
            before=frame2,
            pady=(0, proportion_height(0.02)),
            side='bottom',
        )

style = ttk.Style()
style.configure("def2.TButton", font=('Helvetica', 14,), background='white', fg='white', highlightthickness=1, highlightbackground="gray", borderwidth=1)

chat_with_pdf_button = ttk.Button(frame1, width=proportion_box_width(0.04), text="Chat with PDF", style="def2.TButton",)
chat_with_pdf_button.pack(side='left', expand=False, ipady=proportion_height(0.01))

# 总结功能按钮
summarize_button = ttk.Button(frame1, width=proportion_box_width(0.04), text="Chat with a YouTube video", style="def2.TButton")
summarize_button.pack(side='right', expand=False, padx=(proportion_height(0.01), 0), ipady=proportion_height(0.01))

letter_button = ttk.Button(frame2, width=proportion_box_width(0.04), text="Write a letter", style="def2.TButton")
letter_button.pack(side='left',expand=False, ipady=proportion_height(0.01))

blog_button = ttk.Button(frame2, width=proportion_box_width(0.04), text="Chat with a Blog Post",  style="def2.TButton")
blog_button.pack(side='right',expand=False, padx=(proportion_height(0.01), 0), ipady=proportion_height(0.01))

message_entry = tk.Text(
    frame_for_widgets,
    width=proportion_width(0.04),
    bg='black',
    insertbackground='black',
    fg='black',
    borderwidth=0,
    height=proportion_width(0.000001),
    spacing1=proportion_height(0.02),
    spacing3=proportion_height(0.02),
    font=('Helvetica', 14)
)

# 把输入框放到容器里
message_entry.pack(side='left', expand=False)

# 发送按钮
send_button = ttk.Button(frame_for_widgets)
send_button.pack(side='left', 
        expand=False,                       
        padx=(proportion_width(0.001),0),
        pady=(proportion_height(0.02), proportion_height(0.02)),)

send_button.pack_configure(expand=False)

style = ttk.Style()
style.configure("TMenubutton", background="white", borderwidth=0, font=('Helvetica', 14),)
menu_button = ttk.Menubutton(frame_for_widgets, text="Select", direction='above',)
menu_button.pack(side=tk.LEFT, pady=(proportion_height(0.02), proportion_height(0.02)), expand=False)

root.mainloop()

我的核心需求总结:

  • 无论用户的屏幕分辨率或DPI如何,message_entry的宽度必须完全等于chat_with_pdf_button和summarize_button这两个按钮的宽度之和
  • 严格禁用expand和fill布局属性,避免组件过度拉伸

真的很希望大家能给我出出主意,怎么才能实现这个效果呢?

备注:内容来源于stack exchange,提问作者user29255210

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 10:54:34