muzing 3bdad6d0ef Add 03-05-06
QLabel-内边距、缩放
2022-06-26 16:42:32 +08:00

48 lines
1.6 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 QtGui, QtWidgets
"""
QLabel 内边距、位图缩放
QLabel还可以调整内边距、启用内容缩放以更细致地调节显示效果
.setMargin(int) 设置内边距(框架的最内部像素和内容的最外部像素之间的距离)
.margin() -> int 获取内边距默认值为0
.setScaledContents(bool) 设置是否启用缩放
.hasScaledContents() -> bool 返回是否启用了缩放
"""
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_1 = QtWidgets.QLabel("PySide6", self)
label_1.move(340, 100)
label_1.setStyleSheet("background-color: cyan;")
label_1.setMargin(20) # 设置边距
print(label_1.margin()) # 获取边距
# 启用缩放
label_2 = QtWidgets.QLabel(self)
label_2.resize(50, 50)
label_2.move(350, 200)
label_2.setPixmap(QtGui.QPixmap("../../Resources/Icons/OS_Ubuntu_128px.ico"))
# 该位图为128像素宽高label2仅有50像素如不缩放则只能显示位图的一部分
label_2.setScaledContents(True) # 启用内容缩放
print(label_2.hasScaledContents()) # 获取是否开启了缩放
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyWidget()
window.show()
sys.exit(app.exec())