'Python GUI using tkinter and db connection [duplicate]
I am not able to get any output from the following program.What i am trying to do here is store employee name and salary in a database and later delete database entries based on name and id.It gives no error. I think its a database error. it is not inserting any values in the db . Please help. Thanks
import Tkinter
import sqlite3
import ttk
def put(*args):
try:
e_name = str(i_name)
e_pay = int(i_pay)
conn = sqlite3.connect('medha.db')
c = conn.cursor()
c.execute('create table if not exists employee(id integer primary key auto increment, name text, salary integer)')
c.execute('insert into employee(name, salary) values(?,?)',(e_name,e_pay))
conn.close()
notice = 'insertion successful'
except:
notice ='insert proper values'
def del_id(*args):
try:
e_id = int(d_del)
conn = sqlite3.connect('medha.db')
c = conn.cursor()
c.execute('delete from employee where id =?',(e_id))
notice = 'deleted successfully'
conn.close()
except:
notice = 'insert proper values'
def del_name(*args):
try:
e_name = int(d_del)
conn = sqlite3.connect('medha.db')
c = conn.cursor()
c.execute('delete from employee where name = ?',(e_name))
notice = 'deleted successfully'
conn.close()
except:
notice = 'insert proper values'
root = Tkinter.Tk()
root.title('Python Projet - gui and db')
mainframe = ttk.Frame(root,padding='3 3 12 12' )
mainframe.grid(column=0, row=0)
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight =1)
i_name = str()
i_id = str()
i_pay = str()
notice = str()
d_del = str()
i_name_entry = ttk.Entry(mainframe, width =7, textvariable = i_name)
i_name_entry.grid(column = 1, row = 1)
i_id_entry = ttk.Entry(mainframe, width =7, textvariable = i_id)
i_id_entry.grid(column = 2, row = 1)
i_pay_entry = ttk.Entry(mainframe, width =7, textvariable = i_pay)
i_pay_entry.grid(column = 3, row = 1)
i_del_entry = ttk.Entry(mainframe, width =7, textvariable = d_del)
i_del_entry.grid(column = 1, row =4)
ttk.Label(mainframe, text ='Name of Employee').grid(column = 1, row = 0)
ttk.Label(mainframe, text ='Employee ID').grid(column = 2, row =0)
ttk.Label(mainframe, text ='Employee Pay').grid(column = 3, row=0)
ttk.Label(mainframe, text ='Name/ID of Employee to delete').grid(column = 1, row=3)
ttk.Label(mainframe, text = notice).grid(column = 1, row = 6)
ttk.Button(mainframe, text='Insert', command = put).grid(column=3, row=2)
ttk.Button(mainframe, text='Delete By ID', command = del_id).grid(column =1, row = 5)
ttk.Button(mainframe, text='Delete By Name', command = del_name).grid(column = 3, row=5)
for child in mainframe.winfo_children(): child.grid_configure(padx=5,pady=5)
i_name_entry.focus()
root.mainloop()
Any help is appreciated.
Solution 1:[1]
ttk.Label(mainframe, text = notice).grid(column = 1, row = 6)
Changing the value of the string notice
won't update the text in this label. You need to use a StringVar for this.
notice = Tkinter.StringVar()
ttk.Label(mainframe, textvariable = notice).grid(column = 1, row = 6)
Then, instead of doing notice = "whatever"
in your functions, do notice.set("whatever")
.
Alternatively, don't have a notice
value at all. Just reconfig the text variable directly.
notice_label = ttk.Label(mainframe, text = "")
notice_label.grid(column = 1, row = 6)
(note: don't try to assign notice_label
and do grid
on the same line, it won't work)
Then do notice_label.config(text = "whatever")
instead of notice = "whatever"
.
Solution 2:[2]
First try to see if you are connected to your database. With postgreSQL you can connect via psycopg2
import psycopg2
try:
conn = psycopg2.connect(database="user", user="postgres", password="silverTip", host="localhost")
print("connected")
except:
print ("I am unable to connect to the database")
cur =conn.cursor()
so if you are connected to database, it will print connected, else you will get message unable to connect to database
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Dulangi_Kanchana |