我的编程空间,编程开发者的网络收藏夹
学习永远不晚

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

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

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_())

展示

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

到此这篇关于Python PyQt5实战项目之查询器的实现流程详解的文章就介绍到这了,更多相关Python PyQt5 查询器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

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

下载Word文档到电脑,方便收藏和打印~

下载Word文档

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录