当其他python文件调用时,无法更改Pyqt5窗口的标题?

时间:2019-03-08 11:25:54

标签: python oop pyqt5

我创建了一个通用类来更快地创建PyQt5窗口。我实现了该功能,当我使用其他程序调用该类时,可以更改窗口的标题,图标,几何形状,但是当我调用它时,则不起作用。

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon


class Window(QMainWindow):
    title = ""
    icon = "icon.ico"
    geometry = (300, 300, 900, 450)

    def __init__(self, **kwargs):
        super().__init__()
        try:
            self.title = kwargs['title']
        except:
            pass
        try:
            self.icon = kwargs['icon']
        except:
            pass
        try:
            self.geometry = kwargs['geometry']
        except:
            pass
        print(self.title)
        print(self.geometry)
        self.initailize()

    def initailize(self, *awargs):
        self.setGeometry(self.geometry[0], self.geometry[1], self.geometry[2],
                         self.geometry[3])  # distance with x-axis, distance with y-axis, width, height
        self.setWindowTitle(self.title)
        self.setWindowIcon(QIcon(self.icon))
        self.center()
        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    a = Window()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

尝试以下三个示例:

main_1.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon


class Window(QMainWindow):
    def __init__(self, 
                 title    = "change the title of the Pyqt5",
                 icon     = "im.png",
                 geometry = (300, 300, 900, 450),
                 **kwargs):
        super().__init__()

        self.title    = title
        self.icon     = icon
        self.geometry = geometry

        print(self.title)
        print(self.icon)
        print(self.geometry)

        self.initailize()

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        button = QPushButton("change the title of the Pyqt5")
        button.clicked.connect(self.onClicked)
        gridLayout = QGridLayout(centralWidget)
        gridLayout.addWidget(button)

    def initailize(self, *awargs):
        self.setGeometry(self.geometry[0], 
                         self.geometry[1], 
                         self.geometry[2],
                         self.geometry[3])  
        self.setWindowTitle(self.title)
        self.setWindowIcon(QIcon(self.icon))
        self.center()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def onClicked(self):
        self.setWindowTitle(" New title")
        self.setWindowIcon(QIcon("D:/_Qt/img/qt-logo.png"))        
        self.setGeometry(500, 150, 400, 400)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    a = Window()
    a.show()
    sys.exit(app.exec_())

main_2.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon


class Window(QMainWindow):
    def __init__(self, 
                 title    = "change the title of the Pyqt5",
                 icon     = "im.png",
                 geometry = (300, 300, 900, 450),
                 **kwargs):
        super().__init__()

        self.title    = title
        self.icon     = icon
        self.geometry = geometry

        print(self.title)
        print(self.icon)
        print(self.geometry)

        self.initailize()

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        button = QPushButton("change the title of the Pyqt5")
        button.clicked.connect(self.onClicked)
        gridLayout = QGridLayout(centralWidget)
        gridLayout.addWidget(button)

    def initailize(self, *awargs):
        self.setGeometry(self.geometry[0], 
                         self.geometry[1], 
                         self.geometry[2],
                         self.geometry[3])  
        self.setWindowTitle(self.title)
        self.setWindowIcon(QIcon(self.icon))
        self.center()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def onClicked(self):
        self.setWindowTitle(" New title")
        self.setWindowIcon(QIcon("D:/_Qt/img/qt-logo.png"))        
        self.setGeometry(500, 150, 400, 400)


class MaunWindow(QMainWindow):
    def __init__(self):
        super().__init__()  

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        button = QPushButton("called by other file?")
        button.clicked.connect(self.onClicked)
        gridLayout = QGridLayout(centralWidget)
        gridLayout.addWidget(button)

    def onClicked(self):
        self.w = Window("Title ......",
                        "Ok.png",
                        (100, 200, 300, 300))
        self.hide()
        self.w.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    a = MaunWindow()
    a.show()
    sys.exit(app.exec_())

main_3.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon

from main_1 import Window

class MaunWindow(QMainWindow):
    def __init__(self):
        super().__init__()  

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        button = QPushButton("called by other file?")
        button.clicked.connect(self.onClicked)
        gridLayout = QGridLayout(centralWidget)
        gridLayout.addWidget(button)

    def onClicked(self):
        self.w = Window("Title ......",
                        "Ok.png",
                        (100, 200, 300, 300))
        self.hide()
        self.w.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    a = MaunWindow()
    a.show()
    sys.exit(app.exec_())