Ugrás a fő tartalomra

Python SqLite Pyqt5


 
Alkalmazás GUI (grafikus felhasználói interfész) generáló modul fejlett alkalmazás létrehozási funkciókkal.


Installálása:
    pip install PyQt5


Alábbi komponensei vannak:


QtCore − Core non-GUI classes used by other modules

QtGui − Graphical user interface components

QtMultimedia − Classes for low-level multimedia programming

QtNetwork − Classes for network programming

QtOpenGL − OpenGL support classes

QtScript − Classes for evaluating Qt Scripts

QtSql − Classes for database integration using SQL

QtSvg − Classes for displaying the contents of SVG files

QtWebKit − Classes for rendering and editing HTML

QtXml − Classes for handling XML

QtWidgets − Classes for creating classic desktop-style UIs

QtDesigner − Classes for extending Qt Designer




Egy minimalista képernyő létrehozása

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
'''
PyQt5 minta    [Klajosw]
két gombos kezdő képernyő
'''
app = QApplication([])
window = QWidget()
window.setWindowTitle('KL GUI')
window.setGeometry(500, 500, 300, 100) 
##----- rétegen a megjelenítendő gombok létrehozása
layout = QVBoxLayout()
layout.addWidget(QPushButton('Felső Gomb'))
layout.addWidget(QPushButton('Alsó gomb'))
window.setLayout(layout)
##------ alkalmazás megjelenitése
window.show()
app.exec()





Használata SqLite adatbázis elérésnél:


import sys
from PyQt5.QtCore import Qt
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QMessageBox,
    QTableView,
)
class Contacts(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("QTableView Minta")
        self.resize(420, 420)
        
        # Set up the model
        self.model = QSqlTableModel(self)
        self.model.setTable("contacts")
        self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
        self.model.setHeaderData(0, Qt.Horizontal, "ID")
        self.model.setHeaderData(1, Qt.Horizontal, "Name")
        self.model.setHeaderData(2, Qt.Horizontal, "Job")
        self.model.setHeaderData(3, Qt.Horizontal, "Email")
        self.model.select()
        
        # Set up the view
        self.view = QTableView()
        self.view.setModel(self.model)
        self.view.resizeColumnsToContents()
        self.setCentralWidget(self.view)
def createConnection():
    con = QSqlDatabase.addDatabase("QSQLITE")
    con.setDatabaseName(r"c:\Users\User\Documents\data\sqlite\contacts.db")
    if not con.open():
        QMessageBox.critical(
            None,
            "QTableView Minta - HIBA!",
            "Adatbázis hiba üzenet: %s" % con.lastError().databaseText(),
        )
        return False
    return True
def addrow():
    print (model.rowCount())
    ret = model.insertRows(model.rowCount(), 1)
    print (ret)
def findrow(i):
    delrow = i.row()
    
delrow = -1
app = QApplication(sys.argv)
if not createConnection():
    sys.exit(1)
    
win = Contacts()
win.show()
sys.exit(app.exec_())







import sys
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QMessageBox,
    QTableWidget,
    QTableWidgetItem,
    QWidget, QToolTip, QPushButton, QMessageBox
)
from PyQt5.QtCore import QCoreApplication, Qt

class Contacts(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        
        self.setWindowTitle("QTableView Minta2")
        self.resize(450, 300)
        
        self.view = QTableWidget()
        self.view.setColumnCount(4)
        self.view.setHorizontalHeaderLabels(["ID", "Name", "Job", "Email"])
        query = QSqlQuery("SELECT id, name, job, email FROM contacts")
        while query.next():
            rows = self.view.rowCount()
            self.view.setRowCount(rows + 1)
            self.view.setItem(rows, 0, QTableWidgetItem(str(query.value(0))))
            self.view.setItem(rows, 1, QTableWidgetItem(query.value(1)))
            self.view.setItem(rows, 2, QTableWidgetItem(query.value(2)))
            self.view.setItem(rows, 3, QTableWidgetItem(query.value(3)))
        self.view.resizeColumnsToContents()
        self.setCentralWidget(self.view)
        
        btn = QPushButton("Bezár", self)
        btn.setToolTip("Ablak bezárása")
        btn.clicked.connect(self.close)  ## kilépés
        btn.resize(btn.sizeHint())
        btn.move(350, 20)
        
        
def createConnection():
    con = QSqlDatabase.addDatabase("QSQLITE")
    con.setDatabaseName(r"c:\Users\User\Documents\data\sqlite\contacts.db")
    if not con.open():
        QMessageBox.critical(
            None,
            "QTableView Minta - HIBA!",
            "Adatbázis hiba üzenet: %s" % con.lastError().databaseText(),
        )
        return False
    return True
delrow = -1
app = QApplication(sys.argv)
if not createConnection():
    sys.exit(1)
    
win = Contacts()
win.show()
sys.exit(app.exec_())







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

def initializeModel(model):
    model.setTable('contacts')
    model.setEditStrategy(QSqlTableModel.OnFieldChange)
    model.select()
    model.setHeaderData(0, Qt.Horizontal, "ID")
    model.setHeaderData(1, Qt.Horizontal, "Name")
    model.setHeaderData(2, Qt.Horizontal, "Email")

def createView(title, model):
    view = QTableView()
    view.setModel(model)
    view.setWindowTitle(title)
    view.resize(450, 300)
    return view

def addrow():
    print (model.rowCount())
    ret = model.insertRows(model.rowCount(), 1)
    print (ret)

def findrow(i):
    delrow = i.row()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    db = QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName(r"c:\Users\User\Documents\data\sqlite\contacts.db")
    model = QSqlTableModel()
    delrow = -1
    initializeModel(model)

    view1 = createView("KL Table Model", model) 
    view1.clicked.connect(findrow)

    dlg = QDialog()
    layout = QVBoxLayout()
    layout.addWidget(view1)

    btn_ad = QPushButton("Add a row")
    btn_ad.clicked.connect(addrow)
    layout.addWidget(btn_ad)

    btn_del = QPushButton("Del row")
    btn_del.clicked.connect(lambda: model.removeRow(view1.currentIndex().row()))
    layout.addWidget(btn_del)

    btn_ex =QPushButton("Exit")
    btn_ex.clicked.connect(dlg.close)                ## kilépés
    layout.addWidget(btn_ex)

    dlg.setLayout(layout)
    dlg.setWindowTitle("KL Database Example")
    dlg.resize(650, 300)
    dlg.show()
    sys.exit(app.exec_())  ## vége






Link:

https://www.tutorialspoint.com/pyqt5/index.htm

https://build-system.fman.io/pyqt5-tutorial

https://zetcode.com/gui/pyqt5/

https://github.com/klajosw/python/blob/master/kl_py_pyqt5_01.ipynb

Megjegyzések