我无法从以下程序获得任何输出。我在这里尝试做的是将员工姓名和工资存储在数据库中,然后根据名称和id删除数据库条目。它不会出错。我认为这是一个数据库错误。它没有在db中插入任何值。请帮忙。感谢
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()
感谢任何帮助。
答案 0 :(得分:0)
ttk.Label(mainframe, text = notice).grid(column = 1, row = 6)
更改字符串notice
的值不会更新此标签中的文本。您需要使用StringVar。
notice = Tkinter.StringVar()
ttk.Label(mainframe, textvariable = notice).grid(column = 1, row = 6)
然后,不要在您的函数中执行notice = "whatever"
,而是notice.set("whatever")
。
或者,根本没有notice
值。只需直接重新配置文本变量。
notice_label = ttk.Label(mainframe, text = "")
notice_label.grid(column = 1, row = 6)
(注意:请勿尝试在同一行分配notice_label
并执行grid
,否则无效)
然后执行notice_label.config(text = "whatever")
而不是notice = "whatever"
。
答案 1 :(得分:0)
首先尝试查看您是否已连接到数据库。使用postgreSQL,您可以通过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()
因此,如果您已连接到数据库,它将打印已连接, 否则您将收到无法连接数据库的消息