Spawning one window from another window on button click in qt4

南楼画角 提交于 2019-12-20 07:08:55

问题


I've created two windows using qt4 designer, and would like to link them together. I put them both in a folder and created a file outside the directory, which I'm importing them with.

I can open both windows at the same time, but that's not what I would like to do. I would like to make it so that when a button is pushed on one window, it opens the other window, then closes the first window, so the second window is the only one left open.

This is the code for the first window, with comments next to the button that I would like to have open the other window.

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(893, 591)
        MainWindow.setMinimumSize(QtCore.QSize(893, 591))
        MainWindow.setMaximumSize(QtCore.QSize(893, 591))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("images/turbulence.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setStyleSheet(_fromUtf8(""))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(-10, 50, 911, 581))
        self.label_2.setStyleSheet(_fromUtf8("background-color: rgb(0, 0, 0);"))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(-20, -10, 921, 71))
        self.label.setStyleSheet(_fromUtf8("background-color: rgb(0, 37, 37);"))
        self.label.setText(_fromUtf8(""))
        self.label.setPixmap(QtGui.QPixmap(_fromUtf8("images/header.png")))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(60, 0, 351, 51))
        self.label_3.setStyleSheet(_fromUtf8("font: 20pt \"Sans Serif\";\n"
"color: rgb(0, 221, 221);"))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(-4, 73, 901, 41))
        self.label_4.setStyleSheet(_fromUtf8("background-color: rgba(13, 247, 255, 72);"))
        self.label_4.setText(_fromUtf8(""))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.label_6 = QtGui.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(110, 80, 31, 31))
        self.label_6.setPixmap(QtGui.QPixmap(_fromUtf8("images/arrow.png")))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 80, 101, 31))
        self.pushButton.setStyleSheet(_fromUtf8("font: 13pt \"Sans Serif\";\n"
"color: rgb(0, 255, 255);"))
        self.pushButton.setFlat(True)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(140, 80, 81, 31))
        self.pushButton_2.setStyleSheet(_fromUtf8("font: 12pt \"Sans Serif\";\n"
"color: rgb(0, 255, 255);"))
        self.pushButton_2.setFlat(True)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.label_5 = QtGui.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(6, 3, 51, 51))
        self.label_5.setText(_fromUtf8(""))
        self.label_5.setPixmap(QtGui.QPixmap(_fromUtf8("images/turbulence.png")))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.label_7 = QtGui.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(20, 150, 491, 91))
        self.label_7.setStyleSheet(_fromUtf8("font: 30pt \"Sans Serif\";\n"
"color: rgb(0, 191, 191);"))
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.label_8 = QtGui.QLabel(self.centralwidget)
        self.label_8.setGeometry(QtCore.QRect(60, 240, 831, 271))
        self.label_8.setStyleSheet(_fromUtf8("color: rgb(0, 191, 191);\n"
"font: 10pt \"Sans Serif\";"))
        self.label_8.setObjectName(_fromUtf8("label_8"))
        self.label_9 = QtGui.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(60, 350, 21, 21))
        self.label_9.setPixmap(QtGui.QPixmap(_fromUtf8("images/bullet.png")))
        self.label_9.setObjectName(_fromUtf8("label_9"))
        self.label_10 = QtGui.QLabel(self.centralwidget)
        self.label_10.setGeometry(QtCore.QRect(60, 380, 21, 21))
        self.label_10.setPixmap(QtGui.QPixmap(_fromUtf8("images/bullet.png")))
        self.label_10.setObjectName(_fromUtf8("label_10"))
        self.label_11 = QtGui.QLabel(self.centralwidget)
        self.label_11.setGeometry(QtCore.QRect(60, 410, 21, 21))
        self.label_11.setPixmap(QtGui.QPixmap(_fromUtf8("images/bullet.png")))
        self.label_11.setObjectName(_fromUtf8("label_11"))
        self.label_12 = QtGui.QLabel(self.centralwidget)
        self.label_12.setGeometry(QtCore.QRect(60, 450, 21, 31))
        self.label_12.setPixmap(QtGui.QPixmap(_fromUtf8("images/bullet.png")))
        self.label_12.setObjectName(_fromUtf8("label_12"))
        self.label_13 = QtGui.QLabel(self.centralwidget)
        self.label_13.setGeometry(QtCore.QRect(-10, 550, 911, 51))
        self.label_13.setStyleSheet(_fromUtf8("background-color: rgba(13, 247, 255, 72);"))
        self.label_13.setText(_fromUtf8(""))
        self.label_13.setObjectName(_fromUtf8("label_13"))
        self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(818, 560, 71, 21))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("images/arrow.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_3.setIcon(icon1)
        self.pushButton_3.setIconSize(QtCore.QSize(20, 20))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(10, 560, 71, 23))
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8("images/cancel.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_4.setIcon(icon2)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close) #This button
        QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicke
import sysd()")), MainWindow.close) #And this button too.
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Turbulence", None))
        self.label_3.setText(_translate("MainWindow", "label3", None))
        self.pushButton.setText(_translate("MainWindow", "Welcome", None))
        self.pushButton_2.setText(_translate("MainWindow", "Folders", None))
        self.label_7.setText(_translate("MainWindow", "label7", None))
        self.label_8.setText(_translate("MainWindow", "label8", None))
        self.pushButton_3.setText(_translate("MainWindow", "Next", None))
        self.pushButton_4.setText(_translate("MainWindow", "Cancel", None))

And this is the code for the second window

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(893, 591)
        MainWindow.setMinimumSize(QtCore.QSize(893, 591))
        MainWindow.setMaximumSize(QtCore.QSize(893, 591))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("images/turbulence.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setStyleSheet(_fromUtf8(""))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(-10, 50, 911, 581))
        self.label_2.setStyleSheet(_fromUtf8("background-color: rgb(0, 0, 0);"))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(-20, -10, 921, 71))
        self.label.setStyleSheet(_fromUtf8("background-color: rgb(0, 37, 37);"))
        self.label.setText(_fromUtf8(""))
        self.label.setPixmap(QtGui.QPixmap(_fromUtf8("../images/header.png")))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(60, 0, 351, 51))
        self.label_3.setStyleSheet(_fromUtf8("font: 20pt \"Sans Serif\";\n"
"color: rgb(0, 221, 221);"))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(-4, 73, 901, 41))
        self.label_4.setStyleSheet(_fromUtf8("background-color: rgba(13, 247, 255, 72);"))
        self.label_4.setText(_fromUtf8(""))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.label_6 = QtGui.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(100, 80, 31, 31))
        self.label_6.setPixmap(QtGui.QPixmap(_fromUtf8("../images/arrow.png")))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 80, 91, 31))
        self.pushButton.setStyleSheet(_fromUtf8("font: 13pt \"Sans Serif\";\n"
"color: rgb(0, 255, 255);"))
        self.pushButton.setFlat(True)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(130, 80, 91, 31))
        self.pushButton_2.setStyleSheet(_fromUtf8("font: 12pt \"Sans Serif\";\n"
"color: rgb(0, 255, 255);"))
        self.pushButton_2.setFlat(True)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.label_5 = QtGui.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(6, 3, 51, 51))
        self.label_5.setText(_fromUtf8(""))
        self.label_5.setPixmap(QtGui.QPixmap(_fromUtf8("../images/turbulence.png")))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.label_13 = QtGui.QLabel(self.centralwidget)
        self.label_13.setGeometry(QtCore.QRect(-10, 550, 911, 51))
        self.label_13.setStyleSheet(_fromUtf8("background-color: rgba(13, 247, 255, 72);"))
        self.label_13.setText(_fromUtf8(""))
        self.label_13.setObjectName(_fromUtf8("label_13"))
        self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(818, 560, 71, 21))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../images/arrow.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_3.setIcon(icon1)
        self.pushButton_3.setIconSize(QtCore.QSize(20, 20))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(10, 560, 71, 23))
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8("../images/cancel.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_4.setIcon(icon2)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.pushButton_5 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(720, 560, 91, 21))
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(_fromUtf8("../images/arrowreverse.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_5.setIcon(icon3)
        self.pushButton_5.setIconSize(QtCore.QSize(20, 20))
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QObject.connect(self.pushButton_5, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Turbulence", None))
        self.label_3.setText(_translate("MainWindow", "Customize Your Desktop", None))
        self.pushButton.setText(_translate("MainWindow", "Folders", None))
        self.pushButton_2.setText(_translate("MainWindow", "Themes", None))
        self.pushButton_3.setText(_translate("MainWindow", "Next", None))
        self.pushButton_4.setText(_translate("MainWindow", "Cancel", None))
        self.pushButton_5.setText(_translate("MainWindow", "Previous", None))

I think the way to do this might be to create a function which opens the second window, then closes the first, but I'm not sure if I should put this in the file I'm using to import these two windows, or the first window.

I can hook up the buttons to a function that would normally open up the other window, but when I click on the button nothing happens.

Thanks for any help.


回答1:


You really need to reconsider the design of your application.

Opening and closing multiple main windows in the manner you describe is ugly and completely unnecessary. Instead, you should have one main window and use a QStackedWidget to hold a sequence of pages that can navigated through using buttons.

To experiment with this idea, create a new main window in Qt Designer and add a QStackedWidget to it (it's in the "Containers" section). Then open the two UIs you've already designed, and copy the widgets of each UI into separate pages of the stacked-widget.

Once you've done that, make sure you give all the widgets descriptive names, because you will need to refer to them later when you start writing the logic for your program. The main script of your application should look something like this:

from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        ...
        self.buttonNext.clicked.connect(self.handleButtonNext)
        self.buttonPrev.clicked.connect(self.handleButtonPrev)

    def handleButtonNext(self):
        index = self.stackedWidget.currentIndex() + 1
        if index < self.stackedWidget.count():
            self.stackedWidget.setCurrentWidget(index)

    def handleButtonPrev(self):
        index = self.stackedWidget.currentIndex() - 1
        if index >= 0:
            self.stackedWidget.setCurrentWidget(index)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Obviously, the real logic for your program will be more sophisticated than this, but it should give you a general idea of how to go about things.

PS:

I have never used it myself, but you might also want to see if the QWizard class might be more suitable for your needs.




回答2:


Your question don't show much context but it seems you are new at PyQt and PyUIC tools.

That you post aren't (or not should be) the real windows of your app. Those class are generated for pyuic or pyuic4.

You must have something like:

class RealWindow(QMainWindow):
    def __init__(parent=None):
        super(RealWindow, self).__init__(parent) # Call QMainWindow constructor.
        self.__ui = UI_MainWindow()              # Here is when you should use that class.
        self.__ui.setupUI(self)                  # And apply the properties to yout actual window

    def on_some_button_clicked(*args, **args)   # And here is where you do whatever you want to do when you press a button.

On the other hand, I'm not sure is a good idea having two main windows in the same app. I don't know even if Qt allow that at all.

I recommend you build one of your windows as a QDialog. Or build a single window with a set of controls you can hide or show at will.

I also recommend you: a pyqt tutorial



来源:https://stackoverflow.com/questions/20425110/spawning-one-window-from-another-window-on-button-click-in-qt4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!