基于GUI的应用程序是事件驱动的。函数或方法按照用户的操作(例如点击按钮、从集合中选择项目或点击鼠标等)来执行,这些操作被称为 事件 。
用于构建GUI界面的小部件充当这些事件的来源。每个PyQt小部件都是从QObject类派生而来,设计为响应一个或多个事件而发出 信号 。信号本身不执行任何操作,而是与一个 槽 连接。槽可以是任何可调用的Python函数。
1.使用Qt Designer的信号/槽编辑器
首先,从编辑菜单中选择编辑信号/槽(或按下F4)。然后用鼠标突出显示按钮,并将光标拖向文本框。
当鼠标释放时,会显示一个对话框,其中显示按钮的信号和槽的方法。选择clicked信号和clear()方法。
**clear:**清除对应框中内容
**copy:**复制(搭配全选一起使用,先全选框,再复制)
**cut:**剪切(搭配全选一起使用,先全选框,再复制)
**paste:**粘贴(搭配全选一起使用,先全选框,再粘贴,要不然就是追加写入)
**redo:**重做(与撤销搭配使用,恢复刚刚的输入)
**selectall:**全选
**undo:**撤销(撤销刚刚的输入)
源代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.10
#
# 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.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(220, 20, 113, 21))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(60, 20, 93, 28))
self.pushButton.setObjectName("pushButton")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(220, 60, 113, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(60, 60, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(220, 100, 113, 21))
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(60, 100, 93, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(220, 150, 113, 21))
self.lineEdit_4.setObjectName("lineEdit_4")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(60, 150, 93, 28))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(110, 290, 93, 28))
self.pushButton_5.setObjectName("pushButton_5")
self.lineEdit_6 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_6.setGeometry(QtCore.QRect(220, 200, 113, 21))
self.lineEdit_6.setObjectName("lineEdit_6")
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setGeometry(QtCore.QRect(60, 200, 93, 28))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_9.setGeometry(QtCore.QRect(10, 290, 93, 28))
self.pushButton_9.setObjectName("pushButton_9")
self.lineEdit_8 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_8.setGeometry(QtCore.QRect(220, 290, 113, 21))
self.lineEdit_8.setObjectName("lineEdit_8")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.lineEdit.clear) # type: ignore
self.pushButton_4.clicked.connect(self.lineEdit_4.paste) # type: ignore
self.pushButton_6.clicked.connect(self.lineEdit_6.selectAll) # type: ignore
self.pushButton_9.clicked.connect(self.lineEdit_8.undo) # type: ignore
self.pushButton_6.clicked.connect(self.lineEdit_6.copy) # type: ignore
self.pushButton_2.clicked.connect(self.lineEdit_2.selectAll) # type: ignore
self.pushButton_2.clicked.connect(self.lineEdit_2.copy) # type: ignore
self.pushButton_5.clicked.connect(self.lineEdit_8.redo) # type: ignore
self.pushButton_3.clicked.connect(self.lineEdit_3.selectAll) # type: ignore
self.pushButton_3.clicked.connect(self.lineEdit_3.cut) # type: ignore
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lineEdit.setText(_translate("MainWindow", "1"))
self.pushButton.setText(_translate("MainWindow", "清除"))
self.lineEdit_2.setText(_translate("MainWindow", "2"))
self.pushButton_2.setText(_translate("MainWindow", "复制"))
self.lineEdit_3.setText(_translate("MainWindow", "3"))
self.pushButton_3.setText(_translate("MainWindow", "剪切"))
self.lineEdit_4.setText(_translate("MainWindow", "4"))
self.pushButton_4.setText(_translate("MainWindow", "粘贴"))
self.pushButton_5.setText(_translate("MainWindow", "重做"))
self.lineEdit_6.setText(_translate("MainWindow", "6"))
self.pushButton_6.setText(_translate("MainWindow", "全选"))
self.pushButton_9.setText(_translate("MainWindow", "撤销"))
self.lineEdit_8.setText(_translate("MainWindow", "8"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())