Thursday, September 27, 2018

Refcards: The Beginnings of an Interface

- The latest development in the evolution of the Refcards-System is the creation of a primitive interface;
- Now I can add records to my SQLite3 database through an interface;
- The interface has a button called "Add entry" which upon clicking adds the text ("gets" it) from the Entry and Text widgets in the interface and adds them ("INSERTs" them) directly into the database;
- I've been using Tkinter to create the interface ("frontend") and SQLite3 in Python for the database ("backend");
from Tkinter import *
import os
import sqlite3
os.chdir('C:\\FILEPATH')
# TO RUN ON FIRST PASS, TO CREATE OR OPEN DATABASE AND CREATE FIRST TABLE.
# conn = sqlite3.connect("refcards2019b.db")
# cursor = conn.cursor()
# cursor.execute("""CREATE TABLE refcards(date TEXT, note TEXT)""")
# conn.commit()
def insert(date,note):
conn = sqlite3.connect("refcards2019b.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO refcards VALUES (?,?)",(date, note))
conn.commit()
conn.close()
def add_command():
insert(date_text.get(), T.get("1.0","end-1c"))
root = Tk()
date_label = Label(root, text='Date')
date_label.grid(row=0, column=0)
date_text = StringVar()
date_entry = Entry(root, textvariable=date_text)
date_entry.grid(row=0, column=1)
note_label = Label(root, text='Note')
note_label.grid(row=1, column=0)
T = Text(root, height=20, width=20)
T.grid(row=2, column=1)
add_button = Button(root, text="Add entry", width=12, command=add_command)
add_button.grid(row=4, column=3)
root.mainloop()

No comments:

Post a Comment