Taylor's Blog

Atypical ramblings

Browsing produce at SQL Mart

This past week I dove head-first into SQL! It’s something I’m completely new to, but I’ve managed to get the hang of it faster than I thought. As I mentioned in my last post, I’m using SQLite and was fiddling around in the command prompt to get the basics of it. Now that I have a rudimentary understanding of the syntax, I’ve started using DB Browser to assist me in creating and editing databases. It is a really helpful tool.

I’ve tinkered with Tkinter and managed to get a test database working in Python. I even succeeded in creating checkboxes that correctly select the appropriate data from my database too! This is really awesome because checkboxes are the current tool I’m planning on using in my final program.  Here’s a look at what I’ve created:

fruitStore

Here’s the database I’m using:

foodStore

And the Python code:

[python]
import sqlite3
from tkinter import *

root = Tk()

conn = sqlite3.connect(“C:/code/py3/SQLitePractice/test2.db”)
cursor = conn.cursor()
conn.commit()

def connectData(*args):
T.delete(1.0, END)
if fruitVar.get() == 1 and vegVar.get() == 1:
for row in cursor.execute(“SELECT Name FROM Food”):
T.insert(END, row)
T.insert(END, ‘\n’)
elif fruitVar.get() == 0 and vegVar.get() == 1:
for row in cursor.execute(“SELECT Name FROM Food WHERE type=’Vegetable'”):
T.insert(END, row)
T.insert(END, ‘\n’)
elif fruitVar.get() == 1 and vegVar.get() == 0:
for row in cursor.execute(“SELECT Name FROM Food WHERE type=’Fruit'”):
T.insert(END, row)
T.insert(END, ‘\n’)
elif fruitVar.get() == 0 and vegVar.get() == 0:
T.insert(END, ‘Sorry, no results.’)

fruitVar = IntVar(root)
fruitVar.trace(‘w’, connectData)
fCheck=Checkbutton(root, text=”Fruit!”, variable=fruitVar)
fCheck.pack()

vegVar = IntVar(root)
vegVar.trace(‘w’, connectData)
vCheck=Checkbutton(root, text=”Veggies!”, variable=vegVar)
vCheck.pack()

S = Scrollbar(root)
T = Text(root, height=8, width=50)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)

root.mainloop()
[/python]

Updated: May 10, 2015 — 9:04 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

Taylor's Blog © 2015