此函数从MySQL表中选择数据并将其作为List返回。然后使用此List定义tkinter OptionMenu。问题是,当我多次调用此函数时,tkinter小部件中的选项列表会翻倍。
def CreateSingleScoutAttendanceReportRaiseFrame():
ScoutList=[]
mycursor.execute("SELECT firstname,secondname FROM scoutinfo")
myresults=mycursor.fetchall()
print(myresults)
for i in myresults:
Temp=[i[0]]+[i[1]]
print(Temp)
ScoutList.append(Temp)
AttendanceScoutOptionMenuLabel = tk.Label(CreateSingleAttendanceReportFrame,text="Choose A Scout: ",font=LargeTextFont,bg="white")
AttendanceScoutOptionMenuLabel.grid(row=2,column=1)
由于您无法访问我的数据库,我可以向您显示照片。
根据我的理解,每次使用行ScoutList=[]
运行函数时我都会清除列表,因为它应该清除列表。我试过ScoutList.clear()
,但这也没用。
list_= CreateSingleAttendanceReportFrame()
AttendanceScoutOptionMenuLabel = tk.Label(list_,text="Choose A Scout: ",font=LargeTextFont,bg="white")
很确定应该有效。您将函数嵌套在另一个函数中,当您将列表返回给它时,它使列表成为全局变量。你在CreateSingleAttendanceReportFrame
中没有任何参数,所以当你说该函数中的列表为空时,它所做的只是告诉你的计算机你在该函数中有一个空列表,而不是该列表应该是空的。