我正在创建三个“消息小部件”,但它们似乎根据内部内容调整宽度和高度的大小,是否可以防止这种情况?
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()
Run Code Online (Sandbox Code Playgroud)
更新:
您在这里有几个选项,但根据您的评论,widgets resize according to their content, I want them all with same width
我将使用几何管理器给出最适合您需求的示例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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2917 次 |
最近记录: |