当前位置:网站首页>PyQt5快速开发与实战 8.6 设置样式
PyQt5快速开发与实战 8.6 设置样式
2022-07-30 12:44:00 【Ding Jiaxiong】
PyQt5快速开发与实战
文章目录
8. 第8章 PyQt5 图形和特效
8.6 设置样式
8.6.1 为标签添加背景图片
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QPushButton
import sys
class WindowDemo(QWidget):
def __init__(self):
super().__init__()
label1 = QLabel(self)
label1.setToolTip('这是一个文本标签')
label1.setStyleSheet("QLabel{border-image: url(./images/python.jpg);}")
label1.setFixedWidth(476)
label1.setFixedHeight(259)
vbox = QVBoxLayout()
vbox.addWidget(label1)
vbox.addStretch()
self.setLayout(vbox)
self.setWindowTitle("Label添加背景图片例子")
if __name__ == "__main__":
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
sys.exit(app.exec_())
8.6.2 为按钮添加背景图片
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QPushButton
import sys
class WindowDemo(QWidget):
def __init__(self):
super().__init__()
btn1 = QPushButton(self)
btn1.setObjectName('btn1')
btn1.setMaximumSize(64, 64)
btn1.setMinimumSize(64, 64)
style = ''' #btn1{ border-radius: 30px; background-image: url('./images/left.png'); } #btn1:hover{ border-radius: 30px; background-image: url('./images/leftHover.png'); } #btn1:Pressed{ border-radius: 30px; background-image: url('./images/leftPressed.png'); } '''
btn1.setStyleSheet(style)
vbox = QVBoxLayout()
vbox.addStretch()
vbox.addWidget(btn1)
self.setLayout(vbox)
self.setWindowTitle("按钮添加背景图片例子")
if __name__ == "__main__":
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
sys.exit(app.exec_())
8.6.3 缩放图片
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import Qt
import sys
class WindowDemo(QWidget):
def __init__(self):
super().__init__()
filename = r".\images\Cloudy_72px.png"
img = QImage(filename)
label1 = QLabel(self)
label1.setFixedWidth(120)
label1.setFixedHeight(120)
result = img.scaled(label1.width(), label1.height(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation);
label1.setPixmap(QPixmap.fromImage(result))
# 3
vbox = QVBoxLayout()
vbox.addWidget(label1)
self.setLayout(vbox)
self.setWindowTitle("图片大小缩放例子")
if __name__ == "__main__":
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
sys.exit(app.exec_())
8.6.4 设置窗口透明
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QMainWindow()
win.setWindowTitle("窗口的透明度设置")
win.setWindowOpacity(0.5)
win.resize(350, 250)
win.show()
sys.exit(app.exec_())
8.6.5 加载QSS
在Qt中经常需要使用样式,为了降低耦合性(与逻辑代码分离),通常会定义一个QSS文件,然后编写各种控件(如 QLable、QLineEdit、QPushButton)的样式,最后使用QApplication或 QMainWindow来加载样式,这样就可以让整个应用程序共享同一种样式了。
编写QSS
MainWindow{ border: 1px solid rgb(45 ,45 ,45); } QToolTip{ border: 1px solid rgb(45 ,45 ,45); background: white; color: red; }
加载QSS
编写一个加载样式的公共类CommonHelper
class CommonHelper : def __init__(self ) : pass @staticmethod def readQss( style): with open( style , 'r') as f: return f.read()
主函数中加载
import sys from PyQt5.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QPushButton from CommonHelper import CommonHelper class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.resize(477, 258) self.setWindowTitle("加载QSS文件") btn1 = QPushButton(self) btn1.setText('添加') btn1.setToolTip('测试提示') vbox = QVBoxLayout() vbox.addWidget(btn1) self.setLayout(vbox) if __name__ == "__main__": app = QApplication(sys.argv) win = MainWindow() styleFile = './style.qss' qssStyle = CommonHelper.readQss(styleFile) win.setStyleSheet(qssStyle) win.show() sys.exit(app.exec_())
边栏推荐
猜你喜欢
北上广线下活动丨年底最不可错过的技术聚会都齐了
缓存一致性
机器学习——特征选择
OpenHarmony环境搭建报错: ImportError: cannot import name ‘VERSION‘ from ‘hb.__main__‘
dbaplus丛书丨《MySQL DBA工作笔记》限量签名版来了!
为什么说Prometheus是足以取代Zabbix的监控神器?
What are the hard-core upgrades and applications that cannot be missed in Greenplum 6.0?
What happened when the computer crashed?
常见的云计算安全问题以及如何解决
Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing
随机推荐
常见的云计算安全问题以及如何解决
[PostgreSQL] - 存储结构及缓存shared_buffers
展厅全息投影所具备的三大应用特点
ModelCoder状态机:对柴油机工况判断策略进行建模
[PostgreSQL] - explain SQL analysis introduction
dolphinscheduler simple task definition and complex cross-node parameter transfer
C语言学习练习题:汉诺塔(函数与递归)
[PostgreSQL] - explain SQL分析介绍
Unity Beginner 6 - Simple UI production (blood bar production) and audio addition and NPC dialogue bubbles (2d)
dolphinscheduler添加hana支持
干货分享:小技巧大用处之Bean管理类工厂多种实现方式
Heshu Group: Make smart cities smarter and make real life better
How to solve the problem that the page does not display the channel configuration after the EasyNVR is updated to (V5.3.0)?
[Go]四、模块和包、流程控制、结构体
Breaking the principle and introducing SQL, what does MongoDB want to do???
dbaplus丛书丨《MySQL DBA工作笔记》限量签名版来了!
13-GuliMall 基础篇总结
AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...
JS事件的相关特性以及原理
Current and voltage acquisition module DAM-6160