一、使用QT Desinger设置信号与槽

1.使用系统中的槽函数

点击QT Desinger中的编辑信号与槽,

Python界面demo python界面开发_Python界面demo

 可拖动信号线,链接至标签。

Python界面demo python界面开发_信号与槽_02

 在弹出的对话框中分别选择要链接的信号和槽。即完成信号与槽的链接。

Python界面demo python界面开发_QT_03

2.定义自己的槽函数

在需要实现系统给定的功能以外的情况下,需要自己定义槽函数。把信号线拖到空白处,选择信号函数,点击槽函数下面的编辑按钮,再点击绿色的加号,输入槽函数名,就可创建出自己的槽函数。

Python界面demo python界面开发_python_04

 在ui文件中创建并链接好槽函数之后,还需要在py文件中写槽函数的功能,才能真正实现功能。下文会介绍py文件中槽函数的编写。

二、ui文件转换为py文件

建立一个工程,输入以下代码:

import os
import os.path
import sys

dir = 'C:\\Users\\86191\\PycharmProjects\\uiFilesToPyFiles'

#列出目录下的所有UI文件
def listUiFile():
	list = []
	files = os.listdir(dir)
	for filename in files:
		if os.path.splitext(filename)[1] == '.ui':
			list.append(filename)

	return list

#把扩展名为.ui的文件改成扩展名为.py的文件
def transPyFile(filename):
	return os.path.splitext(filename)[0] + '.py'

#调用系统命令把UI文件转换成Python文件
def runMain():
	list = listUiFile()
	for uifile in list:
		pyfile = transPyFile(uifile)
		cmd = 'pyuic5 -o {pyfile} {uifile}'.format(pyfile=pyfile,uifile=uifile)
		os.system(cmd)

if __name__ == "__main__":
	runMain()

可以把dir目录下的文件全部转换为py文件。不过需要转换的文件和转换好的文件需要在工程的根目录下,不然会报错。

三、在py文件中写槽函数

我要写的槽函数简单实现了一个整数的加法功能。界面文件如下图:控件摆的有点乱。

Python界面demo python界面开发_Python界面demo_05

 在界面文件中,一共设置了3个信号与槽的链接。

Python界面demo python界面开发_Python界面demo_06

 实现加法的是自定义的slot2函数,其他两个测试用的。

界面文件转换的py文件如下:

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.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(680, 50, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(480, 220, 93, 28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(470, 140, 113, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(470, 180, 113, 21))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(250, 210, 91, 19))
        self.checkBox.setObjectName("checkBox")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(240, 230, 104, 87))
        self.textEdit.setObjectName("textEdit")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(150, 240, 72, 15))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(390, 140, 72, 15))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(390, 180, 72, 15))
        self.label_3.setObjectName("label_3")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 27))
        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.checkBox.clicked['bool'].connect(self.textEdit.setVisible)
        self.pushButton.clicked.connect(MainWindow.slot1)
        self.pushButton_2.clicked.connect(MainWindow.slot2)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_2.setText(_translate("MainWindow", "加法"))
        self.checkBox.setText(_translate("MainWindow", "CheckBox"))
        self.label.setText(_translate("MainWindow", "计算结果"))
        self.label_2.setText(_translate("MainWindow", "数字1"))
        self.label_3.setText(_translate("MainWindow", "数字2"))

自定的slot1,slot2需要自己写,写完之后,整体代码如下:

import os
import os.path
import sys
import string
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.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(680, 50, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(480, 220, 93, 28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(470, 140, 113, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(470, 180, 113, 21))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(250, 210, 91, 19))
        self.checkBox.setObjectName("checkBox")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(240, 230, 104, 87))
        self.textEdit.setObjectName("textEdit")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(150, 240, 72, 15))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(390, 140, 72, 15))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(390, 180, 72, 15))
        self.label_3.setObjectName("label_3")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 27))
        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.checkBox.clicked['bool'].connect(self.textEdit.setVisible)
        self.pushButton.clicked.connect(self.slot1)
        self.pushButton_2.clicked.connect(self.slot2)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def slot1(self):
        print("你好,这是第一个槽函数")

    def slot2(self):
        print("你好,这是第二个槽函数")
        s1 = self.lineEdit.text()
        n1 = int(s1)

        print(n1)
        s2 = self.lineEdit_2.text()
        n2 = int(s2)
        print(n2)
        n3 = n1 + n2
        print(n3)
        str1 = '%d' % n3
        print(str1)
        self.textEdit.setText(str1)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_2.setText(_translate("MainWindow", "加法"))
        self.checkBox.setText(_translate("MainWindow", "CheckBox"))
        self.label.setText(_translate("MainWindow", "计算结果"))
        self.label_2.setText(_translate("MainWindow", "数字1"))
        self.label_3.setText(_translate("MainWindow", "数字2"))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)  # 创建一个QApplication,即将开发的软件app
    MainWindow = QtWidgets.QMainWindow()  # QMainWindow装载需要的组件
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)  # 执行类中的setupUi方法
    MainWindow.show()
    sys.exit(app.exec_())  # exit()或点击按钮退出app

需要注意的是,在ui文件生成的py文件的连接语句需要改动,如果不改报错。

self.pushButton.clicked.connect(MainWindow.slot1)
self.pushButton_2.clicked.connect(MainWindow.slot2)

改成下面的:

self.pushButton.clicked.connect(self.slot1)
        self.pushButton_2.clicked.connect(self.slot2)