该怎么用pyqt5来实现数据的增、删、改、查功能…

通过这个布局思路来做一个简单的后台管理系统也是OK的,大家可以参考一下啦!

【阅读全文】

该怎么用pyqt5来实现数据的增、删、改、查功能...

不多说,让我们首先整理一下我们需要的第三方模块。

[En]

Without saying much, let’s first sort out the third-party modules we need.

PyQ5 的UI界面布局部分,同样是还是使用这三个模块就够了。

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

将sys模块导入到代码块中,用于main函数里面的主体循环时使用。

import sys

add_dialog是一个自己写的添加数据的弹框。

from add_dialog import AddDialog

创建好DataManage类,主要用来实现主窗口页面的UI布局。

class DataManage(QWidget):
    def __init__(self):
        super(DataManage, self).__init__()
        self.data_list = []
        self.init_ui()

    def init_ui(self):
        '''全局设置'''

        self.setWindowIcon(QIcon('数据.ico'))
        self.setWindowTitle('数据管理器')
        self.resize(550, 400)
        grid = QGridLayout()

        '''菜单设置'''

        self.add_btn = QPushButton()
        self.add_btn.setText('添加数据')
        self.add_btn.clicked.connect(self.add_btn_click)

        self.del_btn = QPushButton()
        self.del_btn.setText('删除数据')
        self.del_btn.clicked.connect(self.del_data_row)

        self.query_btn = QPushButton()
        self.query_btn.setText('查询')
        self.query_btn.clicked.connect(self.query_data_list)

        '''数据列表设置'''

        self.data_table = QTableWidget()
        COLUMN = 5
        ROW = 0
        self.data_table.setColumnCount(COLUMN)
        self.data_table.setRowCount(ROW)
        h_table_header = ['序号', '姓名', '年龄', '班级', '表现']
        self.data_table.setHorizontalHeaderLabels(h_table_header)
        self.data_table.verticalHeader().setVisible(False)
        self.data_table.setShowGrid(True)
        self.data_table.setEditTriggers(QTableWidget.NoEditTriggers)
        self.data_table.setSelectionBehavior(QTableWidget.SelectRows)
        self.data_table.setSelectionMode(QTableWidget.SingleSelection)

        for index in range(self.data_table.columnCount()):
            headItem = self.data_table.horizontalHeaderItem(index)
            headItem.setTextAlignment(Qt.AlignVCenter)

        '''加入布局'''

        grid.addWidget(self.add_btn, 0, 0, 1, 1)
        grid.addWidget(self.del_btn, 0, 1, 1, 1)
        grid.addWidget(self.query_btn, 0, 2, 1, 1)
        grid.addWidget(self.data_table, 1, 0, 1, 3)

        self.setLayout(grid)

定义所需的槽位函数,通过不同按钮的信号绑定对应的槽位函数,实现按钮需要绑定的事件,实现业务逻辑。

[En]

Define the required slot functions, through the signals of different buttons to bind the corresponding slot functions to achieve the events that the button needs to bind to achieve business logic.

将新数据的按钮绑定到槽函数<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Bind the button for the new data to the slot function</font>*</details>
    def add_btn_click(self):
        '''
        打开新增数据的弹框模块
        :return:
        '''
        AddDialog.get_add_dialog(self)

    # 将查询数据的按钮绑定到该槽函数
    def query_data_list(self):
        '''
        查询数据、并将数据展示到主窗口的数据列表中
        :return:
        '''
        data = self.data_list
        if len(data) != 0 and len(data[0]) != 0:
            self.data_table.setRowCount(len(data))
            self.data_table.setColumnCount(len(data[0]))
            for i in range(len(data)):
                for j in range(len(data[0])):
                    self.data_table.setItem(i, j, QTableWidgetItem(str(data[i][j])))

    # 将删除数据按钮绑定到该槽函数
    def del_data_row(self):
        '''
        删除某一行的数据信息
        :return:
        '''
        row_select = self.data_table.selectedItems()
        print(row_select)
        if len(row_select) != 0:
            row = row_select[0].row()
            print(row)
            self.data_table.removeRow(row)
            del self.data_list[row]
        print(self.data_table)

通过 main() 函数来启动整个应用程序。

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = DataManage()
    main.show()
    sys.exit(app.exec_())

最后,在添加数据时共享自定义弹出模块的代码。该模块是为定制弹出框而单独编写的模块,直接在主窗口中调用该模块的弹出框函数即可实现弹出框的功能。

[En]

