亲宝软件园·资讯

展开

Python PyQt5 查询器 Python PyQt5实战项目之查询器的实现流程详解

不侠居 人气:0
想了解Python PyQt5实战项目之查询器的实现流程详解的相关内容吗,不侠居在本文为您仔细讲解Python PyQt5 查询器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python,查询器,PyQt5,查询器,下面大家一起来学习吧。

简介

这是一个操作数据库(sqlite3)的项目,用PyQt5进行界面封装。
此次项目最主要的是,主界面与子界面的交互,一个主界面与三个子界面交互
可以添加数据,修改数据,删除数据

主界面

class MainWidget(QMainWindow):
    def __init__(self):
        super(MainWidget,self).__init__()
        self.ui_init()
        self.connect_init()
        self.sql_init()

    def ui_init(self):
        self.setWindowTitle('信息查询')
        self.resize(600,700)

        self.addwidget = AddWidget()
        self.deletewidget = DeleteWidget()
        self.updatewidget = UpdateWidget()

        self.show_view = QTextBrowser()
        self.find_text = QLineEdit()
        
        self.find_btn = QPushButton()
        self.find_btn.setText('查询')
        self.update_btn = QPushButton()
        self.update_btn.setText('更改')
        self.delete_btn = QPushButton()
        self.delete_btn.setText('删除')
        self.add_btn = QPushButton()
        self.add_btn.setText('添加')

        self.status_bar = self.statusBar()
        self.widget = QWidget()

        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.find_text)
        self.h1_layout.addWidget(self.find_btn)
        self.h2_layout.addWidget(self.update_btn)
        self.h2_layout.addWidget(self.delete_btn)
        self.h2_layout.addWidget(self.add_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addWidget(self.show_view)

        self.widget.setLayout(self.v_layout)
        self.setCentralWidget(self.widget)


    def connect_init(self):
        '''
        信号与槽
        '''
        '''查询功能'''
        self.find_btn.clicked.connect(lambda:self.find_slot(self.find_text.text()))

        '''添加功能'''
        self.add_btn.clicked.connect(self.add_slot)
        self.addwidget.add_close_signal.connect(lambda:self.setEnabled(True))
        self.addwidget.add_start_signal.connect(self.add_start_slot)
        
        '''删除功能'''
        self.delete_btn.clicked.connect(self.delete_slot)
        self.deletewidget.delete_close_signal.connect(lambda:self.setEnabled(True))
        self.deletewidget.delete_start_signal.connect(self.delete_start_slot)

        '''更新功能'''
        self.update_btn.clicked.connect(self.update_slot)
        self.updatewidget.update_close_signal.connect(lambda:self.setEnabled(True))
        self.updatewidget.update_name_signal.connect(self.update_name_slot)
        self.updatewidget.update_show_signal.connect(self.update_show_slot)

    def sql_init(self):
        '''
        sqllite3初始化
        '''
        try:
            conn = sqlite3.connect('find.db')
            cursor = conn.cursor()
            cursor.execute('select * from design')
            cursor.close()
            conn.close()
        except:
            conn = sqlite3.connect('find.db')
            cursor = conn.cursor()
            cursor.execute('create table design (id  integer PRIMARY KEY AUTOINCREMENT,name varchar(20),show varchar(100))')
            cursor.close()
            conn.close()


        '''查询槽'''
    def find_slot(self,content):
        try:
            result = self.find_func(content)
            self.status_bar.showMessage('查询成功')
            if result:
                self.show_view.append('<font size="5" color="red">共查询 {} 条数据</font>'.format(len(result)))
                for i in range(len(result)):
                    self.show_view.append('id:' + str(result[i][0]))
                    self.show_view.append('名称:' + result[i][1])
                    self.show_view.append('说明:' + result[i][2])
            else:
                self.show_view.append('<font size="5" color="red">共查询 {} 条数据</font>'.format(len(result)))
        except Exception as e:
            self.status_bar.showMessage('查询失败')
            print(e)


        '''添加槽'''
    def add_slot(self):
        self.addwidget.show()
        self.setEnabled(False)

    def add_start_slot(self,name,show):
        try:
            self.insert_func(name,show)
            self.status_bar.showMessage('添加成功')
        except Exception as e:
            self.status_bar.showMessage('添加失败')
        self.setEnabled(True)
        self.addwidget.name_text.clear()
        self.addwidget.show_text.clear()
        self.addwidget.close()


        '''删除槽'''
    def delete_slot(self):
        self.deletewidget.show()
        self.setEnabled(False)

    def delete_start_slot(self,id):
        try:
            self.delete_func(id)
            self.reset_func()
            self.status_bar.showMessage('删除成功')
        except  Exception as e:
            self.status_bar.showMessage('删除失败')
        self.setEnabled(True)
        self.deletewidget.close()


        '''更新槽'''
    def update_slot(self):
        self.updatewidget.show()
        self.setEnabled(False)

    def update_name_slot(self,id,name):
        try:
            self.update_name_func(id,name)
            self.status_bar.showMessage('更新名称成功')
        except Exception as e:
            self.status_bar.showMessage('更新名称失败')
        self.setEnabled(True)
        self.updatewidget.close()

    def update_show_slot(self,id,name):
        try:
            self.update_show_func(id,name)
            self.status_bar.showMessage('更新名称成功')
        except Exception as e:
            self.status_bar.showMessage('更新名称失败')
        self.setEnabled(True)
        self.updatewidget.close()


        '''数据库函数'''
    def insert_func(self,name,show):
        '''
        插入信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('insert into design (id,name,show) values (null,"{}","{}")'.format(name,show))
        cursor.close()
        conn.commit()
        conn.close()

    def find_func(self,content):
        '''
        查询信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM design WHERE name LIKE "%{}%"'.format(content))
        result1 = cursor.fetchall()
        cursor.close()
        conn.close()
        return result1

    def count_func(self):
        '''
        信息数目
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('select * from design')
        result = cursor.fetall()
        count = len(result)
        cursor.close()
        conn.close()
        return count

    def update_name_func(self,id,name):
        '''
        更新名称信息
        '''
        print(id,name)
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('update design set name = ? where id = ?',(name,id))
        cursor.close()
        conn.commit()
        conn.close()

    def update_show_func(self,id,name):
        '''
        更新说明信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('update design set show = ? where id = ?',(name,id))
        cursor.close()
        conn.commit()
        conn.close()

    def delete_func(self,id):
        '''
        删除信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('delete from design where id = ?',(id,))
        cursor.close()
        conn.commit()
        conn.close()

    def reset_func(self):
    	'''
		自增列归零
		'''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute("DELETE FROM sqlite_sequence WHERE name = 'design'")
        cursor.close()
        conn.commit()
        conn.close()

这个类包括主界面,数据库操作的函数,与其他界面操作的函数
当子界面打开时,主界面不可使用
子界面向主界面发送信号,功能实现代码都写在主界面类中
子界面需要在主界面声明,例如:self.addwidget = AddWidget()

添加功能槽

        '''添加槽'''
    def add_slot(self):
        self.addwidget.show()
        self.setEnabled(False)

    def add_start_slot(self,name,show):
        try:
            self.insert_func(name,show)
            self.status_bar.showMessage('添加成功')
        except Exception as e:
            self.status_bar.showMessage('添加失败')
        self.setEnabled(True)
        self.addwidget.name_text.clear()
        self.addwidget.show_text.clear()
        self.addwidget.close()
def add_slot(self):
        self.addwidget.show()
        self.setEnabled(False)

这个函数是用来启动添加界面和将主界面设置为不可用状态

    def add_start_slot(self,name,show):
        try:
            self.insert_func(name,show)
            self.status_bar.showMessage('添加成功')
        except Exception as e:
            self.status_bar.showMessage('添加失败')
        self.setEnabled(True)
        self.addwidget.name_text.clear()
        self.addwidget.show_text.clear()
        self.addwidget.close()

这个函数是添加功能的主要实现,调用数据库插入函数,数据插入完毕后,清除添加子界面的文本,最后关闭添加子界面。其他功能槽大同小异

def reset_func()

def reset_func(self):
    	'''
		自增列归零
		'''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute("DELETE FROM sqlite_sequence WHERE name = 'design'")
        cursor.close()
        conn.commit()
        conn.close()

这个函数作用不是很大
只是将sqlite3数据库自带的系统表,数据库被创建时,sqlite_sequence表会被自动创建,该表包括两列。第一列为name,用来存储表的名称。第二列为seq,用来保存表对应的RowID的最大值,当对应的表增加记录,该表会自动更新。当表删除,该表对应的记录也会自动删除。
本来我是想当有数据删除时,ID值能重新更新,可是这个方法,只有当表内数据全部删除时,ID才会重新开始

添加数据的界面

class AddWidget(QWidget):
    '''
    添加功能界面
    '''
    add_start_signal = pyqtSignal(str,str)
    add_close_signal = pyqtSignal()

    def __init__(self):
        super(AddWidget,self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('添加信息')
        self.setFixedSize(300,200)

        self.name_text = QLineEdit()
        self.show_text = QTextEdit()
        self.start_btn = QPushButton()
        self.start_btn.setText('添加')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.name_label = QLabel()
        self.name_label.setText('名称:')
        self.show_label = QLabel()
        self.show_label.setText('说明:')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.h3_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.name_label)
        self.h1_layout.addWidget(self.name_text)
        self.h2_layout.addWidget(self.show_label)
        self.h2_layout.addWidget(self.show_text)
        self.h3_layout.addWidget(self.start_btn)
        self.h3_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addLayout(self.h3_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.start_btn.clicked.connect(self.start_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def start_slot(self):
        name = self.name_text.text()
        show = self.show_text.toPlainText()
        self.add_start_signal.emit(name,show)


    def close_slot(self):
        self.close()
        self.add_close_signal.emit()

    def closeEvent(self, event):
    	'''重写关闭事件'''
        self.add_close_signal.emit()

这是添加数据的界面
自定义了两个信号
1.添加数据的信号,当按下添加按钮,这个界面向主界面传输两个字符串
2.结束这个界面的信号,不管是×了这个界面(重写了关闭事件)还是按下取消按钮,都向主界面发送信号,用来使主界面可以使用

删除数据的界面

class DeleteWidget(QWidget):
    '''
    删除功能界面
    '''
    delete_cancel_signal = pyqtSignal()
    delete_start_signal = pyqtSignal(str)
    delete_close_signal = pyqtSignal()

    def __init__(self):
        super(DeleteWidget,self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('删除信息')
        self.setFixedSize(300,100)

        self.id_text = QLineEdit()
        self.start_btn = QPushButton()
        self.start_btn.setText('删除')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.id_label = QLabel()
        self.id_label.setText('要删除的id:')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.id_label)
        self.h1_layout.addWidget(self.id_text)
        self.h2_layout.addWidget(self.start_btn)
        self.h2_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.start_btn.clicked.connect(self.start_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def start_slot(self):
        id = self.id_text.text()
        self.delete_start_signal.emit(id)


    def close_slot(self):
        self.close()
        self.delete_close_signal.emit()

    def closeEvent(self, event):
        self.delete_close_signal.emit()

这是删除数据的界面,跟之前界面布局基本一样

修改数据的界面

class UpdateWidget(QWidget):
    '''
    更新功能界面
    '''
    update_name_signal = pyqtSignal(int,str)
    update_show_signal = pyqtSignal(int,str)
    update_close_signal = pyqtSignal()
    

    def __init__(self):
        super(UpdateWidget,self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('修改信息')
        self.setFixedSize(300,150)

        self.content_text = QLineEdit()
        self.id_text = QLineEdit()
        self.name_btn = QPushButton()
        self.name_btn.setText('修改名称')
        self.show_btn = QPushButton()
        self.show_btn.setText('修改说明')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.content_label = QLabel()
        self.content_label.setText('修改内容:')
        self.id_label = QLabel()
        self.id_label.setText('选择id为:')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.h3_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.id_label)
        self.h1_layout.addWidget(self.id_text)
        self.h2_layout.addWidget(self.content_label)
        self.h2_layout.addWidget(self.content_text)
        self.h3_layout.addWidget(self.name_btn)
        self.h3_layout.addWidget(self.show_btn)
        self.h3_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addLayout(self.h3_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.name_btn.clicked.connect(self.update_name_slot)
        self.show_btn.clicked.connect(self.update_show_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def update_name_slot(self):
        id = self.id_text.text()
        id = int(id)
        content = self.content_text.text()
        self.update_name_signal.emit(id,content)


    def update_show_slot(self):
        id = self.id_text.text()
        id = int(id)
        content = self.content_text.text()
        self.update_show_signal.emit(id,content)  

    def close_slot(self):
        self.close()
        self.update_close_signal.emit()

    def closeEvent(self, event):
        self.update_close_signal.emit()

这个更新数据的界面,跟之前的界面的布局基本一样

全部代码

import sys
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QTextEdit, QHBoxLayout, QVBoxLayout, QLineEdit, QLabel,QTextBrowser 
import sqlite3



class MainWidget(QMainWindow):
    def __init__(self):
        super(MainWidget,self).__init__()
        self.ui_init()
        self.connect_init()
        self.sql_init()

    def ui_init(self):
        self.setWindowTitle('信息查询')
        self.resize(600,700)

        self.addwidget = AddWidget()
        self.deletewidget = DeleteWidget()
        self.updatewidget = UpdateWidget()

        self.show_view = QTextBrowser()
        self.find_text = QLineEdit()
        
        self.find_btn = QPushButton()
        self.find_btn.setText('查询')
        self.update_btn = QPushButton()
        self.update_btn.setText('更改')
        self.delete_btn = QPushButton()
        self.delete_btn.setText('删除')
        self.add_btn = QPushButton()
        self.add_btn.setText('添加')

        self.status_bar = self.statusBar()
        self.widget = QWidget()

        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.find_text)
        self.h1_layout.addWidget(self.find_btn)
        self.h2_layout.addWidget(self.update_btn)
        self.h2_layout.addWidget(self.delete_btn)
        self.h2_layout.addWidget(self.add_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addWidget(self.show_view)

        self.widget.setLayout(self.v_layout)
        self.setCentralWidget(self.widget)


    def connect_init(self):
        '''
        信号与槽
        '''
        '''查询功能'''
        self.find_btn.clicked.connect(lambda:self.find_slot(self.find_text.text()))

        '''添加功能'''
        self.add_btn.clicked.connect(self.add_slot)
        self.addwidget.add_close_signal.connect(lambda:self.setEnabled(True))
        self.addwidget.add_start_signal.connect(self.add_start_slot)
        
        '''删除功能'''
        self.delete_btn.clicked.connect(self.delete_slot)
        self.deletewidget.delete_close_signal.connect(lambda:self.setEnabled(True))
        self.deletewidget.delete_start_signal.connect(self.delete_start_slot)

        '''更新功能'''
        self.update_btn.clicked.connect(self.update_slot)
        self.updatewidget.update_close_signal.connect(lambda:self.setEnabled(True))
        self.updatewidget.update_name_signal.connect(self.update_name_slot)
        self.updatewidget.update_show_signal.connect(self.update_show_slot)

    def sql_init(self):
        '''
        sqllite3初始化
        '''
        try:
            conn = sqlite3.connect('find.db')
            cursor = conn.cursor()
            cursor.execute('select * from design')
            cursor.close()
            conn.close()
        except:
            conn = sqlite3.connect('find.db')
            cursor = conn.cursor()
            cursor.execute('create table design (id  integer PRIMARY KEY AUTOINCREMENT,name varchar(20),show varchar(100))')
            cursor.close()
            conn.close()


        '''查询槽'''
    def find_slot(self,content):
        try:
            result = self.find_func(content)
            self.status_bar.showMessage('查询成功')
            if result:
                self.show_view.append('<font size="5" color="red">共查询 {} 条数据</font>'.format(len(result)))
                for i in range(len(result)):
                    self.show_view.append('id:' + str(result[i][0]))
                    self.show_view.append('名称:' + result[i][1])
                    self.show_view.append('说明:' + result[i][2])
            else:
                self.show_view.append('<font size="5" color="red">共查询 {} 条数据</font>'.format(len(result)))
        except Exception as e:
            self.status_bar.showMessage('查询失败')
            print(e)


        '''添加槽'''
    def add_slot(self):
        self.addwidget.show()
        self.setEnabled(False)

    def add_start_slot(self,name,show):
        try:
            self.insert_func(name,show)
            self.status_bar.showMessage('添加成功')
        except Exception as e:
            self.status_bar.showMessage('添加失败')
        self.setEnabled(True)
        self.addwidget.name_text.clear()
        self.addwidget.show_text.clear()
        self.addwidget.close()


        '''删除槽'''
    def delete_slot(self):
        self.deletewidget.show()
        self.setEnabled(False)

    def delete_start_slot(self,id):
        try:
            self.delete_func(id)
            self.reset_func()
            self.status_bar.showMessage('删除成功')
        except  Exception as e:
            self.status_bar.showMessage('删除失败')
        self.setEnabled(True)
        self.deletewidget.close()


        '''更新槽'''
    def update_slot(self):
        self.updatewidget.show()
        self.setEnabled(False)

    def update_name_slot(self,id,name):
        try:
            self.update_name_func(id,name)
            self.status_bar.showMessage('更新名称成功')
        except Exception as e:
            self.status_bar.showMessage('更新名称失败')
        self.setEnabled(True)
        self.updatewidget.close()

    def update_show_slot(self,id,name):
        try:
            self.update_show_func(id,name)
            self.status_bar.showMessage('更新名称成功')
        except Exception as e:
            self.status_bar.showMessage('更新名称失败')
        self.setEnabled(True)
        self.updatewidget.close()


        '''数据库函数'''
    def insert_func(self,name,show):
        '''
        插入信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('insert into design (id,name,show) values (null,"{}","{}")'.format(name,show))
        cursor.close()
        conn.commit()
        conn.close()

    def find_func(self,content):
        '''
        查询信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM design WHERE name LIKE "%{}%"'.format(content))
        result1 = cursor.fetchall()
        cursor.close()
        conn.close()
        return result1

    def count_func(self):
        '''
        信息数目
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('select * from design')
        result = cursor.fetchall()
        count = len(result)
        cursor.close()
        conn.close()
        return count

    def update_name_func(self,id,name):
        '''
        更新名称信息
        '''
        print(id,name)
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('update design set name = ? where id = ?',(name,id))
        cursor.close()
        conn.commit()
        conn.close()

    def update_show_func(self,id,name):
        '''
        更新说明信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('update design set show = ? where id = ?',(name,id))
        cursor.close()
        conn.commit()
        conn.close()

    def delete_func(self,id):
        '''
        删除信息
        '''
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute('delete from design where id = ?',(id,))
        cursor.close()
        conn.commit()
        conn.close()

    def reset_func(self):
        conn = sqlite3.connect('find.db')
        cursor = conn.cursor()
        cursor.execute("DELETE FROM sqlite_sequence WHERE name = 'design'")
        cursor.close()
        conn.commit()
        conn.close()



class AddWidget(QWidget):
    '''
    添加功能界面
    '''
    add_start_signal = pyqtSignal(str,str)
    add_close_signal = pyqtSignal()

    def __init__(self):
        super(AddWidget,self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('添加信息')
        self.setFixedSize(300,200)

        self.name_text = QLineEdit()
        self.show_text = QTextEdit()
        self.start_btn = QPushButton()
        self.start_btn.setText('添加')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.name_label = QLabel()
        self.name_label.setText('名称:')
        self.show_label = QLabel()
        self.show_label.setText('说明:')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.h3_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.name_label)
        self.h1_layout.addWidget(self.name_text)
        self.h2_layout.addWidget(self.show_label)
        self.h2_layout.addWidget(self.show_text)
        self.h3_layout.addWidget(self.start_btn)
        self.h3_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addLayout(self.h3_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.start_btn.clicked.connect(self.start_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def start_slot(self):
        name = self.name_text.text()
        show = self.show_text.toPlainText()
        self.add_start_signal.emit(name,show)


    def close_slot(self):
        self.close()
        self.add_close_signal.emit()

    def closeEvent(self, event):
        self.add_close_signal.emit()

        
        
class DeleteWidget(QWidget):
    '''
    删除功能界面
    '''
    delete_cancel_signal = pyqtSignal()
    delete_start_signal = pyqtSignal(str)
    delete_close_signal = pyqtSignal()

    def __init__(self):
        super(DeleteWidget,self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('删除信息')
        self.setFixedSize(300,100)

        self.id_text = QLineEdit()
        self.start_btn = QPushButton()
        self.start_btn.setText('删除')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.id_label = QLabel()
        self.id_label.setText('要删除的id:')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.id_label)
        self.h1_layout.addWidget(self.id_text)
        self.h2_layout.addWidget(self.start_btn)
        self.h2_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.start_btn.clicked.connect(self.start_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def start_slot(self):
        id = self.id_text.text()
        self.delete_start_signal.emit(id)


    def close_slot(self):
        self.close()
        self.delete_close_signal.emit()

    def closeEvent(self, event):
        self.delete_close_signal.emit()


class UpdateWidget(QWidget):
    '''
    更新功能界面
    '''
    update_name_signal = pyqtSignal(int,str)
    update_show_signal = pyqtSignal(int,str)
    update_close_signal = pyqtSignal()
    

    def __init__(self):
        super(UpdateWidget,self).__init__()
        self.ui_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('修改信息')
        self.setFixedSize(300,150)

        self.content_text = QLineEdit()
        self.id_text = QLineEdit()
        self.name_btn = QPushButton()
        self.name_btn.setText('修改名称')
        self.show_btn = QPushButton()
        self.show_btn.setText('修改说明')
        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.content_label = QLabel()
        self.content_label.setText('修改内容:')
        self.id_label = QLabel()
        self.id_label.setText('选择id为:')
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.h3_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h1_layout.addWidget(self.id_label)
        self.h1_layout.addWidget(self.id_text)
        self.h2_layout.addWidget(self.content_label)
        self.h2_layout.addWidget(self.content_text)
        self.h3_layout.addWidget(self.name_btn)
        self.h3_layout.addWidget(self.show_btn)
        self.h3_layout.addWidget(self.cancel_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addLayout(self.h3_layout)
        self.setLayout(self.v_layout)

    def connect_init(self):
        self.name_btn.clicked.connect(self.update_name_slot)
        self.show_btn.clicked.connect(self.update_show_slot)
        self.cancel_btn.clicked.connect(self.close_slot)

    def update_name_slot(self):
        id = self.id_text.text()
        id = int(id)
        content = self.content_text.text()
        self.update_name_signal.emit(id,content)


    def update_show_slot(self):
        id = self.id_text.text()
        id = int(id)
        content = self.content_text.text()
        self.update_show_signal.emit(id,content)  

    def close_slot(self):
        self.close()
        self.update_close_signal.emit()

    def closeEvent(self, event):
        self.update_close_signal.emit()       

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

展示

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

加载全部内容

相关教程
猜你喜欢
用户评论