当前位置:网站首页>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_())
边栏推荐
- I built another wheel: GrpcGateway
- dolphinscheduler adds hana support
- 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
- 云主机上的MongoDB被威胁,开启AUTH认证
- OpenHarmony环境搭建报错: ImportError: cannot import name ‘VERSION‘ from ‘hb.__main__‘
- 亚洲高校首现KDD博士论文奖:清华裘捷中获Runner Up奖,WINNER奖也是位华人
- 自从外包干了四年,基本废了...
- grep时排除指定的文件和目录
- 忆联:激活数据要素价值潜能,释放SAS SSD创新红利
- Lake storehouse which electricity (2) of the project: project using technology and version and the environment
猜你喜欢

Add the device library after Vivado installation

结合实战,浅析GB/T28181(三)——实况点播

Markdown 1 - 图文音视频等

力扣——11.盛最多水的容器

【Kaggle:UW-Madison GI Tract Image Segmentation】肠胃分割比赛:赛后复盘+数据再理解

ModelCoder状态机:对柴油机工况判断策略进行建模

What are the hard-core upgrades and applications that cannot be missed in Greenplum 6.0?

DeFi 巨头进军 NFT 领域 用户怎么看?

There is no one of the strongest kings in the surveillance world!

看了这些6G原型样机,我想一觉睡到2030年
随机推荐
刷屏了!!!
树形dp小总结(换根,基环树,杂七杂八的dp)
漫谈金丝雀部署(Canary Deployment)
大手笔!两所“双一流”大学,获75亿元重点支持!
力扣——11.盛最多水的容器
Scala基础:数组(Array)、映射(Map)、元组(Tuple)、集合(List)
MySQL查询性能优化
我又造了个轮子:GrpcGateway
【河北工业大学】考研初试复试资料分享
机器学习——特征选择
Heshu Group: Make smart cities smarter and make real life better
物理服务器与虚拟机:主要区别和相似之处
DOM常用方法以及项目
自从外包干了四年,基本废了...
no matching host key type found. Their offer: ssh-rsa
MySQL【多表查询】
Win11打不开exe应用程序怎么办?Win11无法打开exe程序解决方法
Current and voltage acquisition module DAM-6160
int a=8,a=a++,a? int b=8,b=b+1,b?
企业如何成功完成云迁移?