使用pyqt / qt designer

时间:2016-02-15 07:58:51

标签: python qt pyqt pyqt4 qt-designer

有人请帮助我。在我的程序中,我有一个按钮,可以连接到另一个窗口。在主页面上有一个按钮输入以连接到下一页,它工作正常。但是当来到第2页时,有一个男性女性按钮,当我点击它时,它没有连接到第三页,如果我点击男性按钮会转到Ui_mmenu,如果点击女性按钮,则会转到Ui_fmenu。但它出错了。我希望有一个人可以帮助我。我是python的初学者

这是我的代码,

import sys
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.setEnabled(True)
        MainWindow.resize(707, 563)
        MainWindow.setMinimumSize(QtCore.QSize(707, 563))
        MainWindow.setMaximumSize(QtCore.QSize(707, 563))
        MainWindow.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        MainWindow.setAcceptDrops(False)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setTabShape(QtGui.QTabWidget.Rounded)
        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"))
        # Enter Button in Main Window
        self.page2 = QtGui.QWidget()
        ui = Ui_page2()
        ui.setupUi(self.page2)

        self.enter_btn = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.enter_btn.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_btn.setFont(font)
        self.enter_btn.setAutoFillBackground(False)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/install.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.enter_btn.setIcon(icon1)
        self.enter_btn.setCheckable(False)
        self.enter_btn.setAutoDefault(False)
        self.enter_btn.setObjectName(_fromUtf8("enter_btn"))
        self.horizontalLayout.addWidget(self.enter_btn)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.enter_btn, QtCore.SIGNAL(_fromUtf8("released()")), MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

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