Finally, share the code of the custom pop-up module when you add data. This module is a module written separately to customize the bounce box, and the function of a bounce box can be realized by directly calling the pop-up box function of the module in the main window.

创建一个 add_dialog.py 的文件,将下面的代码块放到里面即可。

from PyQt5.QtWidgets import *

class AddDialog(QDialog):
    def __init__(self, parent=None):
        super(AddDialog, self).__init__(parent)
        self.init_ui(parent)

    def init_ui(self,parent):

        '''水平布局'''
        hbox = QHBoxLayout()

        self.save_btn = QPushButton()
        self.save_btn.setText('保存')
        self.save_btn.clicked.connect(lambda :self.save_btn_click(parent))

        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.cancel_btn.clicked.connect(self.cancel_btn_click)

        hbox.addWidget(self.save_btn)
        hbox.addWidget(self.cancel_btn)

        '''表单布局'''
        fbox = QFormLayout()

        self.seq_lab = QLabel()
        self.seq_lab.setText('序号:')
        self.seq_text = QLineEdit()
        self.seq_text.setPlaceholderText('请输入序号')

        self.name_lab = QLabel()
        self.name_lab.setText('姓名:')
        self.name_text = QLineEdit()
        self.name_text.setPlaceholderText('请输入姓名')

        self.age_lab = QLabel()
        self.age_lab.setText('年龄:')
        self.age_text = QLineEdit()
        self.age_text.setPlaceholderText('请输入年龄')

        self.class_lab = QLabel()
        self.class_lab.setText('班级:')
        self.class_text = QLineEdit()
        self.class_text.setPlaceholderText('请输入班级')

        self.socre_lab = QLabel()
        self.socre_lab.setText('表现:')
        self.socre_text = QLineEdit()
        self.socre_text.setPlaceholderText('请输入表现')

        fbox.addRow(self.seq_lab,self.seq_text)
        fbox.addRow(self.name_lab, self.name_text)
        fbox.addRow(self.age_lab, self.age_text)
        fbox.addRow(self.class_lab, self.class_text)
        fbox.addRow(self.socre_lab, self.socre_text)

        vbox = QVBoxLayout()
        vbox.addLayout(fbox)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

    def save_btn_click(self,parent):
        if self.seq_text.text().strip() != '' and self.name_text.text().strip() != '' \
                and self.age_text.text().strip() != ''and self.class_text.text().strip() != '' \
                and self.socre_text.text().strip() != '' :
            print(parent.data_list)
            data = [self.seq_text.text(),
                    self.name_text.text(),
                    self.age_text.text(),
                    self.class_text.text(),
                    self.socre_text.text()]
            parent.data_list.append(data)
            print(parent.data_list)
            parent.query_data_list()
            self.close()

    def cancel_btn_click(self):
        self.close()

    @staticmethod
    def get_add_dialog(parent=None):
        dialog = AddDialog(parent)
        return dialog.exec()

这些是实现数据管理器所需的主要代码块。如果有更好的方法或问题,请随时在评论区发表评论。

[En]

These are the main blocks of code needed to implement a data manager. If there is a better way or question, please feel free to leave a comment in the comments area.

该怎么用pyqt5来实现数据的增、删、改、查功能...

【往期精彩】

介绍一个文字语音神器,几行代码就可以了!

[En]

Introduce a text voice artifact, a few lines of code can be done!

python批量自动整理文件

初学者福利:分享五个免费的 Python 学习网站,抓紧收藏吧!

有趣的控制台游戏:一行代码来绘制控制台图像!

[En]

Interesting console play: one line of code to draw the console image!

数据处理小工具:Excel 批量数据文件拆分/整合器…

办公自动化:PDF文件合并器,将多个PDF文件进行合并…

GUI猜数字游戏,直接开玩…

手把手教你做一个数据图表生成器(带源代码)。

[En]

Hand in hand to teach you to make a data chart generator (with source code).

动态指针时钟:利用pyqt5制作指针钟表显示实时时间

python 日志中最亮的仔,是喜欢的花里胡哨吖…

哈萨,英雄联盟的全皮肤下载器来了。

[En]

Hassa, here comes League of Legends’s full-skin downloader.

最新发布的冬奥会吉祥物:冰墩,附源代码。

[En]

The newly released mascot of the Winter Olympic Games: ice pier, with source code.

Original: https://www.cnblogs.com/lwsbc/p/16018748.html
Author: Python集中营
Title: 该怎么用pyqt5来实现数据的增、删、改、查功能…

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509806/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球