我的项目中有 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 类的所有实例)。我正在使用 …