class Ui_page2(object):
    def setupUi(self, page2):
        page2.setObjectName(_fromUtf8("page2"))
        page2.setEnabled(True)
        page2.resize(601, 444)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        page2.setWindowIcon(icon)
        self.label_male = QtGui.QLabel(page2)
        self.label_male.setGeometry(QtCore.QRect(50, 100, 251, 251))
        self.label_male.setMinimumSize(QtCore.QSize(251, 251))
        self.label_male.setMaximumSize(QtCore.QSize(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.setMinimumSize(QtCore.QSize(251, 251))
        self.label_female.setMaximumSize(QtCore.QSize(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"))
        #male push Button
        self.mmenu = QtGui.QWidget()
        ui = Ui_mmenu()
        ui.setupUi(self.mmenu)

        self.pushButton_male = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.pushButton_male.clicked.connect(self.mmenu.show)

        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_male.setFont(font)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/Male.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_male.setIcon(icon1)
        self.pushButton_male.setObjectName(_fromUtf8("pushButton_male"))
        self.horizontalLayout.addWidget(self.pushButton_male)

        #female push Button
        self.fmenu = QtGui.QWidget()
        ui = Ui_fmenu()
        ui.setupUi(self.fmenu)

        self.pushButton_female = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.pushButton_female.clicked.connect(self.fmenu.show)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_female.setFont(font)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/Female.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_female.setIcon(icon2)
        self.pushButton_female.setObjectName(_fromUtf8("pushButton_female"))
        self.horizontalLayout.addWidget(self.pushButton_female)
        self.label = QtGui.QLabel(page2)
        self.label.setGeometry(QtCore.QRect(40, 10, 531, 61))
        self.label.setMinimumSize(QtCore.QSize(531, 61))
        self.label.setMaximumSize(QtCore.QSize(531, 61))
        self.label.setText(_fromUtf8(""))
        self.label.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/choose gender.jpg")))
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(page2)
        QtCore.QObject.connect(self.pushButton_male, QtCore.SIGNAL(_fromUtf8("released()")), page2.close)
        QtCore.QObject.connect(self.pushButton_female, QtCore.SIGNAL(_fromUtf8("released()")), page2.close)
        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))


class Ui_mmenu(object):
    def setupUi(self, mmenu):
        mmenu.setObjectName(_fromUtf8("mmenu"))
        mmenu.setEnabled(True)
        mmenu.resize(799, 467)
        mmenu.setMinimumSize(QtCore.QSize(799, 467))
        mmenu.setMaximumSize(QtCore.QSize(799, 467))
        mmenu.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        mmenu.setWindowIcon(icon)
        self.verticalLayoutWidget = QtGui.QWidget(mmenu)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(260, 10, 531, 411))
        self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.label_2 = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_2.setMinimumSize(QtCore.QSize(529, 60))
        self.label_2.setMaximumSize(QtCore.QSize(529, 60))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/arm.jpg")))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.verticalLayout.addWidget(self.label_2)
        self.pushButton_1 = QtGui.QPushButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_1.setFont(font)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/Windows Media Player.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_1.setIcon(icon1)
        self.pushButton_1.setObjectName(_fromUtf8("pushButton_1"))
        self.verticalLayout.addWidget(self.pushButton_1)
        self.label_3 = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_3.setMinimumSize(QtCore.QSize(529, 60))
        self.label_3.setMaximumSize(QtCore.QSize(529, 60))
        self.label_3.setText(_fromUtf8(""))
        self.label_3.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/chest.jpg")))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.verticalLayout.addWidget(self.label_3)
        self.pushButton_2 = QtGui.QPushButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setIcon(icon1)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.verticalLayout.addWidget(self.pushButton_2)
        self.label_4 = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_4.setMinimumSize(QtCore.QSize(529, 60))
        self.label_4.setMaximumSize(QtCore.QSize(529, 60))
        self.label_4.setText(_fromUtf8(""))
        self.label_4.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/abs.jpg")))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.verticalLayout.addWidget(self.label_4)
        self.pushButton_3 = QtGui.QPushButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setIcon(icon1)
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.verticalLayout.addWidget(self.pushButton_3)
        self.label_1 = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_1.setMinimumSize(QtCore.QSize(529, 60))
        self.label_1.setMaximumSize(QtCore.QSize(529, 60))
        self.label_1.setText(_fromUtf8(""))
        self.label_1.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/legs.jpg")))
        self.label_1.setObjectName(_fromUtf8("label_1"))
        self.verticalLayout.addWidget(self.label_1)
        self.pushButton_4 = QtGui.QPushButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setIcon(icon1)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.verticalLayout.addWidget(self.pushButton_4)
        self.label = QtGui.QLabel(mmenu)
        self.label.setGeometry(QtCore.QRect(-30, 0, 311, 451))
        self.label.setText(_fromUtf8(""))
        self.label.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/male.png")))
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton_5 = QtGui.QPushButton(mmenu)
        self.pushButton_5.setGeometry(QtCore.QRect(700, 430, 93, 28))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_5.setFont(font)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/back.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_5.setIcon(icon2)
        self.pushButton_5.setAutoDefault(False)
        self.pushButton_5.setDefault(False)
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))

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

    def retranslateUi(self, mmenu):
        mmenu.setWindowTitle(_translate("mmenu", "Male Exercise Menu", None))
        self.pushButton_1.setText(_translate("mmenu", "Enter", None))
        self.pushButton_2.setText(_translate("mmenu", "Enter", None))
        self.pushButton_3.setText(_translate("mmenu", "Enter", None))
        self.pushButton_4.setText(_translate("mmenu", "Enter", None))
        self.pushButton_5.setText(_translate("mmenu", "Back", None))


