Py/sql/tk- q simple GUI example

#1

Hello codecadamy users,
I’m currently in level 4 in the learning path ‘data science’.
After the introduction in sql it continues with python3. Unfortunately, sql is currently not considered. That’s why I paused the course to learn how to use sql in python.
I learned something tkinter to make it more fun. tkinter stands for tk interface for python.
It makes it possible to create platform-independent GUIs in the simplest way.
Here I have programmed a small example:

Screenshot:
mini-sql-browser

source to copy:
from tkinter import *
import sqlite3 as lite

def text_inp():
con=lite.connect(db_entry.get())
with con:
cur=con.cursor()
cur.execute(sql_entry.get())
rows=cur.fetchall()
for row in rows:
listb.insert(END,row)

def clear_listb():
listb.delete(0,‘end’)

mw = Tk()
mw.title(“mini-sql-browser”)

db_lb=Label(mw,text=‘Database:’,bg=‘lightCyan2’)
db_entry=Entry(mw,width=30)
table_lb=Label(mw,text=‘Table:’,bg=‘lightCyan2’)
table_entry=Entry(mw,width=30)
sql_lb=Label(mw,text=‘SQlite query:’,bg=‘lightCyan2’)
sql_entry=Entry(mw,width=30)
sendbt= Button(mw, text=‘Push to send’,bg=‘khaki1’,command=text_inp)
listb=Listbox(mw,height=7,width=30,bg=‘burlywood1’)
scroll_v=Scrollbar(mw,orient=VERTICAL,command=listb.yview)
listb[‘yscrollcommand’]=scroll_v.set
clear_bt=Button(mw,text=‘Clear Listbox’,bg=‘khaki2’,command=clear_listb)

db_lb.grid(row=0,column=0,sticky=(W,E))
db_entry.grid(row=1,column=0,sticky=(W,E))
table_lb.grid(row=2,column=0,sticky=(W,E))
table_entry.grid(row=3,column=0,sticky=(W,E))
sql_lb.grid(row=4,column=0,sticky=(W,E))
sql_entry.grid(row=5,column=0,sticky=(W,E))
sendbt.grid(row=6,column=0)
listb.grid(row=0,column=1,rowspan=6)
listb.insert(END,"—Here comes the query-result:—")
scroll_v.grid(row=0,column=2,rowspan=6,sticky=(N,S))
clear_bt.grid(row=6,column=1)
mw.mainloop()

  • click inside the widget to insert content
  • no return / focus implemented
  • error-code not implemented
  • the database must be in the same directory as the program
  • the table must exist
  • only python3 contains sqlite3 and tk

Do you like it?
Should I post more examples?

greetings
Stefan

1 Like