目录
一、前言
二、界面设计
三、信号与槽设计
四、界面代码文件Ui_Test.py
五、调用界面文件call_Test.py
六、实测结果显示
信号(Signal)就是在特定情况下被发射的事件,例如PushButton常见的信号就是鼠标单击时发射的clicked()信号,槽(Slot)就是对信号响应的函数。
方法1: 界面添加
方法2: 代码手动添加
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(730, 478)
self.pushButton1 = QtWidgets.QPushButton(Form)
self.pushButton1.setGeometry(QtCore.QRect(180, 170, 93, 28))
self.pushButton1.setObjectName("pushButton1")
self.pushButton2 = QtWidgets.QPushButton(Form)
self.pushButton2.setGeometry(QtCore.QRect(380, 170, 93, 28))
self.pushButton2.setObjectName("pushButton2")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(100, 40, 91, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(190, 40, 311, 31))
self.textEdit.setObjectName("textEdit")
self.retranslateUi(Form)
#self.pushButton1.clicked.connect(self.textEdit.paste)
#self.pushButton2.clicked.connect(self.textEdit.clear)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton1.setText(_translate("Form", "显示"))
self.pushButton2.setText(_translate("Form", "清空"))
self.label.setText(_translate("Form", "显示内容:"))
#导入程序运行必须模块
import sys
#PyQt5中使用的基本控件都在PyQt5.QtWidgets模块中
from PyQt5.QtWidgets import QApplication, QMainWindow,QDialog,QWidget
#导入designer工具生成的login模块
from Ui_Test import Ui_Form
import Ui_Test
class My_Form(QWidget):
def __init__(self, parent=None):
super(My_Form, self).__init__(parent)
self.ui = Ui_Test.Ui_Form()
self.ui.setupUi(self)
def signal_slot(self):
self.ui.pushButton1.clicked.connect(self.display)
self.ui.pushButton2.clicked.connect(self.clear)
def display(self):
self.ui.textEdit.setText("hello")
def clear(self):
self.ui.textEdit.clear()
if __name__ == "__main__":
#固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
#初始化
myWin = My_Form()
myWin.signal_slot()
#将窗口控件显示在屏幕上
myWin.show()
#程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())