Created
December 14, 2020 16:09
-
-
Save 0x00002a/73f7a0e6d793a4d9c1fe33768ecba089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
from tkinter import ttk | |
import sqlite3 as sqlite | |
class DB: | |
_conn = None | |
def __init__(self): | |
self._conn = sqlite.connect("example_database.db") | |
self._conn.execute(""" | |
CREATE TABLE IF NOT EXISTS students ( | |
first_name TEXT, | |
second_name TEXT, | |
dob DATE, | |
sin INTEGER PRIMARY KEY | |
); | |
""") | |
self._conn.commit() | |
def insert_record(self, first_name: str, second_name: str, sin: str): | |
self._conn.execute(""" | |
INSERT INTO students (first_name, second_name, dob, sin) VALUES ( | |
?, | |
?, | |
'01/01/1970', | |
? | |
); | |
""", [first_name, second_name, sin]) | |
self._conn.commit() | |
def students_with_surname(self, surname: str) -> list: | |
cursor = self._conn.cursor() | |
cursor.execute(""" | |
SELECT first_name, dob, sin FROM students WHERE second_name = ? | |
""", [surname]) | |
return cursor.fetchall() | |
def print_records_with_surname(surname: str): | |
db = DB() | |
result = db.students_with_surname(surname) | |
for record in result: | |
print(record) | |
def on_ok_clicked(): | |
contents = text_entry.get() | |
print_records_with_surname(contents) | |
def create_insert_form(callback): | |
popup = tk.Toplevel() | |
ttk.Label(popup, text="First name: ").grid(row=0, column=0) | |
first_name_ent = ttk.Entry(popup) | |
first_name_ent.grid(row=0, column=1) | |
ttk.Label(popup, text="Second name: ").grid(row=1, column=0) | |
second_name_ent = ttk.Entry(popup) | |
second_name_ent.grid(row=1, column=1) | |
ttk.Label(popup, text="SIN: ").grid(row=2, column=0) | |
sin_ent = ttk.Entry(popup) | |
sin_ent.grid(row=2, column=1) | |
ttk.Button(popup, text="OK", command=lambda: callback(first_name_ent.get(), | |
second_name_ent.get(), | |
sin_ent.get())).grid(row=3, column=1) | |
def insert_record(name: str, surname: str, sin: str) -> None: | |
DB().insert_record(name, surname, sin) | |
root = tk.Tk() | |
lbl = ttk.Label(root, text="Enter you second name") | |
text_entry = ttk.Entry(root) | |
btns_frame = ttk.Frame(root) | |
cancel = ttk.Button(btns_frame, text="Cancel") | |
btn = ttk.Button(btns_frame, text="OK", command=on_ok_clicked) | |
lbl.pack() | |
text_entry.pack() | |
btns_frame.pack() | |
btn.grid(row=0, column=1) | |
cancel.grid(row=0, column=0) | |
create_insert_form(callback=insert_record) | |
root.mainloop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment