gpt4 book ai didi

python - 如何阻止 Tkinter 消息小部件调整大小?

转载 作者:太空狗 更新时间:2023-10-30 02:39:14 25 4
gpt4 key购买 nike

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

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()

最佳答案

更新:

您在这里有几个选项,但根据您的评论 小部件根据其内容调整大小,我希望它们都具有相同的宽度 我将使用 给出最适合您需要的示例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()

关于python - 如何阻止 Tkinter 消息小部件调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45046356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com