class Ui_fmenu(object):
    def setupUi(self, fmenu):
        fmenu.setObjectName(_fromUtf8("fmenu"))
        fmenu.setWindowModality(QtCore.Qt.NonModal)
        fmenu.setEnabled(True)
        fmenu.resize(840, 467)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(fmenu.sizePolicy().hasHeightForWidth())
        fmenu.setSizePolicy(sizePolicy)
        fmenu.setMinimumSize(QtCore.QSize(840, 467))
        fmenu.setMaximumSize(QtCore.QSize(840, 467))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        fmenu.setWindowIcon(icon)
        self.verticalLayoutWidget = QtGui.QWidget(fmenu)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(310, 0, 531, 421))
        self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.label_2 = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_2.setMinimumSize(QtCore.QSize(529, 60))
        self.label_2.setMaximumSize(QtCore.QSize(529, 60))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/female arm.jpg")))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.verticalLayout.addWidget(self.label_2)
        self.pushButton_1 = QtGui.QPushButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_1.setFont(font)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents/Icon/2.0/Windows Media Player.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_1.setIcon(icon1)
        self.pushButton_1.setObjectName(_fromUtf8("pushButton_1"))
        self.verticalLayout.addWidget(self.pushButton_1)
        self.label_3 = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_3.setMinimumSize(QtCore.QSize(529, 60))
        self.label_3.setMaximumSize(QtCore.QSize(529, 60))
        self.label_3.setText(_fromUtf8(""))
        self.label_3.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/female chest.jpg")))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.verticalLayout.addWidget(self.label_3)
        self.pushButton_2 = QtGui.QPushButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setIcon(icon1)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.verticalLayout.addWidget(self.pushButton_2)
        self.label_4 = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_4.setMinimumSize(QtCore.QSize(529, 60))
        self.label_4.setMaximumSize(QtCore.QSize(529, 60))
        self.label_4.setText(_fromUtf8(""))
        self.label_4.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/female abs.jpg")))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.verticalLayout.addWidget(self.label_4)
        self.pushButton_3 = QtGui.QPushButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setIcon(icon1)
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.verticalLayout.addWidget(self.pushButton_3)
        self.label_5 = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_5.setMinimumSize(QtCore.QSize(529, 60))
        self.label_5.setMaximumSize(QtCore.QSize(529, 60))
        self.label_5.setText(_fromUtf8(""))
        self.label_5.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/label/female butt.jpg")))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.verticalLayout.addWidget(self.label_5)
        self.pushButton_4 = QtGui.QPushButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setIcon(icon1)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.verticalLayout.addWidget(self.pushButton_4)
        self.label = QtGui.QLabel(fmenu)
        self.label.setGeometry(QtCore.QRect(0, 0, 301, 471))
        self.label.setText(_fromUtf8(""))
        self.label.setPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/female.jpg")))
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton_5 = QtGui.QPushButton(fmenu)
        self.pushButton_5.setGeometry(QtCore.QRect(740, 430, 93, 28))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Champagne & Limousines"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_5.setFont(font)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Downloads/back.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_5.setIcon(icon2)
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))

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

    def retranslateUi(self, fmenu):
        fmenu.setWindowTitle(_translate("fmenu", "Female Exercise Menu", None))
        self.pushButton_1.setText(_translate("fmenu", "Enter", None))
        self.pushButton_2.setText(_translate("fmenu", "Enter", None))
        self.pushButton_3.setText(_translate("fmenu", "Enter", None))
        self.pushButton_4.setText(_translate("fmenu", "Enter", None))
        self.pushButton_5.setText(_translate("fmenu", "Back", 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_())

1 个答案:

答案 0 :(得分:0)

您必须将Logic与自动生成的ui文件分开。这样做可以使您的代码更具可读性,灵活性和可维护性,您也可以在不影响应用程序逻辑的情况下修改或重新生成您的ui文件,以下是您使用第一个UI进行操作的示例

ui_main.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

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

main.py

import sys
from PyQt4 import QtGui
from ui_main import Ui_MainWindow

class YourMainWindow(QtGui.QMainWindow): # Your class has to inherit from QMainWindow or a QWidget

    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        # self.ui.enter.clicked.connect(self.show_page2) You can seperate and import the second page and initialize it in the constructor

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    your_main_window = YourMainWindow()
    your_main_window.show()
    sys.exit(app.exec_())

理想情况下,ui文件应该位于它自己的包中,而另一个包中的逻辑或主项目文件夹中的逻辑

看看这个问题,询问如何在这里组织一个pyside / pyqt项目Saving connect statements pyqt when UI is still changing

或者你可以看看这个github repo(自我推销:))https://github.com/danidee10/Maven它的PySide但是将你的导入改为PyQt4仍然有用

在提问时尽量不要粘贴太多代码,因为它不鼓励其他用户回答