PyQty5—第二课:按钮与函数绑定(1)(附完整代码)

在上一节课中我们学会了PyQty5的安装,以及两个环境的配置,那么今天这节课我们学习一下窗口部件如按钮,和我们函数的绑定,这个学会了,我们就可以自己设计界面自己写函数,从而设计出自己的小工具

1、那么首先我们新建一个py文件,右击打开设计窗口

在这里插入图片描述

2、我们点击左边的【Buttons】拖动3个【Push Button】按钮,双击改一下名字

在这里插入图片描述

2、关闭按钮,我们先给他绑定一个关闭函数(这一步先照着做一下)

2-1点击左上角箭头的图标

在这里插入图片描述

2-2点击【关闭窗口】按钮,拖动到空白位置

在这里插入图片描述

2-3按照下图点击(F3退出)

在这里插入图片描述

3、改一下两个按钮的对象名(随便起的)

在这里插入图片描述
在这里插入图片描述

4、那么我们简易的小窗口就创建好了,我们【Ctrl+s】保存一下

在这里插入图片描述

接下来我们用上一节配置的环境,将刚刚设计的ui文件转换为py文件,会生成同名py文件

在这里插入图片描述
在这里插入图片描述

5、接下来我们添加创建对象的代码,记得改一下自己的类名

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(mainWindow)
    mainWindow.show()
    sys.exit(app.exec_())

在这里插入图片描述

视频演示,看起来没有上面问题

请添加图片描述

6、接下来我们将两个按钮绑定一下函数

6-1,我们先看一下我们的按钮

在这里插入图片描述

6-1,按钮绑定函数是:self.button.clicked.connect(绑定的函数),注意绑定的函数不带括号

在这里插入图片描述

# -*- coding: utf-8 -*-
import sys

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# 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_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)


        self.nhbutton = QtWidgets.QPushButton(Dialog)
        self.nhbutton.setGeometry(QtCore.QRect(90, 50, 201, 23))
        self.nhbutton.setObjectName("nhbutton")
        self.nhbutton.clicked.connect(self.print_nh)

        self.hellobutton = QtWidgets.QPushButton(Dialog)
        self.hellobutton.setGeometry(QtCore.QRect(90, 100, 201, 23))
        self.hellobutton.setObjectName("hellobutton")
        self.hellobutton.clicked.connect(self.print_hello_world)


        self.pushButton_3 = QtWidgets.QPushButton(Dialog)
        self.pushButton_3.setGeometry(QtCore.QRect(140, 150, 75, 23))
        self.pushButton_3.setObjectName("pushButton_3")

        self.retranslateUi(Dialog)
        self.pushButton_3.clicked.connect(Dialog.reject) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Dialog)


    def print_nh(self):
        print("你好")


    def print_hello_world(self):
        print("Hello World")

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.nhbutton.setText(_translate("Dialog", "点击按钮打印你好"))
        self.hellobutton.setText(_translate("Dialog", "点击按钮打印HelloWorld"))
        self.pushButton_3.setText(_translate("Dialog", "关闭窗口"))





if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(mainWindow)
    mainWindow.show()
    sys.exit(app.exec_())


7、搞定!

请添加图片描述

这是我们今天学习PyQty5的第 2 课,接下来我也会认真学习,

本节课我们学习了,如何将按钮绑定函数

希望对大家有帮助

致力于办公自动化的小小程序员一枚#

都看到这了,关注+点赞+收藏=不迷路!!

如果你想知道更多关于Python办公自动化的知识各位大佬给个关注吧!

猜你喜欢

转载自blog.csdn.net/weixin_42636075/article/details/132445908