如何阻止Tkinter消息小部件自动调整大小?

4

我正在创建三个“消息小部件”,但它们似乎会根据内部内容调整宽度和高度,有没有办法防止这种情况发生?

from tkinter import *

root = Tk()
root.configure(background = 'Yellow')
root.geometry('500x400')

a = '{}'.format('The Elepthan is Big')
b = '{}'.format('The Bird')
c = '{}'.format('The Lion is big and ferocious, kills any animal')

msg = Message(root, text = a, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()


msg = Message(root, text = b, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()

msg = Message(root, text = c, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack()




root.mainloop()

是的,这是可能的。请提供您用于创建消息小部件的代码示例,我们可以告诉您如何避免调整大小。也许您需要在网格中添加一些权重。 - Mike - SMT
1
请提供一个 [mcve],以说明您遇到的问题。 - Bryan Oakley
@BryanOakley 完成,我希望所有的消息小部件具有相同的宽度,但它们会根据内容进行调整大小 - espktro 8分钟前 - espktro
我想知道为什么你使用 message 而不是 label,是为了保持消息的外观吗?另一个原因是因为根据 tkinter 的说法:"""Message widget to display multiline text. Obsolete since Label does it too.""" - Mike - SMT
@SierraMountainTech,那Label也可以做到吗?我在看一个2005年的教程,不知道Label也能显示多行....那么如何使用Labels实现呢?如何防止Labels调整大小? - espktro
显示剩余3条评论
2个回答

3

更新:

您有几个选项,但根据您的评论小部件根据其内容调整大小,我希望它们都具有相同的宽度,我将给出最适合您需求的示例,使用pack()几何管理器。

对于pack,最简单的选项至少是执行pack(fill = BOTH)

实现您想要的最快速的方法是使用框架。该框架将调整为最大文本,并在所有消息小部件上使用pack(fill = BOTH)将较小的小部件扩展到框架的大小,也是最大小部件的大小。

from tkinter import *

root = Tk()
root.configure(background = 'Yellow')
root.geometry('500x400')

a = '{}'.format('The Elepthan is Big')
b = '{}'.format('The Bird')
c = '{}'.format('The Lion is big and ferocious, kills any animal')

frame = Frame(root)
frame.pack()

msg = Message(frame, text = a, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack(fill=BOTH)


msg = Message(frame, text = b, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack(fill=BOTH)

msg = Message(frame, text = c, width=300, justify='left')
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3)
msg.pack(fill=BOTH)

root.mainloop()

固定宽度怎么样,而不是父级宽度?框架= 400消息= 270...无论是使用网格还是包装,我只需要所有消息小部件具有相同的宽度(270),但不是父小部件的宽度(400)。 - espktro
有一些选项。其中一个是禁用框架的网格调整大小,以便强制框架保持固定宽度。我知道还有其他选项,但我需要考虑一下。我会看看能找到什么。 - Mike - SMT

2

您可以通过找到最长消息的宽度,然后使用Message小部件的padx选项将所有消息居中在该长度值中。这是一个例子:

from tkinter import *
import tkinter.font as tkFont

root = Tk()
root.configure(background='Yellow')
root.geometry('500x400')

a = 'The Elepthan is Big'
b = 'The Bird'
c = 'The Lion is big and ferocious, kills any animal'
messages = a, b, c

msgfont = tkFont.Font(family='times', size=9)
maxwidth = max(msgfont.measure(text) for text in messages)  # get pixel width of longest one

for text in messages:
    msg = Message(root, text=text, width=maxwidth, justify='left')
    padx = (maxwidth-msgfont.measure(text)) // 2  # padding needed to center text
    msg.config(bg='lightgreen', relief=RIDGE, font=msgfont, padx=padx, pady=-2, borderwidth=3)
    msg.pack()

root.mainloop()

Results:

enter image description here


使用框架并使用 pack(fill=BOTH) 允许一切自动调整大小,这样不是更简单吗? - Mike - SMT
@Sierra:是的,如果将所有内容放在一个框架内是可以接受的——这取决于整体设计和其他情况。我回答中展示的方法可能更加复杂,但它展示了如何在不这样做的情况下控制“Message”小部件(以及如何计算所需的值)。 - martineau
1
谢谢你的解释 :) - Mike - SMT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接