英文:
How do i get user details to autofill into entry boxes after i get them from my sql database
问题
我希望输入框中填充用户的数据。
英文:
I have an entry form; when the user types in their username and presses the button, I want it to autofill with their details from the sql database. I know you need to use state=DISABLED, if im correct, but I dont know how to use this.
self.userentered=StringVar()
#fill out entry form
Label(self.master,text="Final steps to entering your race...",font=(20),fg="blue").place(x=10,y=10)
Label(self.master,text="Username:",width=20).place(x=10,y=50)
self.userentry=Entry(self.master,textvariable=self.userentered)
self.userentry.place(x=160,y=50)
Button(self.master,text="Search Details",command=self.searchnames,width=15).place(x=300,y=50)
db=sqlite3.connect('project races.db')
cursor = db.cursor()
u = self.userentered.get()
sql = "SELECT Name from AthleteDetails where username = ?"
cursor.execute(sql,(u,))
name = cursor.fetchone()
sql = "SELECT DOB from AthleteDetails where username = ?"
cursor.execute(sql,(u,))
dob = cursor.fetchone()
sql = "SELECT Gender from AthleteDetails where username = ?"
cursor.execute(sql,(u,))
gender = cursor.fetchone()
sql = "SELECT Email from AthleteDetails where username = ?"
cursor.execute(sql,(u,))
email = cursor.fetchone()
sql = "SELECT Club from AthleteDetails where username = ?"
cursor.execute(sql,(u,))
club = cursor.fetchone()
Label(self.master,text="Full Name",width=20).place(x=10,y=70)
self.nameentry=Entry(self.master,textvariable=name,state=DISABLED)
self.nameentry.place(x=150,y=70)
Label(self.master,text="Date of Birth",width=20).place(x=10,y=90)
self.dobentered = Entry(self.master,textvariable=dob,state=DISABLED)
self.dobentered.place(x=150,y=90)
Label(self.master,text="Gender",width=20).place(x=10,y=110)
self.genderBox = Entry(self.master,textvariable=gender,state=DISABLED)
self.genderBox.place(x=150,y=110)
Label(self.master,text="Club",width=20).place(x=10,y=130)
self.clubentry=Entry(self.master,textvariable=club,state=DISABLED)
self.clubentry.place(x=150,y=130)
Label(self.master,text="Email",width=20).place(x=10,y=150)
self.emailentry=Entry(self.master,textvariable=email,state=DISABLED)
self.emailentry.place(x=150,y=150)
Label(self.master,text="Time prediction:",font=(14)).place(x=10,y=170)
time = StringVar()
self.timeentry=Entry(self.master,textvariable=time)
self.timeentry.place(x=150,y=170)
Label(self.master,text="*To nearest minute",font=(12)).place(x=300,y=170)
答案1
得分: 1
将状态更改为正常,使用小部件的 insert
方法插入数据,然后将其更改为禁用。
self.nameentry.configure(state="normal")
self.nameentry.delete(0, "end") # 仅在之前有值时才需要
self.nameentry.insert(0, name)
self.nameentry.configure(state="disabled")
英文:
Change the state to normal, insert the data using the widget's insert
method, then change it back to disabled.
self.nameentry.configure(state="normal")
self.nameentry.delete(0, "end") # only necessary if it previously had a value
self.nameentry.insert(0,name)
self.nameentry.configure(state="disabled")
答案2
得分: 1
请注意,name
、dob
、gender
、email
和 club
可能为 None
(在数据库中找不到记录)或 tuple
(在数据库中找到记录)。它们不是 StringVar()
的实例,因此将它们用作 Entry
小部件的 textvariable
选项将不会自动更新小部件。
此外,您可以使用一个 SQL 查询来获取它们的所有信息,而不是五个 SQL 查询,然后使用 SQL 查询的结果创建这些 StringVar()
的实例,如下所示:
db = sqlite3.connect('project races.db')
cursor = db.cursor()
u = self.userentered.get()
# 使用单个 SQL 查询获取所有信息
sql = "SELECT Name, DOB, Gender, Email, Club FROM AthleteDetails WHERE username = ?"
cursor.execute(sql, (u,))
result = cursor.fetchone()
if result:
# 创建所需的 StringVar 并设置它们的值
name = StringVar(value=result[0])
dob = StringVar(value=result[1])
gender = StringVar(value=result[2])
email = StringVar(value=result[3])
club = StringVar(value=result[4])
# 用户详细信息
Label(self.master, text="Full Name", width=20).place(x=10, y=70)
self.nameentry = Entry(self.master, textvariable=name, state=DISABLED)
self.nameentry.place(x=150, y=70)
Label(self.master, text="Date of Birth", width=20).place(x=10, y=90)
self.dobentered = Entry(self.master, textvariable=dob, state=DISABLED)
self.dobentered.place(x=150, y=90)
Label(self.master, text="Gender", width=20).place(x=10, y=110)
self.genderBox = Entry(self.master, textvariable=gender, state=DISABLED)
self.genderBox.place(x=150, y=110)
Label(self.master, text="Club", width=20).place(x=10, y=130)
self.clubentry = Entry(self.master, textvariable=club, state=DISABLED)
self.clubentry.place(x=150, y=130)
Label(self.master, text="Email", width=20).place(x=10, y=150)
self.emailentry = Entry(self.master, textvariable=email, state=DISABLED)
self.emailentry.place(x=150, y=150)
Label(self.master, text="Time prediction:", font=(14)).place(x=10, y=170)
time = StringVar()
self.timeentry = Entry(self.master, textvariable=time)
self.timeentry.place(x=150, y=170)
Label(self.master, text="*To nearest minute", font=(12)).place(x=300, y=170)
英文:
Note that name
, dob
, gender
, email
and club
may be None
(no record found in database) or a tuple
(record found in database). They are not instances of StringVar()
, so using them as textvariable
option of Entry
widget will not update the widgets automatically.
Also you can use one SQL query to fetch all of them instead of five SQL queries, and then create those instances of StringVar()
with the result of SQL query as below:
db = sqlite3.connect('project races.db')
cursor = db.cursor()
u = self.userentered.get()
# use single SQL query to get all the information
sql = "SELECT Name, DOB, Gender, Email, Club FROM AthleteDetails WHERE username = ?"
cursor.execute(sql, (u,))
result = cursor.fetchone()
if result:
# create required StringVar and set their values
name = StringVar(value=result[0])
dob = StringVar(value=result[1])
gender = StringVar(value=result[2])
email = StringVar(value=result[3])
club = StringVar(value=result[4])
# user details
Label(self.master,text="Full Name",width=20).place(x=10,y=70)
self.nameentry=Entry(self.master,textvariable=name,state=DISABLED)
self.nameentry.place(x=150,y=70)
Label(self.master,text="Date of Birth",width=20).place(x=10,y=90)
self.dobentered = Entry(self.master,textvariable=dob,state=DISABLED)
self.dobentered.place(x=150,y=90)
Label(self.master,text="Gender",width=20).place(x=10,y=110)
self.genderBox = Entry(self.master,textvariable=gender,state=DISABLED)
self.genderBox.place(x=150,y=110)
Label(self.master,text="Club",width=20).place(x=10,y=130)
self.clubentry=Entry(self.master,textvariable=club,state=DISABLED)
self.clubentry.place(x=150,y=130)
Label(self.master,text="Email",width=20).place(x=10,y=150)
self.emailentry=Entry(self.master,textvariable=email,state=DISABLED)
self.emailentry.place(x=150,y=150)
Label(self.master,text="Time prediction:",font=(14)).place(x=10,y=170)
time = StringVar()
self.timeentry=Entry(self.master,textvariable=time)
self.timeentry.place(x=150,y=170)
Label(self.master,text="*To nearest minute",font=(12)).place(x=300,y=170)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论