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 查詢器內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: