PyQt促进的小部件背景问题

时间:2019-03-03 02:42:59

标签: python pyqt pyqt5

我正在尝试创建一个具有多个屏幕(登录屏幕,主应用程序等)的应用程序。为了做到这一点,我为每个页面创建了ui文件。然后,我创建一个带有stackedWidget的主页。

在每个ui文件中,我都在顶层窗口小部件上设置了样式表。除了背景图片,其他所有内容(字体颜色,标签边框颜色等)似乎都可以正常工作。

我使用pyuic5创建所有.ui文件的.py文件。我注意到样式表在setupUi()中。

MainScreen.py(来自main.ui)

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1050, 808)
        MainWindow.setAutoFillBackground(True)
        MainWindow.setStyleSheet("")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setAutoFillBackground(True)
        self.centralwidget.setStyleSheet("")
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
        self.stackedWidget.setStyleSheet("")
        self.stackedWidget.setObjectName("stackedWidget")
        self.LoginScreen = LoginScreen()
        self.LoginScreen.setAutoFillBackground(True)
        self.LoginScreen.setObjectName("LoginScreen")
        self.stackedWidget.addWidget(self.LoginScreen)
        self.MainScreen = QtWidgets.QWidget()
        self.MainScreen.setObjectName("MainScreen")
        self.label = QtWidgets.QLabel(self.MainScreen)
        self.label.setGeometry(QtCore.QRect(460, 430, 47, 13))
        self.label.setObjectName("label")
        self.stackedWidget.addWidget(self.MainScreen)
        self.verticalLayout.addWidget(self.stackedWidget)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        self.stackedWidget.setCurrentIndex(1)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Timecarder"))
        self.label.setText(_translate("MainWindow", "Normal"))

from Login import LoginScreen

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我在另一个.py文件中创建了自定义小部件:

from PyQt5 import QtWidgets
from LoginScreen import Ui_LoginScreen


class LoginScreen(QtWidgets.QWidget):

    def __init__(self):
        super(LoginScreen, self).__init__()
        self.ui = Ui_LoginScreen()
        self.ui.setupUi(self)

LoginScreen.py(来自LoginScreen.ui)->这是我作为自定义窗口小部件升级的ui

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_LoginScreen(object):
    def setupUi(self, LoginScreen):
        LoginScreen.setObjectName("LoginScreen")
        LoginScreen.resize(1050, 808)
        LoginScreen.setStyleSheet("#LoginScreen\n"
"{\n"
"    background-color: rgb(255, 0, 0);\n"
"}\n"
"\n"
"#label\n"
"{\n"
"    color: rgb(255,255,255);\n"
"}")
        self.label = QtWidgets.QLabel(LoginScreen)
        self.label.setGeometry(QtCore.QRect(296, 380, 261, 71))
        self.label.setObjectName("label")

        self.retranslateUi(LoginScreen)
        QtCore.QMetaObject.connectSlotsByName(LoginScreen)

    def retranslateUi(self, LoginScreen):
        _translate = QtCore.QCoreApplication.translate
        LoginScreen.setWindowTitle(_translate("LoginScreen", "Form"))
        self.label.setText(_translate("LoginScreen", "Login Screen"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    LoginScreen = QtWidgets.QWidget()
    ui = Ui_LoginScreen()
    ui.setupUi(LoginScreen)
    LoginScreen.show()
    sys.exit(app.exec_())

这是我运行应用程序的地方

import sys
from PyQt5 import QtWidgets
from MainScreen import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.Login = self.ui.LoginScreen

        self.ui.stackedWidget.setCurrentIndex(0)


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    application = MainWindow()
    application.show()
    sys.exit(app.exec())

0 个答案:

没有答案