【发布时间】:2020-11-29 11:41:41
【问题描述】:
我正在创建一个多项选择测试,一个问题将包含 3 个部分(下面仅显示 2 个 - 完成后将添加第 3 个)。
我希望用户回答所有部分,然后点击提交按钮记录答案。
我在课堂上有提示/选择。
我显示选项和单选按钮,一切正常。当所有部分都没有回答时,它会发出警告。但是我的方法很愚蠢。我目前显示一条消息,然后在做出所有选择并再次单击按钮时简单地用空白覆盖它。我不能.destroy 或.remove 因为如果我的条件不是我的意思,则创建了子标签并且我得到了一个错误。我可能应该将显示器放在计时器上,然后将其移除。
理想情况下,在做出所有选择之前,我的提交按钮将被禁用,但我还没有弄清楚。
那么您能帮我处理警告消息还是禁用提交按钮,直到所有单选按钮组都有选择?
# First Group of Radio buttons
Radiobutton(manuframe, text=manu_questions.prompt1, padx=xpad, pady=ypad, bg=background,
fg=text_color, font=(text_type, text_height), variable=man_answer,
value=1).pack(anchor=W)
Radiobutton(manuframe, text=manu_questions.prompt2, padx=xpad, pady=ypad, bg=background,
fg=text_color, font=(text_type, text_height), variable=man_answer,
value=2).pack(anchor=W)
Radiobutton(manuframe, text=manu_questions.prompt3, padx=xpad, pady=ypad, bg=background,
fg=text_color, font=(text_type, text_height), variable=man_answer,
value=3).pack(anchor=W)
Radiobutton(manuframe, text=manu_questions.prompt4, padx=xpad, pady=ypad, bg=background,
fg=text_color, font=(text_type, text_height), variable=man_answer,
value=4).pack(anchor=W)
#Second Group of Radio Buttons
Radiobutton(modelframe, text=model_questions.prompt1, padx=xpad, pady=ypad, bg=background,
fg=text_color, font=(text_type, text_height), variable=mod_answer,
value=1).pack(anchor=W)
Radiobutton(modelframe, text=model_questions.prompt2, padx=xpad, pady=ypad, bg=background,
fg=text_color, font=(text_type, text_height), variable=mod_answer,
value=2).pack(anchor=W)
Radiobutton(modelframe, text=model_questions.prompt3, padx=xpad, pady=ypad, bg=background,
fg=text_color, font=(text_type, text_height), variable=mod_answer,
value=3).pack(anchor=W)
Radiobutton(modelframe, text=model_questions.prompt4, padx=xpad, pady=ypad, bg=background,
fg=text_color, font=(text_type, text_height), variable=mod_answer,
value=4).pack(anchor=W)
#Submit Button
submitButton = Button(btm_frame2, text="Submit", command=lambda:submit(q, question_count,
mod_answer, man_answer))
submitButton.pack()
#Submission function - Check all questions answered, if so increment question.
# index and continue.
def submit(q, question_count, mod_answer, man_answer):
if mod_answer.get() == 0 or man_answer.get() == 0:
warn_label =Label(btm_frame2, text="You didn't answer", bg="red", fg="white")
warn_label.place(relx=.5, y=10, anchor=CENTER)
else:
replace_label = Label(btm_frame2,
text=" ",
bg="green", fg="white")
replace_label.place(relx=.5, y=10, anchor=CENTER)
if q == question_count:
raise SystemExit(0)
else:
q = q + 1
【问题讨论】:
-
我建议使用
tlinter.messagebox对话框之一在用户单击提交时通知他们没有回答所有部分。这里有一些info 可用的种类。另请注意,您应该准确提及您在问题中遇到的错误或警告。 -
@martineau 你的意思是
tkinter.messagebox? -
是的,我的意思是
tkinter.messagebox。无论如何,如果您edit 您的问题并提供可运行的minimal reproducible example,人们可以向您展示如何实现您想要的。
标签: python tkinter radio-button