我应该能够使用一个循环做下面的,而不是写出来更多的widget,比我会永远需要的,缩短我的代码。此是我现在做的方式:如何在循环中创建具有不同名称的多个tkinter小部件?
db = sqlite3.connect('/path/to/database')
cursor = db.cursor()
cursor.execute("SELECT Drug FROM database where current >= 1")
allrec = cursor.fetchall()
numrec = len(allrec)
cursor.execute("SELECT Drug FROM database where current >= 1")
results = cursor.fetchone()
if numrec == 0:
exit
else:
c1=Checkbutton(frame1,variable=var1)
c1.grid(row=0,column=0,sticky='nw')
c1.config(bg='black')
e1=Entry(frame1, bg="black", fg="white")
e1.grid(row=0, column=1, sticky=NW)
e1.delete(0, END)
for row in results:
e1.insert(END, *results)
results = cursor.fetchone()
if numrec <= 1:
quit
else:
c2=Checkbutton(frame1,variable=var2)
c2.grid(row=1,column=0,sticky='nw')
c2.config(bg='black')
e2=Entry(frame1, bg="black", fg="white")
e2.grid(row=1, column=1, sticky=NW)
e2.delete(0, END)
for row in results:
e2.insert(END, *results)
record 3 ........
record 4 ........
.......
.......
record 15 .......
这创建15个不同名称的复选框和输入框,以便我可以从我的数据库中插入记录。
使用一个列表,然后使用'record [0]'访问等。 – matsjoyce 2014-11-21 17:52:35