好的,所以我正在尝试为一个存储名字和特定信息的数据库制作一个gui。现在我正处于这个项目的最后阶段,我遇到了一个我认为可以解决的问题。我有一个for循环,它将变量(Label)和.packs变量更改为GUI,这一切都很好,直到我需要删除这个变量(Label),因为它被声明为最后一个,因为它是一个for循环,任何想法?
def read_from_database():
global label4
global But2
global Counter
global e4
global Namelist
if(Counter == 0):
Counter += 1
But4.config(relief=SUNKEN)
But5.config(relief=SUNKEN)
But6.config(relief=SUNKEN)
sql = ("SELECT * FROM nameinfo")
for row in c.execute(sql): # Here is the for loop
Namelist = Label(root, text=row[0])
Namelist.pack()
label4 = Label(root, text="Name:")
label4.pack(side=LEFT)
e4 = Entry(root)
e4.pack(side=LEFT)
But2.pack()
def submit(): # submit your results
global Readinfo
name = (e4.get())
label4.destroy()
But2.pack_forget()
e4.destroy()
sql = ("SELECT * FROM nameinfo")
for sql1 in c.execute(sql):
Namelist.pack_forget()
sql = "SELECT * FROM nameinfo WHERE name = ?"
for row in c.execute(sql, (name, )):
NAME = ('Name: ' + row[0])
AGE = (', Age: ' + str(row[1]))
COLOR = (', Favorite color: ' + row[2])
Readinfo = Label(root, text=NAME + AGE + COLOR, bg='black',
fg='white')
Readinfo.pack()
Clear.pack(padx=8, pady=5)
def clear(): # How I later clear the results
global Counter
Counter += -1
try:
Readinfo
except NameError:
pass
else:
Readinfo.pack_forget()
Clear.pack_forget()
But4.config(relief=RAISED)
But5.config(relief=RAISED)
But6.config(relief=RAISED)
Clear = Button(root, text='Clear', fg='black', bg='green', command=clear)
But2 = Button(root, text='Submit', bg='white', fg='blue', command=submit)
好的,这是我阅读部分中包含的所有代码。我知道它非常草率,可能不需要某些部件或任何东西。我将在未来的编程中提出建议,也可能是这个,我很新,并会接受建议:)。