python3+PyQt5 數(shù)據(jù)庫編程--增刪改實(shí)例
本文通過python3+pyqt5改寫實(shí)現(xiàn)了python Qt gui 編程變成15章的excise例子。
#!/usr/bin/env python3 import os import sys from PyQt5.QtCore import (QFile, QVariant, Qt) from PyQt5.QtWidgets import (QApplication, QDialog, QDialogButtonBox, QMenu, QMessageBox, QTableView, QVBoxLayout) from PyQt5.QtSql import (QSqlDatabase, QSqlQuery, QSqlTableModel) MAC = True try: from PyQt5.QtGui import qt_mac_set_native_menubar except ImportError: MAC = False ID, CATEGORY, SHORTDESC, LONGDESC = range(4) class ReferenceDataDlg(QDialog): def __init__(self, parent=None): super(ReferenceDataDlg, self).__init__(parent) self.model = QSqlTableModel(self) self.model.setTable("reference") self.model.setSort(ID, Qt.AscendingOrder) self.model.setHeaderData(ID, Qt.Horizontal, "ID") self.model.setHeaderData(CATEGORY, Qt.Horizontal,"Category") self.model.setHeaderData(SHORTDESC, Qt.Horizontal,"Short Desc.") self.model.setHeaderData(LONGDESC, Qt.Horizontal,"Long Desc.") self.model.select() self.view = QTableView() self.view.setModel(self.model) self.view.setSelectionMode(QTableView.SingleSelection) self.view.setSelectionBehavior(QTableView.SelectRows) self.view.setColumnHidden(ID, True) self.view.resizeColumnsToContents() buttonBox = QDialogButtonBox() addButton = buttonBox.addButton("&Add", QDialogButtonBox.ActionRole) deleteButton = buttonBox.addButton("&Delete", QDialogButtonBox.ActionRole) sortButton = buttonBox.addButton("&Sort", QDialogButtonBox.ActionRole) if not MAC: addButton.setFocusPolicy(Qt.NoFocus) deleteButton.setFocusPolicy(Qt.NoFocus) sortButton.setFocusPolicy(Qt.NoFocus) menu = QMenu(self) sortByCategoryAction = menu.addAction("Sort by &Category") sortByDescriptionAction = menu.addAction("Sort by &Description") sortByIDAction = menu.addAction("Sort by &ID") sortButton.setMenu(menu) closeButton = buttonBox.addButton(QDialogButtonBox.Close) layout = QVBoxLayout() layout.addWidget(self.view) layout.addWidget(buttonBox) self.setLayout(layout) addButton.clicked.connect(self.addRecord) deleteButton.clicked.connect(self.deleteRecord) sortByCategoryAction.triggered.connect(lambda:self.sort(CATEGORY)) sortByDescriptionAction.triggered.connect(lambda:self.sort(SHORTDESC)) sortByIDAction.triggered.connect(lambda:self.sort(ID)) closeButton.clicked.connect(self.accept) self.setWindowTitle("Reference Data") def addRecord(self): row = self.model.rowCount() self.model.insertRow(row) index = self.model.index(row, CATEGORY) self.view.setCurrentIndex(index) self.view.edit(index) def deleteRecord(self): index = self.view.currentIndex() if not index.isValid(): return record = self.model.record(index.row()) category = record.value(CATEGORY) desc = record.value(SHORTDESC) if (QMessageBox.question(self, "Reference Data", ("Delete {0} from category {1}?" .format(desc,category)), QMessageBox.Yes|QMessageBox.No) == QMessageBox.No): return self.model.removeRow(index.row()) self.model.submitAll() self.model.select() def sort(self, column): self.model.setSort(column, Qt.AscendingOrder) self.model.select() def main(): app = QApplication(sys.argv) filename = os.path.join(os.path.dirname(__file__), "reference.db") create = not QFile.exists(filename) db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(filename) if not db.open(): QMessageBox.warning(None, "Reference Data", "Database Error: {0}".format(db.lastError().text())) sys.exit(1) if create: query = QSqlQuery() query.exec_("""CREATE TABLE reference ( id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, category VARCHAR(30) NOT NULL, shortdesc VARCHAR(20) NOT NULL, longdesc VARCHAR(80))""") form = ReferenceDataDlg() form.show() sys.exit(app.exec_()) main()
運(yùn)行結(jié)果:
以上這篇python3+PyQt5 數(shù)據(jù)庫編程--增刪改實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用django-suit為django 1.7 admin后臺(tái)添加模板
前面我們介紹了Django-grappelli給admin添加模板,可是使用中發(fā)現(xiàn)inline有點(diǎn)問題,所以就換了今天我們要談的Django-suit,貌似要稍微好一些2014-11-11淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點(diǎn)
這篇文章主要介紹了淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Python實(shí)現(xiàn)針對(duì)含中文字符串的截取功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)針對(duì)含中文字符串的截取功能,結(jié)合具體實(shí)例形式分析了Python針對(duì)utf-8及gb18030編碼的中文字符串截取操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL的方法
在日常工作中,我們經(jīng)常會(huì)遇到需要處理大量文件并將數(shù)據(jù)存儲(chǔ)至數(shù)據(jù)庫或整合到一個(gè)文件的需求,本文將向大家展示如何使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL數(shù)據(jù)庫中,需要的朋友可以參考下2024-01-01python使用beautifulsoup4爬取酷狗音樂代碼實(shí)例
這篇文章主要介紹了python使用beautifulsoup4爬取酷狗音樂代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12python?Pydub簡(jiǎn)單易用的音頻處理庫使用實(shí)例探索
Pydub是一個(gè)簡(jiǎn)單易用的Python庫,它讓音頻處理變得像處理列表或字符串一樣簡(jiǎn)單,你可以用Pydub來剪輯、合并、調(diào)整音頻文件,以及執(zhí)行許多其他的音頻處理任務(wù),它支持多種音頻格式,包括常見的MP3、WAV和AAC2024-01-01