鱼C论坛

 找回密码
 立即注册
查看: 3226|回复: 11

[已解决]Pyqt5 外部加载QSS文件出错

[复制链接]
发表于 2019-1-24 17:26:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ChenpiT 于 2019-1-24 17:56 编辑

Qss Files: styleSheey]t.qss
  1. QPushButton{
  2.     background:#F76677;
  3.     border-radius:5px;
  4. }
  5. QPushButton#Btn2{
  6.     background:rgb(102, 194, 247);
  7.     border-radius:5px;
  8. }
复制代码


main.py
  1. from PyQt5.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QDialog, QGroupBox, QPushButton
  2. from PyQt5 import QtGui, QtCore
  3. import sys


  4. class Window(QDialog):
  5.     def __init__(self):
  6.         super().__init__()
  7.         self.initWindow()

  8.     def initWindow(self):
  9.         self.setWindowTitle("example")
  10.         self.setGeometry(700, 400, 450, 150)

  11.         self.pushButton()

  12.         vbox = QVBoxLayout()
  13.         vbox.addLayout(self.hbox)
  14.         self.setLayout(vbox)

  15.         self.show()

  16.     def pushButton(self):
  17.         self.hbox = QHBoxLayout()
  18.         
  19.         self.Btn1 = QPushButton('Btn1')
  20.         self.Btn1.setFont(QtGui.QFont('Sanserif', 10))
  21.         self.hbox.addWidget(self.Btn1)

  22.         self.Btn2 = QPushButton('Btn2')
  23.         self.Btn2.setFont(QtGui.QFont('Sanserif', 10))
  24.         self.hbox.addWidget(self.Btn2)

  25.         self.Btn3 = QPushButton('Btn3')
  26.         self.Btn3.setFont(QtGui.QFont('Sanserif', 10))
  27.         self.hbox.addWidget(self.Btn3)

  28.     def readQssFile(self, filePath):
  29.         with open(filePath, 'r', encoding='utf-8') as fileObj:
  30.             styleSheet = fileObj.read()
  31.         return styleSheet





  32. if __name__ == "__main__":
  33.     app = QApplication(sys.argv)
  34.     window = Window()
  35.     styleSheet = window.readQssFile('./styleSheet.qss')
  36.     window.setStyleSheet(styleSheet)
  37.     sys.exit(app.exec_())
复制代码


QSS如何指定类成员的id,使Btn2显示蓝色,其他为红色?
最佳答案
2019-2-3 09:41:32
学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-28 12:03:00 | 显示全部楼层

回帖奖励 +9 鱼币

顶一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-3 09:41:32 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +9 鱼币

学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-2-3 15:45:14 | 显示全部楼层
self.Btn2设置对象名即可QAQ
  1. self.Btn2.setObjectName('Btn2')
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-6 16:25:38 | 显示全部楼层
看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-2-6 20:56:18 | 显示全部楼层
好难啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-9 11:33:47 | 显示全部楼层
有点看不懂。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-12 11:36:32 | 显示全部楼层
学习学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-15 00:22:56 | 显示全部楼层
谢谢你的 分享
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-26 15:02:46 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-26 15:03:21 | 显示全部楼层
顶上去
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-26 15:04:13 | 显示全部楼层

回帖奖励 +9 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-30 09:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表