Take addition, subtraction, multiplication and division as an example, use pycharm
1. Design interface
- design home page
Click tools->External Tools->Qt Designer, add four buttons, page 1, page 2, page 3, page 4, save as shouye.ui
Use pyuic5 -o shouye.py shouye.ui -x in the terminal to convert the ui file to a .py file
-
page 1
Realize the addition of two numbers, number 1 and number 2 are labels, the number input box is LineEdit, the result is PushButton, and the result output box is TextEdit.
Add link relationship:
Add some code in setupUi(), where add is the function name.
self.pushButton.clicked.connect(self.add)
Get the content of LineEdit
self.lineEdit.text()
Get the content and input content of TextEdit
self.textEdit.toPlainText()
self.textEdit.setText('要输入的内容')
- Page 2, Page 3, Page 4 Same as above
2. Jump between different pages
Import the py files of each page in the main interface
from add import * # py文件名
from sub import *
from mul import *
from dev import *
Change the object and QtWidgets.QMainWindow of the main interface, and modify the class names of each sub-interface so as not to overlap with the class names of the main interface, and make the following definitions:
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
class Add(QtWidgets.QMainWindow):
def __init__(self):
super(Add,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
class Sub(QtWidgets.QMainWindow):
def __init__(self):
super(Sub,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
class Mul(QtWidgets.QMainWindow):
def __init__(self):
super(Mul,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
class Dev(QtWidgets.QMainWindow):
def __init__(self):
super(Dev,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
Set the home page button to jump
self.pushButton.clicked.connect(self.yemian1)
self.pushButton_2.clicked.connect(self.yemian2)
self.pushButton_3.clicked.connect(self.yemian3)
self.pushButton_4.clicked.connect(self.yemian4)
And design each page jump function
def yemian1(self):
ui_add.show()
MainWindow.close()
def yemian2(self):
ui_sub.show()
MainWindow.close()
def yemian3(self):
ui_mul.show()
MainWindow.close()
def yemian4(self):
ui_dev.show()
MainWindow.close()
Set the object of each page jump in the page
ui_add=Add() # 类名
ui_sub=Sub()
ui_mul=Mul()
ui_dev=Dev()