为条目和文本 tkinter 标签设置相同的宽度 [英] Set the same width to an entry and text tkinter label

查看:33
本文介绍了为条目和文本 tkinter 标签设置相同的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个条目和一个文本标签放在另一个下面并具有相同的宽度.

I would like to put an entry and a text label one under the other and with the same width.

这是我的代码:

from tkinter import * 

root = Tk()

title = StringVar()
title_entry = Entry(root, textvariable=title, width=30)
title_entry.pack()


content_text = Text(root, width=30)
content_text.pack()

root.mainloop()

但是我的 2 个小部件的宽度不同.有什么解决办法吗?

But my 2 widgets don't have the same width. Any idea to solve it ?

推荐答案

小部件的大小不同可能是因为它们具有不同的默认字体.如果它们具有相同的字体和相同的宽度,则它们应该具有相同的自然宽度.但是,实际宽度会受到它们在窗口中的放置方式的影响,并且通常有充分的理由为这些小部件使用不同的字体.

The widgets are different sizes probably because they have different default fonts. If they have the same fonts and the same widths, they should have the same natural width. However, the actual width can be affected by how they are placed in the window, and there are often good reasons to use different fonts for these widgets.

在您的情况下,最简单的解决方案是让每个小部件在 x 轴上填充容器.这确保,无论它们的自然宽度如何,它们都会从边到边扩展以填充窗口:

The simplest solution in your case is to have each widget fill the container in the x axis. This makes sure that, regardless of their natural width, they will expand to fill the window from edge to edge:

title.pack(fill="x")
content_text.pack(fill="x")

如果这是您仅有的两个小部件,您需要更进一步并指定其他选项以获得适当的调整大小行为:

If these are your only two widgets you'll want to go a step further and specify additional options to get proper resize behavior:

title.pack(fill="x")
content_text.pack(fill="both", expand=True)

这篇关于为条目和文本 tkinter 标签设置相同的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