![](/img/trans.png)
[英]So basically I need to take data a user inputs into a Tkinter entry box and store it into my sqlite3 database. I am using Python 3.6
[英]Issue - Tkinter need to check if a value in entry box is already in a specific column in my sqlite database table
我是这个社区的新手,我正在尝试学习 Python(我的 Windows 10 版本是 3.7)
我已经用 python/Tkinter 创建了这个程序(我将在下面附上代码)
这是我被困的地方:
当用户按下“提交”按钮时,在插入所有数据之前,我需要检查我的 sqlite3 表中是否已经存在输入框“分配编号”(如果您启动程序,它位于 GUI 的顶部)用户提交将存储在数据库中
基本上我需要提交的每个调查都有一个唯一的分配编号
如果你能帮忙就太好了(一周没睡了)
预先感谢您的所有帮助
import backend
import csv
from backend import write_to_csv
import os
from Send_email_gui import send_email
from backend import check_assignment
def add_entry_command():
#check_assignment_no.check_assignment(assignment_no_text.get())
if check_assignment(assignment_no_text.get())== 0:
backend.insert(client_code_text.get(), client_name_text.get(), assignment_no_text.get(), accountant_text.get(), year_end_text.get(), file_presentation_text.get(), quality_of_job_processed_text.get(), queries_text.get(), indexing_text.get(), final_reports_text.get(), documents_handling_text.get(), meeting_deadlines_text.get(), communication_text.get(), analytical_review_text.get(), overall_job_text.get(), suggestion_text.get())
send_email(accountant_text, client_code_text, client_name_text, assignment_no_text, year_end_text, file_presentation_text, quality_of_job_processed_text, queries_text, indexing_text, final_reports_text, documents_handling_text, meeting_deadlines_text, communication_text, analytical_review_text, overall_job_text, suggestion_text)
messagebox.showinfo('Confirmation', 'Thank you for your submission, click ok and close')
else:
messagebox.showinfo('Assignment number used', 'Assignment number already used, please check again and submit')
def __init__(self, assignment_no_, accountant_, year_end_, client_code_, client_name_, file_presentation_, quality_of_job_processed_, queries_,
indexing_, final_reports_, documents_handling_, meeting_deadlines_, communication_, analytical_review_, overall_rate_, suggestion_):
self.assignment_no_=assignment_no_
self.accountant_=accountant_
self.year_end_=year_end_
sel.client_code_=client_code_
self.client_name_=client_name_
self.file_presentation_=file_presentation_
self.quality_job_=quality_of_job_processed_
self.queries_assumptions_=queries_
self.indexing_cross_ref_=indexing_
self.final_reports_=final_reports_
self.doc_handling_=documents_handling_
self.meet_deadlines_=meeting_deadlines_
self.communi_=communication_
self.analytical_rev_=analytical_review_
self.overall_rate_=overall_rate_
self.suggestion_box_=suggestion_
window=Tk()
"""
Center the window of the program
"""
# Gets the requested values of the height and widht.
windowWidth = window.winfo_reqwidth()
windowHeight = window.winfo_reqheight()
#print("Width",windowWidth,"Height",windowHeight)
# Gets both half the screen width/height and window width/height
positionRight = int(window.winfo_screenwidth()/6 - windowWidth/6)
positionDown = int(window.winfo_screenheight()/6 - windowHeight/6)
# Positions the window in the center of the page.
window.geometry("+{}+{}".format(positionRight, positionDown))
"""
Top data entry level
TO DO - build function to return client name directly from Iris database - build button
"""
label_title=Label(window, text="Global Infosys feedback collector", height=3, fg="blue", font=(20))
label_title.grid(row=0, column=2)
l1=Label(window, text="Client code")
l1.grid(row=4, column=1, sticky='E')
l2=Label(window, text="Client name")
l2.grid(row=4, column=3, sticky='E')
l3=Label(window, text="Assignment no:")
l3.grid(row=5, column=1, sticky='E')
l4=Label(window, text="Accountant:")
l4.grid(row=6, column=1, sticky='E')
l5=Label(window, text="Year end:")
l5.grid(row=7, column=1, sticky='E')
"""
Second and third titles - form beginning
"""
label_second_title=Label(window, text="Scoring Referance (enter only 1 number):", height=3, fg="blue")
label_second_title.grid(row=8, column=1, columnspan=2, sticky=W)
label_third_title=Label(window, text="Excellent: 10 | Good: 9 - 8 | Avarage: 7 - 6 | Poor: 0 - 5", height=3, fg="blue")
label_third_title.grid(row=9, column=1, columnspan=2, sticky=W)
empty_label=Label(window, text=" ", width=15)
empty_label.grid(row=9, column=0)
second_empty_label=Label(window, text=" ", width=15)
second_empty_label.grid(row=4, column=6)
#third_empty_label=Label(window, text="View Entry", height=3)
#third_empty_label.grid(row=26, column=1)
"""
Form level labels
"""
l6=Label(window, text="File presentation:")
l6.grid(row=10, column=1, sticky='E')
l7=Label(window, text="Quality of job processing")
l7.grid(row=11, column=1, sticky='E')
l8=Label(window, text="Queries and assumptions:")
l8.grid(row=12, column=1, sticky='E')
l9=Label(window, text="Indexing and cross referencing:")
l9.grid(row=13, column=1, sticky='E')
l10=Label(window, text="Final reports (draft accounts):")
l10.grid(row=14, column=1, sticky='E')
l11=Label(window, text="Documents handling / usage:")
l11.grid(row=15, column=1)
l12=Label(window, text="Meeting deadlines:")
l12.grid(row=16, column=1, sticky='E')
l13=Label(window, text="Communication:")
l13.grid(row=17, column=1, sticky='E')
l14=Label(window, text="Analytical review:")
l14.grid(row=18, column=1, sticky='E')
l15=Label(window, text="Overall Job rating:")
l15.grid(row=19, column=1, sticky='E')
l16=Label(window, text="Suggestion / Review:")
l16.grid(row=20, column=1, sticky='E')
l25=Label(window, text="@ A production of Shehan H.", fg="red")
l25.grid(row=30, column=6, sticky='E')
"""
Entry boxes top
"""
client_code_text=StringVar()
e1=Entry(window, textvariable=client_code_text)
e1.grid(row=4, column=2)
client_name_text=StringVar()
e2=Entry(window, textvariable=client_name_text)
e2.grid(row=4, column=4)
assignment_no_text=StringVar()
e3=Entry(window, textvariable=assignment_no_text)
e3.grid(row=5, column=2)
accountant_text=StringVar()
e4=Entry(window, textvariable=accountant_text)
e4.grid(row=6, column=2)
year_end_text=StringVar()
e5=Entry(window, textvariable=year_end_text)
e5.grid(row=7, column=2)
"""
Entry boxes form level
"""
file_presentation_text=StringVar()
e6=Entry(window, textvariable=file_presentation_text)
e6.grid(row=10, column=2)
quality_of_job_processed_text=StringVar()
e7=Entry(window, textvariable=quality_of_job_processed_text)
e7.grid(row=11, column=2)
queries_text=StringVar()
e8=Entry(window, textvariable=queries_text)
e8.grid(row=12, column=2)
indexing_text=StringVar()
e9=Entry(window, textvariable=indexing_text)
e9.grid(row=13, column=2)
final_reports_text=StringVar()
e10=Entry(window, textvariable=final_reports_text)
e10.grid(row=14, column=2)
documents_handling_text=StringVar()
e11=Entry(window, textvariable=documents_handling_text)
e11.grid(row=15, column=2)
meeting_deadlines_text=StringVar()
e12=Entry(window, textvariable=meeting_deadlines_text)
e12.grid(row=16, column=2)
communication_text=StringVar()
e13=Entry(window, textvariable=communication_text)
e13.grid(row=17, column=2)
analytical_review_text=StringVar()
e14=Entry(window, textvariable=analytical_review_text)
e14.grid(row=18, column=2)
overall_job_text=StringVar()
e15=Entry(window, textvariable=overall_job_text)
e15.grid(row=19, column=2)
suggestion_text=StringVar()
e16=Entry(window, textvariable=suggestion_text, width=50)
e16.grid(row=20, column=2, padx=5,pady=10,ipady=3)
"""
Buttons
"""
b1=Button(window, text="Submit to GI", width=12, command=add_entry_command)
b1.grid(row=25, column=2)
b2=Button(window, text="Export report", width=12, command=write_to_csv)
b2.grid(row=25, column=3)
b3=Button(window, text="Connect client", width=12)
b3.grid(row=4, column=5)
"""
List box to view results
Maight be deleted and changed with popup to confirm
list1=Listbox(window, height=1, width=70)
list1.grid(row=26, column=2)
"""
window.mainloop()
这是后端模块
import sqlite3
import csv
import os
from tkinter import messagebox
from tkinter import *
def connect():
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS gi_store (id INTEGER PRIMARY KEY, client_code_ text, client_name_ text, assignment_no_ text, accountant_ text, year_end_ text, file_presentation_ integer, quality_of_job_processed_ integer, queries_ integer, indexing_ integer, final_reports_ integer, documents_handling_ integer, meeting_deadlines_ integer, communication_ integer, analytical_review_ integer, overall_job_ integer, suggestion_ text)")
conn.commit()
conn.close()
def insert(client_code_, client_name_, assignment_no_, accountant_, year_end_, file_presentation_, quality_of_job_processed_, queries_, indexing_, final_reports_, documents_handling_, meeting_deadlines_, communication_, analytical_review_, overall_job_, suggestion_):
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("INSERT INTO gi_store VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (client_code_, client_name_, assignment_no_, accountant_, year_end_, file_presentation_, quality_of_job_processed_, queries_, indexing_, final_reports_, documents_handling_, meeting_deadlines_, communication_, analytical_review_, overall_job_, suggestion_))
conn.commit()
conn.close()
def view():
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("SELECT * FROM gi_store")
rows=cur.fetchall()
conn.close()
return rows
def write_to_csv():
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("SELECT * FROM gi_store")
csvWriter = csv.writer(open("output.csv", "w", newline=''))
rows = cur.fetchall()
csvWriter.writerow(["ID", "Client Code", "Client Name", "Assignment number", "Accountant", "Year End", "File Presentation", "Quality of job processed", "Queries", "Indexing", "Final reports", "Doc. handling", "Meeting deadlines", "Communication", "Analytical review", "Overall rate", "Suggestion box"])
for row in rows:
# do your stuff
csvWriter.writerow(row)
conn.close()
def check_assignment(assignment_no_):
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
control_assignment = cur.execute("SELECT * FROM gi_store WHERE assignment_no_ = ?", (assignment_no_,))
control_assignment_row=cur.fetchone()
conn.close()
return control_assignment_row
connect()
#print(check_assignment(assignment_no_text="2"))
您的函数check_assignment()
返回带有分配编号或None
行,但您与0
进行比较。 你应该与None
比较
if check_assignment(assignment_no_text.get()) is None:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.