0,引言
最近在帮课题组老师做一款石油专业数据处理与分析的窗口软件,出于促使自己学习新知识的目的,笔者选择采用刚刚才学会“hello world”的python。以下记录使用python实现窗口跳转的详细过程,便于今后查阅,也为大家提供参考。笔者才疏,如有笔误,请大佬指正。
1,环境配置
(1)pyQt_designer(下载地址:https://www.qt.io/download)
(2)PyCharm Edition(下载地址:http://www.jetbrains.com/pycharm/)
2,操作步骤
(1)使用pyQt_designer设计好相关的窗口,并生成python源码(具体操作见笔者上一篇博文https://blog.csdn.net/u013419318/article/details/84555529);
a,第一个窗口文件(登录窗口文件login.py)
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Login_Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(786, 362)
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(200, 30, 391, 31))
font = QtGui.QFont()
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label_3.setText(_translate("Form", "欢迎使用数据处理系统"))
self.groupBox.setTitle(_translate("Form", "用户登录"))
self.pushButton0.setText(_translate("Form", "注册"))
self.pushButton1.setText(_translate("Form", "登录"))
self.label1.setText(_translate("Form", "密 码:"))
self.label0.setText(_translate("Form", "用户名:"))
b,第二个窗口文件(主窗口文件mainForm.py)
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Main_Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1211, 717)
self.pushButton0 = QtWidgets.QPushButton(Dialog)
self.pushButton0.setGeometry(QtCore.QRect(250, 80, 111, 41))
font = QtGui.QFont()
font.setFamily("华文中宋")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "数据计算程序"))
self.pushButton0.setText(_translate("Dialog", "选择源文件"))
c,第三个窗口文件(弹出窗口computeModule0_form.py)
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form0(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(702, 361)
self.pushButton1 = QtWidgets.QPushButton(Form)
self.pushButton1.setGeometry(QtCore.QRect(370, 70, 81, 31))
font = QtGui.QFont()
font.setFamily("华文中宋")
font.setBold(False)
font.setWeight(50)
self.pushButton1.setFont(font)
self.pushButton1.setObjectName("pushButton1")
self.label3 = QtWidgets.QLabel(Form)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton1.setText(_translate("Form", "计算百分比"))
self.label3.setText(_translate("Form", "轴向位移2"))
self.label4.setText(_translate("Form", "圆周位移"))
d,第四个窗口文件(弹出窗口computeModule0_form.py)
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form1(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(704, 361)
self.label2 = QtWidgets.QLabel(Form)
self.label2.setGeometry(QtCore.QRect(280, 210, 54, 12))
font = QtGui.QFont()
font.setFamily("等线")
self.label2.setFont(font)
self.label2.setObjectName("label2")
self.label4 = QtWidgets.QLabel(Form)
self.label4.setGeometry(QtCore.QRect(480, 210, 54, 12))
font = QtGui.QFont()
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label2.setText(_translate("Form", "轴向位移1"))
self.label4.setText(_translate("Form", "圆周位移"))
self.pushButton3.setText(_translate("Form", "计算泊松比"))
self.pushButton4.setText(_translate("Form", "计算弹性模量"))
(2)新建窗口类,搭建窗口之间的关系;
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import QBrush, QColor, QFont
from tkinter import *
class computeModule_0_window(QtWidgets.QMainWindow,Ui_Form0):
def __init__(self):
super(computeModule_0_window,self).__init__()
self.setupUi(self)
self.setWindowTitle('自动获取拐点计算模块'
self.pushButton0.clicked.connect(self.get_inflectionPoint_button0)
window_pale = QtGui.QPalette()
window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap("background_img/img2.jpg")))
self.setPalette(window_pale)
class computeModule_1_window(QtWidgets.QMainWindow,Ui_Form1):
def __init__(self):
super(computeModule_1_window,self).__init__()
self.setupUi(self)
self.setWindowTitle('鼠标获取拐点计算模块')
self.pushButton0.clicked.connect(self.get_inflectionPoint_button0)
window_pale = QtGui.QPalette()
window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap("background_img/img2.jpg")))
self.setPalette(window_pale)
class mainWindow(Main_Ui_Dialog,QWidget):
def __init__(self):
super(mainWindow,self).__init__()
self.setupUi(self)
self.setWindowTitle('数据处理程序')
self.pushButton0.clicked.connect(self.open_file_button0)
window_pale = QtGui.QPalette()
window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap("background_img/img2.jpg")))
self.setPalette(window_pale)
class loginWindow(Login_Ui_Form,QWidget):
def __init__(self):
super(loginWindow,self).__init__()
self.setupUi(self)
self.setWindowTitle('数据处理程序')
self.pushButton1.clicked.connect(self.login_button1)
window_pale = QtGui.QPalette()
window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap("background_img/img.jpg")))
self.setPalette(window_pale)
def login_button1(self):
if self.lineEdit0.text() == '':
QMessageBox.warning(self,"警告!","用户名不能为空!",QMessageBox.Yes)
return
if self.lineEdit0.text().strip() == '':
QMessageBox.warning(self,"警告!","密码不能为空!",QMessageBox.Yes)
return
if (self.lineEdit0.text().strip() == "u" and self.lineEdit1.text() == "1"):
self.hide()
self.dia = mainWindow()
self.dia.show()
else:
QMessageBox.warning(self,"警告!","用户名或者密码错误!",QMessageBox.Yes)
self.lineEdit0.clear()
self.lineEdit1.clear()
self.lineEdit0.setFocus()
return
(3)‘main’函数开启第一个登录窗口。
if __name__ == '__main__':
app = QApplication(sys.argv)
w = loginWindow()
w.show()
sys.exit(app.exec_())
3,总结
以上代码由于涉及到保密的原因,没办法完全展示和记录,但是就跳转方式来说,相信本文已经比较清楚明了了。这样的窗口跳转方式非常简单,适合python初学者。