python tkinter 使用(七)
python tkinter 使用(七)
本篇文章主要讲下tkinter 中的message 控件.
Message控件可以用于在窗口中显示一段文本消息.
以下是个简单的例子:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
@Author: zh
@Time 2023/11/24 上午11:38 .
@Email:
@Describe:
"""
from tkinter import *
# 初始化Tk() 创建窗口
root = Tk()
root.title('满江红')
root.geometry('500x500')
test = ("满江红 岳飞"
"怒发冲冠,凭栏处")
# 创建一个Message
msg = Message(root,text=test)
msg.configure(bg='red', font=('italic', 16))
msg.pack()
msg = Message(root,text=test)
msg.configure(bg='gray',font=('italic', 20),width=400,fg="red",padx=10,pady=10)
msg.pack()
root.mainloop()
Message的常用属性:
-
bg:控件的背景颜色。
-
font:控件中文本的字体,大小等。
-
fg:控件中文本的前景色。
-
width/height:控件的宽度/高度
-
text:控件中显示的文本内容。
-
padx:控件中文本与控件边缘的水平距离。(padding效果)
-
pady: 控件中文本与控件边缘的垂直距离。
全部的配置信息可以查看方法,具体代码如下:
def configure( self, cnf: dict[str, Any] | None = ..., *, anchor: _Anchor = ..., aspect: int = ..., background: _Color = ..., bd: _ScreenUnits = ..., bg: _Color = ..., border: _ScreenUnits = ..., borderwidth: _ScreenUnits = ..., cursor: _Cursor = ..., fg: _Color = ..., font: _FontDescription = ..., foreground: _Color = ..., highlightbackground: _Color = ..., highlightcolor: _Color = ..., highlightthickness: _ScreenUnits = ..., justify: Literal["left", "center", "right"] = ..., padx: _ScreenUnits = ..., pady: _ScreenUnits = ..., relief: _Relief = ..., takefocus: _TakeFocusValue = ..., text: float | str = ..., textvariable: Variable = ..., width: _ScreenUnits = ..., ) -> dict[str, tuple[str, str, str, Any, Any]] | None: ... @overload def configure(self, cnf: str) -> tuple[str, str, str, Any, Any]: ... config = configure