Message控件用于显示多行文本且用户不可编辑的消息。Message 控件能够自动换行,对齐,并调整文本的尺寸使其适应给定的尺寸。
Message 控件通常用于显示简单的文本消息,它和Label的差异主要有三点,一是能自动换行来适应窗口的宽高比,二是对齐,三是能处理控制字符(control characters )和不可打印字符(non-printing characters)。
Message控件也非常简单,了解一下即可。
aspect
指定Message的宽高比,aspect ratio=100*width/height. 当aspect=100时表示w:h=1:1,当aspect=200时表示w:h=2:1,当aspect=50时表示w:h=1:2。如果指定了width属性,aspect属性就无效了。
from tkinter import Tk,Message
main_win = Tk()
main_win.title('渔道的listbox控件')
width = 300
height = 300
main_win.geometry(f'{width}x{height}')
# aspect,使用不同的aspect值
msg = Message(main_win, text='嘿,渔道', bg='white', fg='red')
# msg = Message(main_win, text='嘿,渔道', bg='white', fg='red', aspect=10)
# msg = Message(main_win, text='嘿,渔道', bg='white', fg='red', aspect=200)
# msg = Message(main_win, text='嘿,渔道', bg='white', fg='red', aspect=50)
msg.pack()
main_win.mainloop()
width
指定message中每一行文本的宽度
msg = Message(main_win, text='嘿,渔道', bg='white', fg='red', aspect=50, width=100)
justify
指定message中的文本对齐方式
# msg = Message(main_win, text='嘿,渔道\nhello,python\n我是一个粉刷匠', bg='white', fg='red', aspect=50, width=100, justify='center')
msg = Message(main_win, text='嘿,渔道\nhello,python\n我是一个粉刷匠', bg='white', fg='red', aspect=50, width=100, justify='right')
textvariable
var = StringVar()
msg = Message(main_win, textvariable=var, bg='white', fg='red')
msg.pack()
var.set('嘿,渔道\nhello,python\n我是一个粉刷匠')