No.4 PyQt学习(页面跳转)

先定义了两个MainWindow进行跳转,但发现这样的话,从第二个Window无法跳转会第一个。代码如下:

# -*- coding: utf-8 -*-
import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import MainPage

class Home(QMainWindow):

    def __init__(self):
        super(Home, self).__init__()
        #QtGui.QWidget.__init__(self)
        self.style = """ 
                        QPushButton{background-color:grey;color:white;} 
                        #window{ background-image: url(background1.jpg); }
                    """
        self.setStyleSheet(self.style)
        self.initUI()

    def initUI(self):

        self.resize(650, 480)
        self.statusBar().showMessage('Ready')
        self.setObjectName("window")
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog)
        self.center()

        widget = QWidget()
        label = QLabel()
        label.setText("<font size=%s><B>%s</B></font>" %("15", "Face Recognition System"))
        start = QPushButton("Start", self)
        widget.setStatusTip('  ')
        #start.resize(50, 25)
        quit = QPushButton("Quit", self)
        #quit.resize(50,25)
        start.clicked.connect(self.startClicked)
        quit.clicked.connect(self.quitClicked)

        vbox1 = QVBoxLayout()  # 垂直布局
        vbox2 = QVBoxLayout()
        vbox3 = QVBoxLayout()
        vbox4 = QVBoxLayout()

        #两边空隙填充
        label1 = QLabel()
        label1.resize(50,50)
        label2 = QLabel()
        label2.resize(50, 50)
        vbox1.addWidget(label1)
        #vbox2.addWidget(label)
        vbox4.addWidget(start)
        vbox4.addWidget(quit)
        vbox3.addWidget(label2)
        # 按钮两边空隙填充
        label3 = QLabel()
        label3.resize(50, 50)
        label4 = QLabel()
        label4.resize(50, 50)
        hbox1 = QHBoxLayout()
        hbox1.addWidget(label3)
        hbox1.addLayout(vbox4)
        hbox1.addWidget(label4)
        #标题与两个按钮上下协调
        label5 = QLabel()
        label5.resize(1, 1)
        label6 = QLabel()
        label6.resize(1, 1)
        label7 = QLabel()
        label7.resize(1, 1)
        vbox2.addWidget(label5)
        vbox2.addWidget(label)
        vbox2.addWidget(label6)
        vbox2.addLayout(hbox1)
        vbox2.addWidget(label7)

        hbox = QHBoxLayout()
        hbox.addLayout(vbox1)
        hbox.addLayout(vbox2)
        hbox.addLayout(vbox3)
        widget.setLayout(hbox)

        self.setCentralWidget(widget)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            QApplication.postEvent(self, QEvent(174))
            event.accept()

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

    def quitClicked(self):
        reply = QMessageBox.question(self, 'Warning',
                                           'Are you sure to quit?', QMessageBox.Yes,
                                           QMessageBox.No)
        if reply == QMessageBox.Yes:
            quit()

    def startClicked(self):
        self.hide()
        self.ui = MainPage.MainPage()                #必须将另一个界面改为成员变量,负责MainPage会与函数调用周期一样一闪而过
        self.ui.show()

    def center(self):
        qr = self.frameGeometry()  # 得到该主窗口的矩形框架qr
        cp = QDesktopWidget().availableGeometry().center()  # 屏幕中间点的坐标cp
        qr.moveCenter(cp)  # 将矩形框架移至屏幕正中央
        self.move(qr.topLeft())  # 应用窗口移至矩形框架的左上角点


def main():
    app = QApplication(sys.argv)
    main = Home()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

第二个窗口代码为:

# -*- coding: utf-8 -*-
import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

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

try:
    _encoding = QApplication.UnicodeUTF8

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


class MainPage(QMainWindow):
    def __init__(self):
        super(MainPage, self).__init__()
        self.style = """ 
                                QPushButton{background-color:grey;color:white;} 
                                #window{ background-image: url(background1.jpg); }
                            """
        self.setStyleSheet(self.style)
        self.initUI()

    def initUI(self):
        self.resize(650, 480)
        self.setObjectName("window")
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog)
        self.center()
        back = QPushButton("Back", self)
        back.setGeometry(190, 210,50, 25)
        back.clicked.connect(self.backClicked)

    def backClicked(self):
        self.close()

    def center(self):
        qr = self.frameGeometry()  # 得到该主窗口的矩形框架qr
        cp = QDesktopWidget().availableGeometry().center()  # 屏幕中间点的坐标cp
        qr.moveCenter(cp)  # 将矩形框架移至屏幕正中央
        self.move(qr.topLeft())  # 应用窗口移至矩形框架的左上角点

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

