PyQt5 创建样式公共类加载窗口样式
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QIcon
class M_Window(QMainWindow):
def __init__(self):
super().__init__()
self.resize(600,350)
self.status = self.statusBar()
# 状态栏可以设置显示时间,如果没有设置时间参数,状态栏将一直显示
self.status.showMessage("状态栏:给主窗口添加样式")
self.setWindowTitle("给主窗口添加样式")
self.setWindowIcon(QIcon("./icon/title.png"))
# 使用样式公共类给主窗口添加样式
qssfile = "./qss/mainstyle.qss"
style = CommonStyleSheet.loadqss(qssfile)
self.setStyleSheet(style)
# 创建样式公共类
class CommonStyleSheet:
def __init__(self):
pass
@staticmethod
def loadqss(style):
with open (style, "r", encoding="utf-8") as f:
return f.read()
if __name__ == "__main__":
app = QApplication(sys.argv)
m_form = M_Window()
m_form.show()
sys.exit(app.exec_())
Qss文件
QMainWindow{
color:greenyellow;
background-color:rgb(34, 34, 229);
}
QWidget{
color:white;
}
-->