目录
一.单文件初始化代码
当我们拿到一个新文件的时候,我们为了便于修改,就不在它创建的类外面写我们的初始化代码,所以我们就得创建一个类,然后继承即可。
class function(Ui_MainWindow):
def __init__(self):
self.setupUi(MainWindow)#这是UI文件的绘制UI函数
self.init()
MainWindow.show()#让窗口显示我们创建好的GUI
def init(self):
#下面放置我们的操作函数即可
然后,我们还是要处理我们的主程序(Python3)
import sys
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = function()
sys.exit(app.exec_())
至此,一个代码好看一点的程序,我们就写出来了。完整的计算器代码如下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(358, 287)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setEnabled(True)
self.pushButton.setGeometry(QtCore.QRect(50, 60, 61, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setEnabled(True)
self.pushButton_2.setGeometry(QtCore.QRect(110, 60, 61, 51))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setEnabled(True)
self.pushButton_3.setGeometry(QtCore.QRect(170, 60, 61, 51))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setEnabled(True)
self.pushButton_4.setGeometry(QtCore.QRect(50, 110, 61, 51))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setEnabled(True)
self.pushButton_5.setGeometry(QtCore.QRect(110, 110, 61, 51))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setEnabled(True)
self.pushButton_6.setGeometry(QtCore.QRect(170, 110, 61, 51))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setEnabled(True)
self.pushButton_7.setGeometry(QtCore.QRect(50, 160, 61, 51))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_8.setEnabled(True)
self.pushButton_8.setGeometry(QtCore.QRect(110, 160, 61, 51))
self.pushButton_8.setObjectName("pushButton_8")
self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_9.setEnabled(True)
self.pushButton_9.setGeometry(QtCore.QRect(170, 160, 61, 51))
self.pushButton_9.setObjectName("pushButton_9")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 20, 341, 31))
self.lineEdit.setText("")
self.lineEdit.setMaxLength(99999999)
self.lineEdit.setObjectName("lineEdit")
self.pushButton_10 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_10.setEnabled(True)
self.pushButton_10.setGeometry(QtCore.QRect(230, 60, 61, 51))
self.pushButton_10.setObjectName("pushButton_10")
self.pushButton_11 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_11.setEnabled(True)
self.pushButton_11.setGeometry(QtCore.QRect(230, 110, 61, 51))
self.pushButton_11.setObjectName("pushButton_11")
self.pushButton_12 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_12.setEnabled(True)
self.pushButton_12.setGeometry(QtCore.QRect(230, 160, 61, 51))
self.pushButton_12.setObjectName("pushButton_12")
self.pushButton_13 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_13.setEnabled(True)
self.pushButton_13.setGeometry(QtCore.QRect(230, 210, 61, 51))
self.pushButton_13.setObjectName("pushButton_13")
self.pushButton_14 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_14.setEnabled(True)
self.pushButton_14.setGeometry(QtCore.QRect(50, 210, 61, 51))
self.pushButton_14.setObjectName("pushButton_14")
self.pushButton_15 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_15.setEnabled(True)
self.pushButton_15.setGeometry(QtCore.QRect(170, 210, 61, 51))
self.pushButton_15.setObjectName("pushButton_15")
self.pushButton_16 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_16.setEnabled(True)
self.pushButton_16.setGeometry(QtCore.QRect(110, 210, 61, 51))
self.pushButton_16.setObjectName("pushButton_16")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "这是测试计算器"))
self.pushButton.setText(_translate("MainWindow", "1"))
self.pushButton_2.setText(_translate("MainWindow", "2"))
self.pushButton_3.setText(_translate("MainWindow", "3"))
self.pushButton_4.setText(_translate("MainWindow", "4"))
self.pushButton_5.setText(_translate("MainWindow", "5"))
self.pushButton_6.setText(_translate("MainWindow", "6"))
self.pushButton_7.setText(_translate("MainWindow", "7"))
self.pushButton_8.setText(_translate("MainWindow", "8"))
self.pushButton_9.setText(_translate("MainWindow", "9"))
self.pushButton_10.setText(_translate("MainWindow", "+"))
self.pushButton_11.setText(_translate("MainWindow", "-"))
self.pushButton_12.setText(_translate("MainWindow", "×"))
self.pushButton_13.setText(_translate("MainWindow", "÷"))
self.pushButton_14.setText(_translate("MainWindow", "←"))
self.pushButton_15.setText(_translate("MainWindow", "="))
self.pushButton_16.setText(_translate("MainWindow", "0"))
class function(Ui_MainWindow):
def __init__(self):
self.setupUi(MainWindow)
self.init()
MainWindow.show()
def init(self):
self.pushButton.clicked.connect(self.click1)
self.pushButton_2.clicked.connect(self.click2)
self.pushButton_3.clicked.connect(self.click3)
self.pushButton_4.clicked.connect(self.click4)
self.pushButton_5.clicked.connect(self.click5)
self.pushButton_6.clicked.connect(self.click6)
self.pushButton_7.clicked.connect(self.click7)
self.pushButton_8.clicked.connect(self.click8)
self.pushButton_9.clicked.connect(self.click9)
self.pushButton_16.clicked.connect(self.click0)
self.pushButton_10.clicked.connect(self.clickp)
self.pushButton_11.clicked.connect(self.clickm)
self.pushButton_12.clicked.connect(self.clickc)
self.pushButton_13.clicked.connect(self.clickd)
self.pushButton_14.clicked.connect(self.clickclear)
self.pushButton_15.clicked.connect(self.clicke)
def click0(self):
self.lineEdit.insert('0')
def click1(self):
self.lineEdit.insert('1')
def click2(self):
self.lineEdit.insert('2')
def click3(self):
self.lineEdit.insert('3')
def click4(self):
self.lineEdit.insert('4')
def click5(self):
self.lineEdit.insert('5')
def click6(self):
self.lineEdit.insert('6')
def click7(self):
self.lineEdit.insert('7')
def click8(self):
self.lineEdit.insert('8')
def click9(self):
self.lineEdit.insert('9')
def clickp(self):
self.lineEdit.insert('+')
def clickm(self):
self.lineEdit.insert('-')
def clickc(self):
self.lineEdit.insert('*')
def clickd(self):
self.lineEdit.insert('/')
def clickclear(self):
self.lineEdit.setText("")
def clicke(self):
print(eval(self.lineEdit.text()))
text = self.lineEdit.text()
self.lineEdit.setText('%s= %f' % (text, eval(text)))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = function()
sys.exit(app.exec_())
二.多文件Demo
首先,我们与UI文件的相同目录下,创建我们的程序文件,然后,导入我们的UI文件
import sys
from cv.UI_calc import *
假设我们有一个文件夹叫:cv,这个cv文件夹里面有UI_calc.py(界面)和main.py(本程序),那么我们在main.py里面引用UI_calc里面的文件的所有东西的代码就是
from cv.UI_calc import *
此后和建立单文件Demo差不多,我们的功能设置:
class function(Ui_MainWindow):
def __init__(self):
self.setupUi(MainWindow)
self.init()#这里是功能初始化,代码自己写
MainWindow.show()
然后我们再设置我们的main函数入口:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = function()
sys.exit(app.exec_())
好了,我们的Demo就写好了,这里是两个文件的完整代码
UI_calc:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(358, 287)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setEnabled(True)
self.pushButton.setGeometry(QtCore.QRect(50, 60, 61, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setEnabled(True)
self.pushButton_2.setGeometry(QtCore.QRect(110, 60, 61, 51))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setEnabled(True)
self.pushButton_3.setGeometry(QtCore.QRect(170, 60, 61, 51))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setEnabled(True)
self.pushButton_4.setGeometry(QtCore.QRect(50, 110, 61, 51))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setEnabled(True)
self.pushButton_5.setGeometry(QtCore.QRect(110, 110, 61, 51))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setEnabled(True)
self.pushButton_6.setGeometry(QtCore.QRect(170, 110, 61, 51))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setEnabled(True)
self.pushButton_7.setGeometry(QtCore.QRect(50, 160, 61, 51))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_8.setEnabled(True)
self.pushButton_8.setGeometry(QtCore.QRect(110, 160, 61, 51))
self.pushButton_8.setObjectName("pushButton_8")
self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_9.setEnabled(True)
self.pushButton_9.setGeometry(QtCore.QRect(170, 160, 61, 51))
self.pushButton_9.setObjectName("pushButton_9")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 20, 341, 31))
self.lineEdit.setText("")
self.lineEdit.setMaxLength(99999999)
self.lineEdit.setObjectName("lineEdit")
self.pushButton_10 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_10.setEnabled(True)
self.pushButton_10.setGeometry(QtCore.QRect(230, 60, 61, 51))
self.pushButton_10.setObjectName("pushButton_10")
self.pushButton_11 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_11.setEnabled(True)
self.pushButton_11.setGeometry(QtCore.QRect(230, 110, 61, 51))
self.pushButton_11.setObjectName("pushButton_11")
self.pushButton_12 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_12.setEnabled(True)
self.pushButton_12.setGeometry(QtCore.QRect(230, 160, 61, 51))
self.pushButton_12.setObjectName("pushButton_12")
self.pushButton_13 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_13.setEnabled(True)
self.pushButton_13.setGeometry(QtCore.QRect(230, 210, 61, 51))
self.pushButton_13.setObjectName("pushButton_13")
self.pushButton_14 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_14.setEnabled(True)
self.pushButton_14.setGeometry(QtCore.QRect(50, 210, 61, 51))
self.pushButton_14.setObjectName("pushButton_14")
self.pushButton_15 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_15.setEnabled(True)
self.pushButton_15.setGeometry(QtCore.QRect(170, 210, 61, 51))
self.pushButton_15.setObjectName("pushButton_15")
self.pushButton_16 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_16.setEnabled(True)
self.pushButton_16.setGeometry(QtCore.QRect(110, 210, 61, 51))
self.pushButton_16.setObjectName("pushButton_16")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "这是测试计算器"))
self.pushButton.setText(_translate("MainWindow", "1"))
self.pushButton_2.setText(_translate("MainWindow", "2"))
self.pushButton_3.setText(_translate("MainWindow", "3"))
self.pushButton_4.setText(_translate("MainWindow", "4"))
self.pushButton_5.setText(_translate("MainWindow", "5"))
self.pushButton_6.setText(_translate("MainWindow", "6"))
self.pushButton_7.setText(_translate("MainWindow", "7"))
self.pushButton_8.setText(_translate("MainWindow", "8"))
self.pushButton_9.setText(_translate("MainWindow", "9"))
self.pushButton_10.setText(_translate("MainWindow", "+"))
self.pushButton_11.setText(_translate("MainWindow", "-"))
self.pushButton_12.setText(_translate("MainWindow", "×"))
self.pushButton_13.setText(_translate("MainWindow", "÷"))
self.pushButton_14.setText(_translate("MainWindow", "←"))
self.pushButton_15.setText(_translate("MainWindow", "="))
self.pushButton_16.setText(_translate("MainWindow", "0"))
main.py:
import sys
from cv.UI_calc import *
class function(Ui_MainWindow):
def __init__(self):
self.setupUi(MainWindow)
self.init()
MainWindow.show()
def init(self):
self.pushButton.clicked.connect(self.click1)
self.pushButton_2.clicked.connect(self.click2)
self.pushButton_3.clicked.connect(self.click3)
self.pushButton_4.clicked.connect(self.click4)
self.pushButton_5.clicked.connect(self.click5)
self.pushButton_6.clicked.connect(self.click6)
self.pushButton_7.clicked.connect(self.click7)
self.pushButton_8.clicked.connect(self.click8)
self.pushButton_9.clicked.connect(self.click9)
self.pushButton_16.clicked.connect(self.click0)
self.pushButton_10.clicked.connect(self.clickp)
self.pushButton_11.clicked.connect(self.clickm)
self.pushButton_12.clicked.connect(self.clickc)
self.pushButton_13.clicked.connect(self.clickd)
self.pushButton_14.clicked.connect(self.clickclear)
self.pushButton_15.clicked.connect(self.clicke)
def click0(self):
self.lineEdit.insert('0')
def click1(self):
self.lineEdit.insert('1')
def click2(self):
self.lineEdit.insert('2')
def click3(self):
self.lineEdit.insert('3')
def click4(self):
self.lineEdit.insert('4')
def click5(self):
self.lineEdit.insert('5')
def click6(self):
self.lineEdit.insert('6')
def click7(self):
self.lineEdit.insert('7')
def click8(self):
self.lineEdit.insert('8')
def click9(self):
self.lineEdit.insert('9')
def clickp(self):
self.lineEdit.insert('+')
def clickm(self):
self.lineEdit.insert('-')
def clickc(self):
self.lineEdit.insert('*')
def clickd(self):
self.lineEdit.insert('/')
def clickclear(self):
self.lineEdit.setText("")
def clicke(self):
print(eval(self.lineEdit.text()))
text = self.lineEdit.text()
self.lineEdit.setText('%s= %f' % (text, eval(text)))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = function()
sys.exit(app.exec_())
三.Demo快速复制:
1.单文件
import sys
class function(Ui_MainWindow):
def __init__(self):
self.setupUi(MainWindow)
self.init()
MainWindow.show()
def init(self):
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = function()
sys.exit(app.exec_())
2.多文件
import sys
from cv.ui import *
class function(Ui_MainWindow):
def __init__(self):
self.setupUi(MainWindow)
self.init()
MainWindow.show()
def init(self):
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = function()
sys.exit(app.exec_())
完美