截图如下:

    

点击start至第二个界面,点击back无法返回第一个页面。

 修改代码,可以实现两个页面的互相跳转:

Page1:

# -*- coding: utf-8 -*-
import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import MainPage

class Home(QMainWindow):

    def __init__(self):
        super(Home, self).__init__()
        #QtGui.QWidget.__init__(self)
        self.style = """ 
                        QPushButton{background-color:grey;color:white;} 
                        #window{ background-image: url(background1.jpg); }
                    """
        self.setStyleSheet(self.style)
        self.initUI()

    def initUI(self):

        self.resize(650, 480)
        self.statusBar().showMessage('Ready')
        self.setObjectName("window")
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog)
        self.center()

        widget = QWidget()
        label = QLabel()
        label.setText("<font size=%s><B>%s</B></font>" %("15", "Face Recognition System"))
        start = QPushButton("Start", self)
        widget.setStatusTip('  ')
        #start.resize(50, 25)
        quit = QPushButton("Quit", self)
        #quit.resize(50,25)
        start.clicked.connect(self.startClicked)
        quit.clicked.connect(self.quitClicked)

        vbox1 = QVBoxLayout()  # 垂直布局
        vbox2 = QVBoxLayout()
        vbox3 = QVBoxLayout()
        vbox4 = QVBoxLayout()

        #两边空隙填充
        label1 = QLabel()
        label1.resize(50,50)
        label2 = QLabel()
        label2.resize(50, 50)
        vbox1.addWidget(label1)
        #vbox2.addWidget(label)
        vbox4.addWidget(start)
        vbox4.addWidget(quit)
        vbox3.addWidget(label2)
        # 按钮两边空隙填充
        label3 = QLabel()
        label3.resize(50, 50)
        label4 = QLabel()
        label4.resize(50, 50)
        hbox1 = QHBoxLayout()
        hbox1.addWidget(label3)
        hbox1.addLayout(vbox4)
        hbox1.addWidget(label4)
        #标题与两个按钮上下协调
        label5 = QLabel()
        label5.resize(1, 1)
        label6 = QLabel()
        label6.resize(1, 1)
        label7 = QLabel()
        label7.resize(1, 1)
        vbox2.addWidget(label5)
        vbox2.addWidget(label)
        vbox2.addWidget(label6)
        vbox2.addLayout(hbox1)
        vbox2.addWidget(label7)

        hbox = QHBoxLayout()
        hbox.addLayout(vbox1)
        hbox.addLayout(vbox2)
        hbox.addLayout(vbox3)
        widget.setLayout(hbox)

        self.setCentralWidget(widget)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            QApplication.postEvent(self, QEvent(174))
            event.accept()

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

    def quitClicked(self):
        reply = QMessageBox.question(self, 'Warning',
                                           'Are you sure to quit?', QMessageBox.Yes,
                                           QMessageBox.No)
        if reply == QMessageBox.Yes:
            quit()

    def startClicked(self):
        self.hide()
        Form = QDialog()
        ui = MainPage.MainPage(Form)
        Form.show()
        Form.exec_()
        self.show()

    def center(self):
        qr = self.frameGeometry()  # 得到该主窗口的矩形框架qr
        cp = QDesktopWidget().availableGeometry().center()  # 屏幕中间点的坐标cp
        qr.moveCenter(cp)  # 将矩形框架移至屏幕正中央
        self.move(qr.topLeft())  # 应用窗口移至矩形框架的左上角点


def main():
    app = QApplication(sys.argv)
    main = Home()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Page2:

# -*- coding: utf-8 -*-
import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

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

try:
    _encoding = QApplication.UnicodeUTF8


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


class MainPage(QMainWindow):
    def __init__(self, Dialog):
        super(MainPage, self).__init__()
        self.initUI(Dialog)

    def initUI(self, Dialog):
        Dialog.resize(650, 480)
        self.form = Dialog
        self.form.setObjectName("window")
        self.form.setStyleSheet ("background-image: url(background1.jpg)");
        self.form.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog)

        back = QPushButton("Back", Dialog)
        back.setGeometry(190, 210, 50, 25)
        back.clicked.connect(self.backClicked)

    def backClicked(self):
        self.form.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    Dialog = QDialog()
    ui = MainPage(Dialog)
    ui.show()
    sys.exit(app.exec_())

 

参考:http://blog.csdn.net/sollor525/article/details/40076395

posted @ 2017-10-18 16:19  Not-Bad  阅读(2478)  评论(0编辑  收藏  举报