Не отображаются иконки PyQt5

Рейтинг: 1Ответов: 1Опубликовано: 15.04.2023

Когда я открываю окно questions.py отдельно, то иконка появляется.
Когда запускаю с main.py, то наследуется иконка MainWindow.

Задача состоит в том, что при открытии второго окна из главного окна у него должно отображаться его иконка.

*main.py

from PyQt5 import QtCore, QtGui, QtWidgets, QtGui, Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtCore import QTimer, QDateTime, QUrl
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from questions import *

class Ui_Gamepol(object):
    def setupUi(self, Gamepol):
        Gamepol.setObjectName("Gamepol")
        Gamepol.resize(1334, 1000)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Gamepol.sizePolicy().hasHeightForWidth())
        Gamepol.setSizePolicy(sizePolicy)
        Gamepol.setStyleSheet("background-color: rgb(184, 234, 255);")

        self.citys1 = QtWidgets.QPushButton(Gamepol)
        self.citys1.setGeometry(QtCore.QRect(440, 330, 121, 91))
        self.citys1.setStyleSheet("QPushButton{\n"
"border:none;\n"
"background-color: rgb(255, 174, 75);\n"
"font: 34px \"MS Shell Dlg 2\";\n"
"color: white;\n"
"transition: all 0.2s linear;\n"
"border-radius: 10px;\n"
"}\n"
"QPushButton:hover{\n"
"border:none;\n"
"background-color: rgb(255, 0, 127);\n"
"font: 34px \"MS Shell Dlg 2\";\n"
"color: white;\n"
"border-radius: 10px;\n"
"}\n")
        self.citys1.setObjectName("citys1")

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

    def retranslateUi(self, Gamepol):
        _translate = QtCore.QCoreApplication.translate
        Gamepol.setWindowTitle(_translate("Gamepol", "Collabio"))
        self.citys1.setText(_translate("Gamepol", "1"))

class MainWindow(QtWidgets.QMainWindow, Ui_Gamepol):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.citys1.clicked.connect(self.CityQuestion1)

    def CityQuestion1(self):
        self.citys1.setStyleSheet("QPushButton{\n"
                                    "border:none;\n"
                                    "background-color: #aaaa7f;\n"
                                    "font: 34px \"MS Shell Dlg 2\";\n"
                                    "color: white;\n"
                                    "transition: all 0.2s linear;\n"
                                    "}\n")
        self.citys1.setEnabled(False)
        self.window = CityQuestion_1()
        self.window.show()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setWindowIcon(QtGui.QIcon('D:\p\icons\logo.svg'))
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

questions.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Ui_cityquestion1(object):
    def setupUi(self, cityquestion1):
        cityquestion1.setObjectName("cityquestion1")
        cityquestion1.resize(1223, 800)
        cityquestion1.setStyleSheet("background-color: rgb(255, 255, 255);")

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

    def retranslateUi(self, cityquestion1):
        _translate = QtCore.QCoreApplication.translate
        cityquestion1.setWindowTitle(_translate("cityquestion1", "CityQuestion_1"))

class CityQuestion_1(QtWidgets.QWidget, Ui_cityquestion1):
    def __init__(self):
        super().__init__()
        self.setupUi(self) 

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setWindowIcon(QtGui.QIcon('D:\p/icons/city.svg'))
    wd = CityQuestion_1()
    wd.show()
    sys.exit(app.exec_())

Ответы

▲ 0Принят
...

class CityQuestion_1(QtWidgets.QWidget, Ui_cityquestion1):
    def __init__(self):
        super().__init__()
        self.setupUi(self) 

# ----> vvvv <------------------------------------------------
        self.setWindowIcon(QtGui.QIcon('D:/_Qt/__Qt/Ok.png'))   

main.py

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *

# ??? from questions import *
from q1513520_widget import CityQuestion_1


class Ui_Gamepol(object):
    def setupUi(self, Gamepol):
        Gamepol.setObjectName("Gamepol")
        Gamepol.resize(1334, 1000)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Gamepol.sizePolicy().hasHeightForWidth())
        Gamepol.setSizePolicy(sizePolicy)
        Gamepol.setStyleSheet("background-color: rgb(184, 234, 255);")

        self.citys1 = QtWidgets.QPushButton(Gamepol)
        self.citys1.setGeometry(QtCore.QRect(440, 330, 121, 91))
        self.citys1.setStyleSheet("QPushButton{\n"
"border:none;\n"
"background-color: rgb(255, 174, 75);\n"
"font: 34px \"MS Shell Dlg 2\";\n"
"color: white;\n"

"/* transition: all 0.2s linear;                  # ???# ???# ???        */\n" 

"border-radius: 10px;\n"
"}\n"
"QPushButton:hover{\n"
"border:none;\n"
"background-color: rgb(255, 0, 127);\n"
"font: 34px \"MS Shell Dlg 2\";\n"
"color: white;\n"
"border-radius: 10px;\n"
"}\n")
        self.citys1.setObjectName("citys1")

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

    def retranslateUi(self, Gamepol):
        _translate = QtCore.QCoreApplication.translate
        Gamepol.setWindowTitle(_translate("Gamepol", "Collabio"))
        self.citys1.setText(_translate("Gamepol", "1"))

class MainWindow(QtWidgets.QMainWindow, Ui_Gamepol):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.citys1.clicked.connect(self.CityQuestion1)

    def CityQuestion1(self):
        self.citys1.setStyleSheet("QPushButton{\n"
                                    "border:none;\n"
                                    "background-color: #aaaa7f;\n"
                                    "font: 34px \"MS Shell Dlg 2\";\n"
                                    "color: white;\n"
                                    
                                    
                                    "/* transition: all 0.2s linear; # ??? */\n"
                                    
                                    
                                    "}\n")
        self.citys1.setEnabled(False)
        self.window = CityQuestion_1()
        self.window.show()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setWindowIcon(QtGui.QIcon('icons/baseline-check_box-24px.svg'))
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

q1513520_widget.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_cityquestion1(object):
    def setupUi(self, cityquestion1):
        cityquestion1.setObjectName("cityquestion1")
        cityquestion1.resize(1223, 800)
        cityquestion1.setStyleSheet("background-color: rgb(255, 255, 255);")

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

    def retranslateUi(self, cityquestion1):
        _translate = QtCore.QCoreApplication.translate
        cityquestion1.setWindowTitle(_translate("cityquestion1", "CityQuestion_1"))


class CityQuestion_1(QtWidgets.QWidget, Ui_cityquestion1):
    def __init__(self):
        super().__init__()
        self.setupUi(self) 

# ----> vvvv <------------------------------------------------
        self.setWindowIcon(QtGui.QIcon('D:/_Qt/__Qt/Ok.png'))   

введите сюда описание изображения