ich hatte mal wieder ne Idee, ein Programm mit Datenbank in dem ich meine Kinobesuche erfassen kann.
Die SQLite Datenbank war ja schnell erstellt mithilfe des SQLite Browser Programms.
Die DB enthält die Tabellen Filme, Kinos, Säle, Kategorien, Freunde und Besuche.
Angefangen hab ich mal mit der Anzeige der Filme-Einträge, die enthält den Titel, Kinostart in DE, Beschreibung und ein Bild.
Das laden und anzeigen des ersten Eintrages funktioniert auch schon mal, nur mit dem durchblättern komm ich jetzt nicht mehr weiter, mir scheint der generierte py-Code aus der ui-Datei ist nicht so optimal oder ich hab an den falschen Stellen den Code erweitert...
Wäre sehr nett wenn hier der ein oder andere nicht nur Tipps geben könnte sondern vielleicht auch einfach den Code mal in groben Zügen ändert.
Leider lässt sich hier ja keine Datei anhängen, daher hier der Aufbau der Tabelle Filme in der kino.db:
Code: Alles auswählen
CREATE TABLE "Filme" (
"Film_ID" INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
"Titel" TEXT,
"Beschreibung" TEXT,
"Kinostart DE" TEXT,
"Plakat" BLOB
);
Code: Alles auswählen
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.12
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
import sqlite3
class Ui_Form(object):
def open(self): # OPEN DATABASE
try:
conn = sqlite3.connect('kino.db')
#cursor = conn.cursor()
print(sqlite3.version)
except sqlite3.Error as e:
print("Failed connecting to database...")
# Get last Row
query = "SELECT * FROM Filme"
self.cur = conn.cursor()
self.cur.execute(query)
last_row = self.cur.fetchall()
print(len(last_row))
# Start with first entry
filmid = 0
query = "SELECT * FROM Filme"
c = conn.execute(query)
result = c.fetchall()
# 0 = erste ID [ID][Titel][Beschreibung][Kinostart][Poster]
# print("Titel :", result[filmid][1])
# print("Datum :", result[filmid][3])
# # Date format
# # now = QDateTime.currentDateTimeUtc()
# now = QDateTime(2019, 2, 21, 20, 31, 16, 992)
# unix_time = now.toSecsSinceEpoch()+3600 # +3600 = +1h
# d = QDateTime.fromSecsSinceEpoch(unix_time)
# print("Date Time: " ,d.toString("dd.MM.yyyy hh:mm:ss t"))
# print("UnixTime: " ,unix_time)
# print("now: " ,now)
# Get column data
filmtitel = result[filmid][1]
filmbeschreibung = result[filmid][2]
filmstart = result[filmid][3]
poster_data = result[filmid][4]
qimg = QtGui.QImage.fromData(poster_data)
poster = QtGui.QPixmap.fromImage(qimg)
eintrag = result[filmid][0]
eintraege = len(last_row)
# View column data
ui.txt_title.setText(filmtitel)
ui.txt_description.setText(filmbeschreibung)
ui.txt_cinedate.setText(QDate.fromString(filmstart, "yyyy-MM-dd").toString("dd.MM.yyyy"))
ui.view_poster.setPixmap(poster)
ui.txt_entry.setText(str(eintrag))
ui.txt_entrys.setText(str(eintraege))
if str(eintrag) == '1':
ui.btn_first.setEnabled(False)
ui.btn_prev.setEnabled(False)
else:
ui.btn_first.setEnabled(True)
ui.btn_prev.setEnabled(True)
if str(eintrag) == str(eintraege):
ui.btn_next.setEnabled(False)
ui.btn_last.setEnabled(False)
else:
ui.btn_next.setEnabled(True)
ui.btn_last.setEnabled(True)
# def first(self):
# filmid = 0
# def prev(self):
# filmid-1
def next(self):
# filmid + 1
print("btn_next.clicked")
# def last(self):
# filmid = str(eintraege)
# self.conn.close()
def setupUi(self, Form):
# FORM
Form.setObjectName("Form")
Form.setWindowTitle("Kinofilme")
Form.resize(640, 480)
# LABELS
self.lbl_title = QtWidgets.QLabel(Form)
self.lbl_title.setGeometry(QtCore.QRect(10, 10, 71, 16))
self.lbl_title.setObjectName("lbl_title")
self.lbl_title.setText("Titel")
self.lbl_cinedate = QtWidgets.QLabel(Form)
self.lbl_cinedate.setGeometry(QtCore.QRect(10, 40, 71, 16))
self.lbl_cinedate.setObjectName("lbl_cinedate")
self.lbl_cinedate.setText("Kinostart")
self.lbl_description = QtWidgets.QLabel(Form)
self.lbl_description.setGeometry(QtCore.QRect(10, 70, 71, 16))
self.lbl_description.setObjectName("lbl_description")
self.lbl_description.setText("Beschreibung")
self.lbl_slash = QtWidgets.QLabel(Form)
self.lbl_slash.setGeometry(QtCore.QRect(300, 440, 31, 20))
self.lbl_slash.setAlignment(QtCore.Qt.AlignCenter)
self.lbl_slash.setObjectName("lbl_slash")
self.lbl_slash.setText("/")
# FIELDS
self.txt_title = QtWidgets.QTextBrowser(Form)
self.txt_title.setGeometry(QtCore.QRect(90, 10, 261, 21))
self.txt_title.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_title.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_title.setObjectName("txt_title")
self.txt_cinedate = QtWidgets.QTextBrowser(Form)
self.txt_cinedate.setGeometry(QtCore.QRect(90, 40, 81, 21))
self.txt_cinedate.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_cinedate.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_cinedate.setObjectName("txt_cinedate")
self.txt_description = QtWidgets.QTextBrowser(Form)
self.txt_description.setGeometry(QtCore.QRect(10, 90, 341, 321))
self.txt_description.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.txt_description.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_description.setObjectName("txt_description")
self.view_poster = QtWidgets.QLabel(Form)
self.view_poster.setScaledContents(True)
self.view_poster.setGeometry(QtCore.QRect(365, 10, 261, 401))
self.view_poster.setObjectName("view_poster")
self.txt_entry = QtWidgets.QTextBrowser(Form)
self.txt_entry.setGeometry(QtCore.QRect(250, 440, 51, 21))
self.txt_entry.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_entry.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_entry.setObjectName("txt_entry")
self.txt_entrys = QtWidgets.QTextBrowser(Form)
self.txt_entrys.setGeometry(QtCore.QRect(330, 440, 51, 21))
self.txt_entrys.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_entrys.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.txt_entrys.setObjectName("txt_entrys")
# BUTTONS
self.btn_first = QtWidgets.QPushButton(Form)
self.btn_first.setGeometry(QtCore.QRect(50, 440, 75, 23))
self.btn_first.setObjectName("btn_first")
self.btn_first.setText("Erster")
self.btn_prev = QtWidgets.QPushButton(Form)
self.btn_prev.setGeometry(QtCore.QRect(150, 440, 75, 23))
self.btn_prev.setObjectName("btn_prev")
self.btn_prev.setText("Vorheriger")
self.btn_next = QtWidgets.QPushButton(Form)
self.btn_next.setGeometry(QtCore.QRect(410, 440, 75, 23))
self.btn_next.setObjectName("btn_next")
self.btn_next.setText("Nächster")
self.btn_last = QtWidgets.QPushButton(Form)
self.btn_last.setGeometry(QtCore.QRect(510, 440, 75, 23))
self.btn_last.setObjectName("btn_last")
self.btn_last.setText("Letzter")
self.open()
# self.btn_first.clicked.connect(self.first)
# self.btn_prev.clicked.connect(self.prev)
self.btn_next.clicked.connect(self.next)
# self.btn_last.clicked.connect(self.last)
QtCore.QMetaObject.connectSlotsByName(Form)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Thilo