跳转至

Avatar 头像

用于展示用户头像,支持图片和文字两种模式,支持圆形和方形。

基础用法

头像预览

from monkeyqt import MkAvatar

# 文字头像
avatar = MkAvatar(text="MK")

# 图片头像
avatar_img = MkAvatar(image_path="path/to/photo.jpg")

形状

# 圆形(默认)
avatar_circle = MkAvatar(text="A", shape="circle")

# 方形(带圆角)
avatar_square = MkAvatar(text="B", shape="square")

尺寸

avatar_sm = MkAvatar(text="S", size=32)   # 小
avatar_md = MkAvatar(text="M", size=40)   # 中(默认)
avatar_lg = MkAvatar(text="L", size=64)   # 大
avatar_xl = MkAvatar(text="XL", size=100) # 超大

动态修改

avatar = MkAvatar(text="MK", size=40)

# 运行时切换为图片头像
avatar.image_path = "path/to/new_photo.jpg"

# 切换形状
avatar.shape = "square"

# 修改尺寸
avatar.size = 64

API

构造函数

MkAvatar(text="", image_path="", shape="circle", size=40, parent=None)
参数 类型 默认值 说明
text str "" 文字头像内容(取前 2 个字符,自动大写)
image_path str "" 图片路径(优先显示图片)
shape str "circle" 形状:circle / square
size int 40 头像尺寸(宽高相等,像素)
parent QWidget None 父组件

Properties

属性 类型 说明
text str 文字内容
image_path str 图片路径
shape str 形状
size int 尺寸