目录
- 为标签添加背景图片
- 为按钮添加背景图片
- 缩放图片
- 设置窗口编程透明
为标签添加背景图片
import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * if __name__ == "__main__": app = QApplication(sys.argv) win = QMainWindow() label = QLabel() label.setToolTip("这是一个文本标签") label.setStyleSheet("QLabel{border-image:url(./pyqt5/images/python.jpg)};") label.setFixedwidth(320) label.setFixedHeight(200) 开发者_开发教程win.setCentralWidget(label) win.show() sys.exit(app.exec_())
为按钮添加背景图片
import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * if __name__ == "__main__": app = QApplication(sys.argv) win = QWidget() layout = QvboxLayout() btn1 = QPushButton("按钮1") btn1.setMaximumSize(48, 48) btn1.setMinimumSize(48, 48) style = ''' QPushButton { border-radius: 30px; background-image: url('./pyqt5/images/left.png'); } ''' btn1.setStyleSheet(style) layout.addWidget(btn1) win.setLayout(layout) win.show() sys.exit(app.exec_())
import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * if __name__ == "__main__": app = QApplication(sys.argv) win = QWidget() btn1 = QPushButton("按钮1") btn1.setFixedSize(50, 50) btn1.setObjectName("btn1") style = ''' #btn1{ border-radius: 30px; background-image: url('./pyqt5/images/left.png') } #btn1:hover{ border-radius: 30px; background-image: url('./pyqt5/images/leftHover.png') } js #btn1:Pressed{ border-radius: 30px; background-image: url('./pyqt5/images/leftPressed.png') } ''' btn1.setStyleSheet(style) layout = QVBoxLayout() layout.ajavascriptddWidget(btn1) win.setLayout(layout) win.show() sys.exit(app.exec_())
缩放图片
import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * if __name__ == "__main__": ap编程客栈p = QApplication(sys.argv) win = QWidget() pic = r"./pyqt5/images/Cloudy_72px.png" img = QImage(pic) label1 = QLabel() label1.setFixedWidth(120) label1.setFixedHeight(120) result = img.scaled(label1.width(), label1.height(), Qt.IgnoreASPectRatio, Qt.SmoothTransformation) label1.setPixmap(QPixmap.fromImage(result)) label2 = QLabel() label2.setPixmap(QPixmap.fromImage(img)) layout = QVBoxLayout() layout.addWidget(label2) layout.addWidget(label1) win.setLayout(layout) win.show() sys.exit(app.exec_())
设置窗口透明
import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * if __name__ == "__main__": app = QApplication(sys.argv) win = QMainWindow() win.setWindowTitle("设置窗口透明") win.setWindowOpacity(0.5) # 设置窗口透明 透明度取值范围:0.0(全透明)~1.0(不透明),默认值1.0 win.show() sys.exit(app.exec_())
到此这篇编程客栈关于Python PyQt5学习之样式设置详解的文章就介绍到这了,更多相关PyQt5设置样式内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论