PyQt5 装载QSS文件 案例
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QHBoxLayout, QPushButton, QMessageBox, QApplication, QVBoxLayout, QWidget, \
QLabel, QGridLayout, QLineEdit, QTextEdit, QFormLayout, QComboBox, QMainWindow
from CommonHelper import CommonHelper
'''
PyQt5 装载QSS文件 案例
'''
class OWindowDemo(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 360, 260)
# 设置窗口标题
self.setWindowTitle('装载QSS文件 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
self.resize(477,258)
btn = QPushButton('装载QSS文件',self)
btn.setToolTip('提示文本')
btn.clicked.connect(self.onClick)
vbox = QVBoxLayout()
vbox.addWidget(btn)
self.setLayout(vbox)
widget = QWidget(self)
self.setCentralWidget(widget)
widget.setLayout(vbox)
def onClick(self):
styleFile = './style.qss'
qssStyle = CommonHelper.readQSS(styleFile)
w.setStyleSheet(qssStyle)
def mousePressEvent(self,event):
# 左击
if event.button() == Qt.LeftButton:
print(event.globalPos())
print(event.pos())
print(self.pos())
# 右击
if event.button() == Qt.RightButton:
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = OWindowDemo()
# 0-1,1表示不透明,0表示完全透明
# w.setWindowOpacity(0.7)
w.show()
sys.exit(app.exec_())
style.css
OWindowDemo{
border-image:url(a.gif)
}
QToolTip {
border:1px solid rgb(45,45,45);
background:white;
color:red;
}
CommonHelper.py
class CommonHelper:
@staticmethod
def readQSS(style):
with open(style,'r') as f:
return f.read()