Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么一个类在另一个类中产生的结果与';另一类';?_Python_Python 3.x_Pyqt_Pyqt5_Qtstylesheets - Fatal编程技术网

Python 为什么一个类在另一个类中产生的结果与';另一类';?

Python 为什么一个类在另一个类中产生的结果与';另一类';?,python,python-3.x,pyqt,pyqt5,qtstylesheets,Python,Python 3.x,Pyqt,Pyqt5,Qtstylesheets,我正在使用PyQt5开发Python应用程序。以下是相关代码: 类对话框(QtWidgets.QMainWindow): 定义初始化(自): super()。\uuuu init\uuuuu() self.layout=qtwidts.QGridLayout() self.main=qtwidts.QWidget() self.main.setLayout(self.layout) self.setStyleSheet(qmainWindowsStyle) self.setCentralWidg

我正在使用PyQt5开发Python应用程序。以下是相关代码:

类对话框(QtWidgets.QMainWindow): 定义初始化(自): super()。\uuuu init\uuuuu() self.layout=qtwidts.QGridLayout() self.main=qtwidts.QWidget() self.main.setLayout(self.layout) self.setStyleSheet(qmainWindowsStyle) self.setCentralWidget(self.main) self.show() 类外观选项卡(QtWidgets.QWidget): 定义初始化(自): super()。\uuuu init\uuuuu() 类设置对话框(对话框): 定义初始化(自): super()。\uuuu init\uuuuu() self.tabs=QtWidgets.QTabWidget(self) self.tabs.setStyleSheet(QTabWidgetStyle) self.layout.addWidget(self.tabs) self.tab_外观=外观选项卡() self.tab_外观.setStyleSheet(QWidgetStyle) self.tab_外观_布局=qtwidts.QGridLayout() self.tab\u外观.setLayout(self.tab\u外观\u布局) self.tabs.addTab(self.tab_外观,“外观”) self.tab_server=qtwidts.QWidget() self.tab_server.setStyleSheet(QWidgetStyle) self.tab_server_layout=qtwidts.QGridLayout() self.tab\u server.setLayout(self.tab\u server\u布局) self.tabs.addTab(self.tab_服务器,“服务器”) 为什么当
self.tab_外观
AppearanceTab
实例(应该是
QWidget
的副本)时,当
self.tab_服务器
QWidget
的实例时,它与
self.tab_服务器
有不同的风格(即背景颜色变化)

样式表只定义了
背景色:#333333
颜色:#dddddd

提前谢谢

编辑:

我认为样式表没有正确地应用于
AppearanceTab
,但是我不这么认为;我不知道为什么会这样,因为它只是继承自
QWidget

编辑2:


可以找到MCVE(以及我的项目的其余部分)。

在文档中,有一个段落和样式:

继承

在经典的CSS中,当项目的字体和颜色没有显式设置时,它会自动从父项继承。使用Qt样式表时,小部件不会自动从其父小部件继承其字体和颜色设置

如果我们想设置QGroupBox及其子项的颜色,我们可以写:

qApp->setStyleSheet("QGroupBox, QGroupBox * { color: red; }");
所以你可能想改变

