如何从pyqt中的主窗口连接到下一个窗口?

时间:2016-02-14 10:43:05

标签: python pyqt pyqt4

我是Pyqt4的初学者。所以,我需要一个帮助。我在Pyqt设计器中做了一个多个小部件,所以我想把我的主窗口连接到我的小部件。在我的主菜单中,有一个输入按钮,我希望当我点击输入按钮时,它会转到page2.ui这是我的代码

MainWindow.py

 # -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

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(707, 563)
        MainWindow.setMinimumSize(QtCore.QSize(707, 563))
        MainWindow.setMaximumSize(QtCore.QSize(707, 563))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label_start = QtGui.QLabel(self.centralwidget)
        self.label_start.setGeometry(QtCore.QRect(0, 0, 731, 501))
        self.label_start.setText(_fromUtf8(""))
        self.label_start.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/Workout routine program.jpg")))
        self.label_start.setObjectName(_fromUtf8("label_start"))
        self.horizontalLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 500, 721, 51))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.enter = QtGui.QPushButton(self.horizontalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Corbel"))
        font.setPointSize(10)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        font.setStyleStrategy(QtGui.QFont.PreferAntialias)
        self.enter.setFont(font)
        self.enter.setAutoFillBackground(False)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/install.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.enter.setIcon(icon1)
        self.enter.setCheckable(False)
        self.enter.setAutoDefault(False)
        self.enter.setObjectName(_fromUtf8("enter"))
        self.horizontalLayout.addWidget(self.enter)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Welcome to Workout Routine Program", None))
        self.enter.setText(_translate("MainWindow", "Enter", None))


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

page2.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'page2_choosegender.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

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_page2(object):
    def setupUi(self, page2):
        page2.setObjectName(_fromUtf8("page2"))
        page2.setEnabled(True)
        page2.resize(627, 459)
        self.label_male = QtGui.QLabel(page2)
        self.label_male.setGeometry(QtCore.QRect(50, 100, 251, 251))
        self.label_male.setText(_fromUtf8(""))
        self.label_male.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/1455198182_Tony_Stark.png")))
        self.label_male.setObjectName(_fromUtf8("label_male"))
        self.label_female = QtGui.QLabel(page2)
        self.label_female.setGeometry(QtCore.QRect(330, 100, 251, 251))
        self.label_female.setText(_fromUtf8(""))
        self.label_female.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/1455198167_Customer_Female_Dark.png")))
        self.label_female.setObjectName(_fromUtf8("label_female"))
        self.horizontalLayoutWidget = QtGui.QWidget(page2)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(40, 360, 531, 80))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.pushButton_male = QtGui.QPushButton(self.horizontalLayoutWidget)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/Male.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_male.setIcon(icon)
        self.pushButton_male.setObjectName(_fromUtf8("pushButton_male"))
        self.horizontalLayout.addWidget(self.pushButton_male)
        self.pushButton_female = QtGui.QPushButton(self.horizontalLayoutWidget)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/Female.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_female.setIcon(icon1)
        self.pushButton_female.setObjectName(_fromUtf8("pushButton_female"))
        self.horizontalLayout.addWidget(self.pushButton_female)
        self.textEdit = QtGui.QTextEdit(page2)
        self.textEdit.setGeometry(QtCore.QRect(40, 20, 531, 61))
        self.textEdit.setReadOnly(True)
        self.textEdit.setObjectName(_fromUtf8("textEdit"))

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

    def retranslateUi(self, page2):
        page2.setWindowTitle(_translate("page2", "Choose your gender", None))
        self.pushButton_male.setText(_translate("page2", "Male", None))
        self.pushButton_female.setText(_translate("page2", "Female", None))
        self.textEdit.setHtml(_translate("page2", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:20pt; font-weight:600;\">Are you ?</span></p></body></html>", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    page2 = QtGui.QWidget()
    ui = Ui_page2()
    ui.setupUi(page2)
    page2.show()
    sys.exit(app.exec_())

我希望你们能帮助我。感谢

1 个答案:

答案 0 :(得分:0)

我建议您继续按照自己的方式继续操作,即使顶部的错误消息警告您,如果再次重新生成,自动生成文件中的更改也会丢失。你应该分开你的Ui文件和你的主要代码

 # -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

from page2 import Ui_page2


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(707, 563)
        MainWindow.setMinimumSize(QtCore.QSize(707, 563))
        MainWindow.setMaximumSize(QtCore.QSize(707, 563))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label_start = QtGui.QLabel(self.centralwidget)
        self.label_start.setGeometry(QtCore.QRect(0, 0, 731, 501))
        self.label_start.setText(_fromUtf8(""))
        self.label_start.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/Workout routine program.jpg")))
        self.label_start.setObjectName(_fromUtf8("label_start"))
        self.horizontalLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 500, 721, 51))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))

        # initialize the qwidget as an attribute of the class and set a listener on the button
        self.page2 = QtGui.QWidget()
        ui = Ui_page2()
        ui.setupUi(self.page2)

        self.enter = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.enter.clicked.connect(self.page2.show)

        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Corbel"))
        font.setPointSize(10)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        font.setStyleStrategy(QtGui.QFont.PreferAntialias)
        self.enter.setFont(font)
        self.enter.setAutoFillBackground(False)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/install.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.enter.setIcon(icon1)
        self.enter.setCheckable(False)
        self.enter.setAutoDefault(False)
        self.enter.setObjectName(_fromUtf8("enter"))
        self.horizontalLayout.addWidget(self.enter)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Welcome to Workout Routine Program", None))
        self.enter.setText(_translate("MainWindow", "Enter", None))

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

    sys.exit(app.exec_())

正如我之前所说的,您的代码结构不正确,如果它就像调用self.hide()or self.close一样简单,但您的Ui_MainWindow不是QWidget或者QMainWindow所以它没有使用QMainWindow在课堂外构建的方法,因此您无法访问它。一些黑客攻击可以修复,但这不是一个好习惯。我建议您查看git repo https://github.com/shuge/Qt-Python-Binding-Examples并了解如何组织代码。理想情况下,自动生成的ui文件应该与您的逻辑分离...这样可以在将来修改逻辑时更容易,而不会在再次生成ui时弄乱ui或丢失更改

理想情况下,你应该继承QtGui.QMainWindow或某种小部件

您自动生成的ui文件

ui.py

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(995, 717)

    def retranslateUi(self, MainWindow)
        pass

logic.py

from ui import Ui_MainWindow
class MyUi(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(criticalPath, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

main.py

import sys
from logic import MyUi

def main():
    app = QtGui.QApplication(sys.argv)

    #create a new object
    my_interface = MyUi()
    my_interface.show()
    sys.exit(app.exec_())

    if __name__ == '__main__': main()