PostgreSQL and tkinter

Dul’s Blog
2 min readNov 9, 2019

I found very less tkinter tutorials, hope this helps anyone looking forward for code.

There are lots of tutorials out there for web development, but actually a very few for GUI application building, and GUI applications to build with python one solution is Tkinter.

My app.py code

from tkinter import *
from models import *
window = Tk()
window.title(“Join”)
btn=Button(window, text=”OK”, fg=’blue’)
btn.place(x=130, y=150)
lbl=Label(window, text=”This is Label widget”, fg=’red’, font=(“Helvetica”, 16))
lbl.place(x=60, y=50)
txtfld=Entry(window, text=”This is Entry Widget”, bd=5)
txtfld.place(x=80, y=100)
window.title(‘Hello Python’)
window.geometry(“300x200+10+10”)
window.mainloop()

My models.py code

from tkinter import *
import psycopg2
try:
conn = psycopg2.connect(database="flaskmovie", user="postgres", password="silverTip", host="localhost")
print("connected")
except:
print ("I am unable to connect to the database")
cur =conn.cursor()
cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",(100, "abc'def"))
print(coins)conn.commit()
cur.close()
conn.close()

I created the connection with PostgreSQL , and create table of test within the database called flaskmovie. Remember before creating the table to have already made the database at PostgreSQL.

The table called test, has 3 columns, id, num and data. Serial, integer, and varchar are data types present in PostgreSQL.

By the cur.execute statement I’m adding values as 100 to num, and abc’def to data, the id column is getting auto-incremented, so it should be displayed as one.

By the print(coins), statement I m printing out on the command prompt’s user entered/execued via cur.execute statement.

Thank you.

--

--