QLabel的显示圆形 python
时间: 2024-04-14 21:26:21 浏览: 237
要将QLabel的显示设置为圆形,您可以使用QLabel的样式表(stylesheet)来实现。以下是一个示例代码:
```python
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Circular QLabel Example")
# 创建一个垂直布局
layout = QVBoxLayout()
# 加载图片并进行等比缩放
pixmap = QPixmap("image.jpg")
scaled_pixmap = pixmap.scaled(200, 200, aspectRatioMode=Qt.KeepAspectRatio)
# 创建一个 QLabel 并设置缩放后的 pixmap
label = QLabel(self)
label.setPixmap(scaled_pixmap)
# 设置样式表使 QLabel 显示为圆形
label.setStyleSheet("border-radius: 100px;")
# 将 QLabel 添加到布局中
layout.addWidget(label)
# 设置窗口的布局
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
请确保将上述代码中的 "image.jpg" 替换为您自己的图片路径。在上面的代码中,我们将QLabel的样式表设置为 "border-radius: 100px;",这会将QLabel显示为一个半径为100像素的圆形。您可以根据需要调整样式表中的像素值来改变圆形的大小。
阅读全文
相关推荐

















