使用pyqt构建桌面图形应用

作者:xin 时间:22-11-03 20:41:16 阅读数:1464人阅读

使用python语言也是可以编写Windows桌面应用的(尽管python并没有那么适合搞这个,并且相较于部分语言,效率也较低,但是却能让只会python的小伙伴不用学习其他语言也可以做出自己的桌面应用)。

要使用python来编写桌面应用,可以使用的库特别的多,这其中最方便的就属pyqt了,因为pyqt是可以使用qt设计师来直接拖放组件的,能大大提高编程的效率。

使用流程

1、下载qt设计师软件,可以到官网下载,其他平台也能搜索到。

2、在qt设计师中拖放组件,设计成自己想要的样子之后,生成python代码,将代码复制到python IDE当中去,之后对代码进行修改即可。

源码分享

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'wenan.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.

# 导入相关的包
import json
import sys, time, threading
import pymysql, pyperclip
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication


class Ui_MainWindow(object):
    # 设置Ui界面的方法
    def setupUi(self, MainWindow):
        # 设置窗口标题
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(700, 739)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("logo.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setMinimumSize(QtCore.QSize(0, 70))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.plainTextEdit.setFont(font)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.horizontalLayout_2.addWidget(self.plainTextEdit)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(10)
        sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
        self.pushButton.setSizePolicy(sizePolicy)
        self.pushButton.setMaximumSize(QtCore.QSize(16777215, 70))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout_2.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setMaximumSize(QtCore.QSize(16777215, 70))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_2.addWidget(self.pushButton_2)
        self.verticalLayout_2.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.plainTextEdit_2 = QtWidgets.QPlainTextEdit(self.centralwidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.plainTextEdit_2.setFont(font)
        self.plainTextEdit_2.setPlainText("")
        self.plainTextEdit_2.setObjectName("plainTextEdit_2")
        self.horizontalLayout_3.addWidget(self.plainTextEdit_2)
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setMaximumSize(QtCore.QSize(16777215, 70))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout_3.addWidget(self.pushButton_3)
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setMaximumSize(QtCore.QSize(16777215, 70))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setObjectName("pushButton_4")
        self.horizontalLayout_3.addWidget(self.pushButton_4)
        self.verticalLayout_2.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.plainTextEdit_3 = QtWidgets.QPlainTextEdit(self.centralwidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.plainTextEdit_3.setFont(font)
        self.plainTextEdit_3.setObjectName("plainTextEdit_3")
        self.horizontalLayout_4.addWidget(self.plainTextEdit_3)
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setMaximumSize(QtCore.QSize(16777215, 70))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setObjectName("pushButton_5")
        self.horizontalLayout_4.addWidget(self.pushButton_5)
        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setMaximumSize(QtCore.QSize(16777215, 70))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.pushButton_6.setFont(font)
        self.pushButton_6.setObjectName("pushButton_6")
        self.horizontalLayout_4.addWidget(self.pushButton_6)
        self.verticalLayout_2.addLayout(self.horizontalLayout_4)
        self.verticalLayout_3.addLayout(self.verticalLayout_2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 700, 26))
        self.menubar.setObjectName("menubar")
        self.menu = QtWidgets.QMenu(self.menubar)
        self.menu.setObjectName("menu")
        MainWindow.setMenuBar(self.menubar)
        self.actiondu = QtWidgets.QAction(MainWindow)
        self.actiondu.setObjectName("actiondu")
        self.menu.addAction(self.actiondu)
        self.menubar.addAction(self.menu.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "获取文案"))
        self.label.setText(_translate("MainWindow", "欢迎你,小佩奇"))
        self.pushButton.setText(_translate("MainWindow", "复制"))
        self.pushButton_2.setText(_translate("MainWindow", "删除"))
        self.pushButton_3.setText(_translate("MainWindow", "复制"))
        self.pushButton_4.setText(_translate("MainWindow", "删除"))
        self.pushButton_5.setText(_translate("MainWindow", "复制"))
        self.pushButton_6.setText(_translate("MainWindow", "删除"))
        self.menu.setTitle(_translate("MainWindow", "数据"))
        self.actiondu.setText(_translate("MainWindow", "读取文案"))

    def connect(self):
        # 绑定3个复制按钮的函数
        self.pushButton.clicked.connect(self.copy1)
        self.pushButton_3.clicked.connect(self.copy3)
        self.pushButton_5.clicked.connect(self.copy5)
        self.pushButton_2.clicked.connect(self.del2)
        self.pushButton_4.clicked.connect(self.del4)
        self.pushButton_6.clicked.connect(self.del6)
        # 菜单绑定函数
        self.actiondu.triggered.connect(self.con_mysql)
    # 复制编辑框1 内容
    def copy1(self):
        neirong1 = self.plainTextEdit.toPlainText()
        pyperclip.copy(neirong1)
        text = pyperclip.paste()
        if text == neirong1:
            self.label.setText('复制成功')
            timer = threading.Timer(5, self.clear)
            timer.start()

    # 复制编辑框2 内容
    def copy3(self):
        neirong3 = self.plainTextEdit_2.toPlainText()
        pyperclip.copy(neirong3)
        text = pyperclip.paste()

        if text == neirong3:
            self.label.setText('复制成功')
            timer = threading.Timer(5, self.clear)
            timer.start()

    # 复制编辑框3 内容
    def copy5(self):
        neirong5 = self.plainTextEdit_3.toPlainText()
        pyperclip.copy(neirong5)
        text = pyperclip.paste()

        if text == neirong5:
            self.label.setText('复制成功')
            timer = threading.Timer(5, self.clear)
            timer.start()
    #删除第1个文案
    def del2(self):
        with open('D:/ids.json','r') as ids:
            ids =json.load(ids)
            id_checked=ids[0]
        conn = pymysql.connect(host='106.12.106.246', user='gong', password='GAAPAEZcj3c684aA', database='gong',
                               charset='utf8')
        cursor = conn.cursor()
        sql='DELETE FROM `wenan` WHERE id='+str(id_checked)
        cursor.execute(sql)

        sql = 'SELECT * FROM `wenan` LIMIT 3'
        cursor.execute(sql)
        results = cursor.fetchmany(3)

        # 读取对应的文案id 存json
        id1 = results[0][0]
        if id1!=id_checked:
            self.label.setText('删除成功')
            self.con_mysql()
            # timer = threading.Timer(5, self.self.con_mysql)
            # timer.start()
            timer = threading.Timer(5, self.clear)
            timer.start()

        cursor.close()
        conn.close()
    # 删除第2个文案
    def del4(self):
        with open('D:/ids.json','r') as ids:
            ids =json.load(ids)
            id_checked=ids[1]
        conn = pymysql.connect(host='106.12.106.246', user='gong', password='GAAPAEZcj3c684aA', database='gong',
                               charset='utf8')
        cursor = conn.cursor()
        sql='DELETE FROM `wenan` WHERE id='+str(id_checked)
        cursor.execute(sql)

        sql = 'SELECT * FROM `wenan` LIMIT 3'
        cursor.execute(sql)
        results = cursor.fetchmany(3)


        id1 = results[1][0]
        if id1!=id_checked:
            self.label.setText('删除成功')
            self.con_mysql()
            # timer = threading.Timer(5, self.self.con_mysql)
            # timer.start()
            timer = threading.Timer(5, self.clear)
            timer.start()

        cursor.close()
        conn.close()
    # 删除第3个文案
    def del6(self):
        with open('D:/ids.json','r') as ids:
            ids =json.load(ids)
            id_checked=ids[2]
        conn = pymysql.connect(host='106.12.106.246', user='gong', password='GAAPAEZcj3c684aA', database='gong',
                               charset='utf8')
        cursor = conn.cursor()
        sql='DELETE FROM `wenan` WHERE id='+str(id_checked)
        cursor.execute(sql)

        sql = 'SELECT * FROM `wenan` LIMIT 3'
        cursor.execute(sql)
        results = cursor.fetchmany(3)


        id1 = results[2][0]
        if id1!=id_checked:
            self.label.setText('删除成功')
            self.con_mysql()
            # timer = threading.Timer(5, self.self.con_mysql)
            # timer.start()
            timer = threading.Timer(5, self.clear)
            timer.start()

        cursor.close()
        conn.close()
    # 恢复提示语句
    def clear(self):
        self.label.setText('欢迎你,小佩奇')

    # 读取前3条文案展示在软件界面上
    def con_mysql(self):

        # 链接mysql
        conn = pymysql.connect(host='11.11.11.11', user='xxx', password='xxxxxx', database='xxx',
                               charset='utf8')
        cursor = conn.cursor()
        sql = 'SELECT * FROM `wenan` LIMIT 3'
        cursor.execute(sql)
        results = cursor.fetchmany(3)
        # 读取前3条文案展示在软件界面上
        self.plainTextEdit.setPlainText(results[0][1])
        self.plainTextEdit_2.setPlainText(results[1][1])
        self.plainTextEdit_3.setPlainText(results[2][1])
        # 读取对应的文案id 存json
        id1 = results[0][0]
        id2 = results[1][0]
        id3 = results[2][0]
        id_list = [id1, id2, id3]
        with open('D:/ids.json','w') as ids:
            json.dump(id_list,ids)

        cursor.close()
        conn.close()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = QtWidgets.QMainWindow()
    w = Ui_MainWindow()
    w.setupUi(form)
    w.connect()

    form.show()
    sys.exit(app.exec_())

界面截图

image.png