我有一个按钮保存条目中的内容,并且由于某种原因,它没有正确地将其保存到该功能的外部列表中,就像在函数Submit()里面一样,它记住条目值就好了。 以下是设置条目的功能部分:
Q8answer = Entry(r)
Q8answer.config(fg = "blue",bg = "white",font = ("Arial",20),)
Q8answer.place(relx = 0.5,rely = 0.5,anchor = "center")
submit = Button(r, text = "Submit THIS Answer",command = Submit)
submit.place(relx = 0.5, rely = 0.8, anchor = "center")
这是名为Submit()的函数:
def Submit():
answers[7] = str(Q8answer.get())
print(answers)
顺便说一下,我确实需要全部化所有变量,因为其他答案是按钮问题(答案[0]到答案[6])的多项选择,所有答案都正常工作并保存到列表中。
编辑:还有更多背景:
Q8answer.place_forget()
submit.place_forget()
Q8answer = Entry(r)
Q8answer.config(fg = "blue",font = ("Arial",20),)
submit = Button(r, text = "Submit THIS Answer", command = Submit() )
Amode = 0
Bmode = 0
Cmode = 0
Dmode = 0
Abutton.config(image = unselected)
Bbutton.config(image = unselected)
Cbutton.config(image = unselected)
Dbutton.config(image = unselected)
if mode == 1:
if questionNumber < len(questions):
questionNumber += 1
if questionNumber == 8:
Correct()
elif questionNumber == 7:
Abutton.place_forget()
Bbutton.place_forget()
Cbutton.place_forget()
Dbutton.place_forget()
Alabel.place_forget()
Blabel.place_forget()
Clabel.place_forget()
Dlabel.place_forget()
Number = Label(r,text = "Question " + str(questionNumber + 1))
Number.config(fg = "blue",bg = "grey", font = ("Arial",15))
Number.place(relx = 0.02, rely = 0.02 )
question = questions[questionNumber]
Question.place_forget()
Question = Label(r,text = question)
Question.config(fg = "blue",bg = "grey")
Question.config(font = ("Arial",20))
Question.place(relx = 0.5, rely = 0.15, anchor = "center")
Q8answer = Entry(r)
Q8answer.config(fg = "blue",bg = "white",font = ("Arial",20),)
Q8answer.place(relx = 0.5,rely = 0.5,anchor = "center")
submit = Button(r, text = "Submit THIS Answer",command = Submit)
submit.place(relx = 0.5, rely = 0.8, anchor = "center")
else:
Q8answer.place_forget()
submit.place_forget()
在else语句之后,它与此无关。
答案 0 :(得分:1)
我认为问题在于您将按钮设置为执行的命令。
例如以下代码
tk.Button(..., command=submit())
在此代码中,即使在主循环运行之前,也将执行命令commit(),并且在那个时间实例中,答案[7]不存在。
只需将命令样式更改为
tk.Button(..., command=submit)
它应该可以工作。
更多-(有用的信息)
对于需要提供参数的函数,lambda关键字的用法如下:
tk.Button(..., command=lambda x: submit(x))