QMainWindowStyle = QMainWindow {color: #dddddd; background-color: #333333;}

使主窗口的所有子窗口小部件具有相同的样式。

试试:

from PyQt5 import QtWidgets

class Dialog(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.layout = QtWidgets.QGridLayout()
        self.main = QtWidgets.QWidget()
        self.main.setLayout(self.layout)

#        self.setStyleSheet(QMainWindowStyle)
        self.setCentralWidget(self.main)
        self.show()

class AppearanceTab(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

class SettingsDialog(Dialog):
    def __init__(self):
        super().__init__()
        self.tabs = QtWidgets.QTabWidget(self)
#        self.tabs.setStyleSheet(QTabWidgetStyle)
        self.layout.addWidget(self.tabs)

        self.tab_appearance = AppearanceTab()
#        self.tab_appearance.setStyleSheet(QWidgetStyle)
##        self.tab_appearance.setStyleSheet("QWidget, QWidget * {color: #dddddd; background-color: #333333;}") #note: Tried this however it didn't work.
        self.tab_appearance_layout = QtWidgets.QGridLayout()
        self.tab_appearance.setLayout(self.tab_appearance_layout)
        self.tabs.addTab(self.tab_appearance, "Appearance")

        self.tab_server = QtWidgets.QWidget()
#        self.tab_server.setStyleSheet(QWidgetStyle)
        self.tab_server_layout = QtWidgets.QGridLayout()
        self.tab_server.setLayout(self.tab_server_layout)
        self.tabs.addTab(self.tab_server, "Server")



style = """
QWidget {
    color: #dddddd;
    background-color: #333333;
}

QMainWindow {
    color: #dddddd;
    background-color: #333333;
}

QTabWidget {
    background-color: #333333;
    color: #dddddd;
}
QTabBar {
    color: #dddddd;
    background-color: #333333;
}
"""

if __name__ == "__main__":
    QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))
    app = QtWidgets.QApplication([])
    app.setStyleSheet(style)                       # < ---
    d = SettingsDialog()
    print(app.exec_())
从PyQt5导入QtWidgets
类对话框(QtWidgets.QMainWindow):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.layout=qtwidts.QGridLayout()
self.main=qtwidts.QWidget()
self.main.setLayout(self.layout)
#self.setStyleSheet(qmainWindowsStyle)
self.setCentralWidget(self.main)
self.show()
类外观选项卡(QtWidgets.QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
类设置对话框(对话框):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.tabs=QtWidgets.QTabWidget(self)
#self.tabs.setStyleSheet(QTabWidgetStyle)
self.layout.addWidget(self.tabs)
self.tab_外观=外观选项卡()
#self.tab_外观.setStyleSheet(QWidgetStyle)
##self.tab_appearance.setStyleSheet(“QWidget,QWidget*{color:#ddddddd;background color:#333333;}”)#注意:尝试了这个方法,但没有成功。
self.tab_外观_布局=qtwidts.QGridLayout()
self.tab\u外观.setLayout(self.tab\u外观\u布局)
self.tabs.addTab(self.tab_外观,“外观”)
self.tab_server=qtwidts.QWidget()
#self.tab_server.setStyleSheet(QWidgetStyle)
self.tab_server_layout=qtwidts.QGridLayout()
self.tab\u server.setLayout(self.tab\u server\u布局)
self.tabs.addTab(self.tab_服务器,“服务器”)
style=”“”
QWidget{
颜色:#dddddd;
背景色:#333333;
}
QMainWindow{
颜色:#dddddd;
背景色:#333333;
}
QTabWidget{
背景色:#333333;
颜色:#dddddd;
}
克塔巴{
颜色:#dddddd;
背景色:#333333;
}
"""
如果名称=“\uuuuu main\uuuuuuuu”:
qtwidts.QApplication.setStyle(qtwidts.QStyleFactory.create(“Fusion”))
app=QtWidgets.QApplication([])
附录.设置样式表(样式)#---
d=设置对话框()
打印(app.exec_389;()

不幸的是,这不适合我。我将
self.tab_外观.setStyleSheet(QWidgetStyle)
更改为
self.tab_外观.setStyleSheet(“QWidget,QWidget*{background color:#333333;color:#dddddd;}”)
,但没有成功。我想把它放到CSS中。你也试过了吗?我的错误,我认为这是关于子部件而不是类继承。你能发布一个和css样式表吗?是的,我会尝试,但是会有一段时间(很多代码需要修剪/我目前在学校)@JacquesGaudin查看我最近对MCVE的编辑(还有我的其他代码)谢谢,我的机器上现在没有QT,但我可以稍后再试。我的第一个想法是将
QMainWindowStyle=QMainWindow{color:#ddddddd;背景色:#333333;}
更改为
QMainWindowStyle=QMainWindow,QMainWindow*{color:#ddddddd;背景色:#333333;}
,这样主窗口的所有子窗口小部件都具有相同的样式。@JacquesGaudin成功了,谢谢!我会接受一个答案,如果你张贴一个。
from PyQt5 import QtWidgets

class Dialog(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.layout = QtWidgets.QGridLayout()
        self.main = QtWidgets.QWidget()
        self.main.setLayout(self.layout)

#        self.setStyleSheet(QMainWindowStyle)
        self.setCentralWidget(self.main)
        self.show()

class AppearanceTab(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

class SettingsDialog(Dialog):
    def __init__(self):
        super().__init__()
        self.tabs = QtWidgets.QTabWidget(self)
#        self.tabs.setStyleSheet(QTabWidgetStyle)
        self.layout.addWidget(self.tabs)

        self.tab_appearance = AppearanceTab()
#        self.tab_appearance.setStyleSheet(QWidgetStyle)
##        self.tab_appearance.setStyleSheet("QWidget, QWidget * {color: #dddddd; background-color: #333333;}") #note: Tried this however it didn't work.
        self.tab_appearance_layout = QtWidgets.QGridLayout()
        self.tab_appearance.setLayout(self.tab_appearance_layout)
        self.tabs.addTab(self.tab_appearance, "Appearance")

        self.tab_server = QtWidgets.QWidget()
#        self.tab_server.setStyleSheet(QWidgetStyle)
        self.tab_server_layout = QtWidgets.QGridLayout()
        self.tab_server.setLayout(self.tab_server_layout)
        self.tabs.addTab(self.tab_server, "Server")



style = """
QWidget {
    color: #dddddd;
    background-color: #333333;
}

QMainWindow {
    color: #dddddd;
    background-color: #333333;
}

QTabWidget {
    background-color: #333333;
    color: #dddddd;
}
QTabBar {
    color: #dddddd;
    background-color: #333333;
}
"""

if __name__ == "__main__":
    QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))
    app = QtWidgets.QApplication([])
    app.setStyleSheet(style)                       # < ---
    d = SettingsDialog()
    print(app.exec_())