博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
No.4 PyQt学习(页面跳转)
阅读量:6622 次
发布时间:2019-06-25

本文共 9782 字,大约阅读时间需要 32 分钟。

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

# -*- coding: utf-8 -*-import sysfrom PyQt4.QtGui import *from PyQt4.QtCore import *import MainPageclass 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("%s" %("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 sysfrom PyQt4.QtGui import *from PyQt4.QtCore import *try:    _fromUtf8 = QString.fromUtf8except AttributeError:    def _fromUtf8(s):        return stry:    _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 sysfrom PyQt4.QtGui import *from PyQt4.QtCore import *import MainPageclass 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("%s" %("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 sysfrom PyQt4.QtGui import *from PyQt4.QtCore import *try:    _fromUtf8 = QString.fromUtf8except AttributeError:    def _fromUtf8(s):        return stry:    _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

你可能感兴趣的文章
本地wampserver如何配置伪静态
查看>>
操作系统面试
查看>>
【转载】支持向量机SVM(一)
查看>>
C#串口通信实例
查看>>
小程序数据返回时刷新当前页面数据
查看>>
MySQL数据故障时备份与恢复
查看>>
Nlopt优化函数库,用法举例
查看>>
海思 core 电压动态调整
查看>>
jFinal 关联数据库操作
查看>>
团队冲刺第二天
查看>>
sed删除空行和开头的空格和tab键
查看>>
php扩展安装
查看>>
Windows与Linux之间的文件自动同步
查看>>
What a C programmer should know about memory
查看>>
MySQL备份账号权限
查看>>
15个重要的Android代码
查看>>
(转)android 牛人必修 ant 编译android工程
查看>>
求最大公约数与最小公倍数
查看>>
C# Winform 跨线程更新UI控件常用方法总结(转)
查看>>
eclipse菜单栏不显示 + the system is running in lou-graphics mode问题
查看>>