muzing f44ba4be67 Add 03-05-01
QLabel 简介与创建
2022-06-24 18:22:00 +08:00

48 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
from PySide6 import QtWidgets
"""
QLabel 标签控件
标签控件用于在界面上显示文字、图像等
官方文档https://doc.qt.io/qtforpython/PySide6/QtWidgets/QLabel.html
继承自 QFrame
有两种构造函数可以传入文本、父控件、Qt.WindowFlags(一般不用)三个参数:
__init__(self, parent: Optional[QtWidgets.QWidget] = None, f: Qt.WindowFlags = Default(Qt.WindowFlags))
__init__(self, text: str, parent: Optional[QtWidgets.QWidget] = None, f: Qt.WindowFlags = Default(Qt.WindowFlags))
QLabel的大小会根据其内容自动调整也可以手动指定
"""
class MyWidget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("QLabel-创建")
self.resize(800, 600)
self.setup_ui()
def setup_ui(self) -> None:
"""设置界面"""
label = QtWidgets.QLabel("PySide6", self) # 创建时即设置了标签文本与父对象
label.move(350, 200)
# 也可以创建空白标签,随后设置父控件与文字
label_2 = QtWidgets.QLabel()
label_2.setParent(self)
label_2.setText("QLabel 标签控件")
label_2.move(350, 240)
# 将label2设置显示背景颜色以直观感受创建时其尺寸大小随内容自动调整
label_2.setStyleSheet("background-color: cyan;")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyWidget()
window.show()
sys.exit(app.exec())