应用
典型的,函数在执行时,要带上所有必要的参数进行调用。然后,有时参数可以在函数被调用之前提前获知。这种情况下,一个函数有一个或多个参数预先就能用上,以便函数能用更少的参数进行调用。
示例pyqt5中运用
# 通常在designer设计中想要触发按钮的事件我们可以用
ui.pushButton.clicked.connect(click_success) #click_success表示按钮事件触发的函数
上面这样写的话,不能传参,所以我们需要换一种方式。
# 方式一传参
可以通过lambda函数
# 方式二传参
通过functools中的partial,偏函数来处理
ui.pushButton.clicked.connect(partial(click_success, ui)) # 参数1触发的函数名,参数2要传递的参数
示例
from functools import partial
def add(a,b):
return a+b
result = partial(add, 10)
add(19)
# 使用partial先为函数add传递了一个参数,随后真正调用的时候又传递了一个函数
结果为:29
示例 汇率转换
# huilv.py文件,由ui文件生成
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'huilv.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 20, 131, 51))
self.label.setLineWidth(1)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(40, 80, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(160, 80, 51, 20))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(190, 80, 75, 23))
self.pushButton.setObjectName("pushButton")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(280, 80, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(400, 80, 54, 16))
self.label_3.setObjectName("label_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
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", "MainWindow"))
self.label.setText(_translate("MainWindow", "汇率转换器"))
self.label_2.setText(_translate("MainWindow", "USD"))
self.pushButton.setText(_translate("MainWindow", "转换"))
self.label_3.setText(_translate("MainWindow", "CNY"))
# main.py 主程序。启动,编写函数。实现ui和主程序的分离
import sys
import huilv
from functools import partial
from PyQt5.QtWidgets import QApplication, QMainWindow
def turn_result(ui):
# 获取文本框中的内容
input_txt = ui.lineEdit.text().strip()
# 运算结果
if input_txt.isdigit():
result = float(input_txt) * 6.7
# 将结果添加到结果输入框中
ui.lineEdit_2.setText(str(result))
if __name__ == '__main__':
app = QApplication(sys.argv)
mywin = QMainWindow()
ui = huilv.Ui_MainWindow()
ui.setupUi(mywin)
mywin.show()
# 使用partial可以给函数传递参数
ui.pushButton.clicked.connect(partial(turn_result, ui))
sys.exit(app.exec_())