小编Ole*_*leh的帖子

5
推荐指数
1
解决办法
997
查看次数

在 PyQt5 中通过对象 #id 设置样式

我的项目中有 2 个文件: main.py

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication)
from styles import styles

class MyApp(QWidget):
def __init__(self):
    super().__init__()
    self.initUI()

def initUI(self):
    self.setStyleSheet(styles)
    btn1 = QPushButton('Button1', self)
    btn1.resize(btn1.sizeHint())
    btn1.move(50, 50)
    btn2 = QPushButton('Button2', self)
    btn2.resize(btn2.sizeHint())
    btn2.move(100, 100)
    self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    my = MyApp()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

style.py

styles="QPushButton#btn2 { background-color: red }"
Run Code Online (Sandbox Code Playgroud)

正如此处所述这应该更改 btn2 的背景颜色。然而,它什么也不做。怎么了?

styles="QPushButton { background-color: red }"
Run Code Online (Sandbox Code Playgroud)

工作正常(对于 QPushButton 类的所有实例)。我正在使用 …

css python python-3.x pyqt5 python-3.6

3
推荐指数
1
解决办法
879
查看次数

标签 统计

css ×1

pyqt5 ×1

python ×1

python-3.6 ×1

python-3.x ×1

telegram ×1

telegram-bot ×1