在qt4中点击按钮,从另一个窗口产生一个窗口

时间:2013-12-06 13:27:40

标签: python qt4 pyqt4 qt-designer

我使用qt4 designer创建了两个窗口,并希望将它们链接在一起。我把它们放在一个文件夹中,并在目录外创建了一个文件,我将它们导入。

我可以同时打开两个窗口,但这不是我想要做的。我想这样做,以便在一个窗口上按下按钮时,它会打开另一个窗口,然后关闭第一个窗口,因此第二个窗口是唯一一个打开的窗口。

这是第一个窗口的代码,我希望打开另一个窗口的按钮旁边有注释。

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))

这是第二个窗口的代码

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))

我认为这样做的方法可能是创建一个打开第二个窗口然后关闭第一个窗口的函数,但我不确定是否应该将它放在我用来导入这两个窗口的文件中,或第一个窗口。

我可以将按钮连接到一个通常打开另一个窗口的功能,但是当我点击按钮时没有任何反应。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您确实需要重新考虑应用程序的设计。

以您描述的方式打开和关闭多个主窗口是丑陋的,完全没必要。相反,您应该有一个主窗口并使用QStackedWidget来保存可以使用按钮导航的一系列页面。

要试验这个想法,在Qt Designer中创建一个新的主窗口并向其添加一个QStackedWidget(它位于“容器”部分)。然后打开您已经设计的两个UI,并将每个UI的小部件复制到堆叠小部件的单独页面中。

完成后,请确保为所有小部件提供描述性名称,因为稍后当您开始为程序编写逻辑时需要参考它们。应用程序的主要脚本应如下所示:

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_())

显然,你的程序的真正逻辑将比这更复杂,但它应该让你大致了解如何处理事情。

PS:

我自己从未使用过它,但您可能还想看看QWizard class是否更适合您的需求。

答案 1 :(得分:0)

你的问题并没有显示太多的背景,但似乎你是PyQt和PyUIC工具的新手。

您发布的内容不是(或不应该)您应用的真实窗口。这些类是为pyuic或pyuic4生成的。

你必须有类似的东西:

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.

另一方面,我不确定在同一个应用程序中有两个主窗口是个好主意。我甚至不知道Qt是否允许这样做。

我建议您将一个窗口构建为QDialog。或者构建一个带有一组控件的窗口,您可以随意隐藏或显示这些控件。

我还建议你:a pyqt tutorial