I am trying to create a selection menu for questions with four options. I am instantiating four radio buttons, one for each choice. For some reason when the form shows up, the last three are already selected. When you click on one, only that button is selected, and there others become deselected. I would like it so the original version of the form has none of them selected.
self.radio_button_frame = Frame(self)
self.radio_button_frame.grid(row=1, column=1, rowspan=4)
self.correct_label = Label(self, text="Correct: " + str(self.health_regain), width=25, justify=RIGHT)
self.correct_label.grid(row=0, column=3)
#space the image so it takes up 4 rows
self.hydra_label.grid_forget()
self.hydra_label.grid(row=0, column=0, rowspan=6)
#get the question
self.question = self.question_base.get_question()
self.answer = 0
#get rid of the begin button
self.begin_button.grid_forget()
#create radio buttons for each question choice
self.description_label.config(text=self.question.question)
self.radio_button_1 = Radiobutton(self.radio_button_frame, text=self.question.choice_a, padx=10, value=0, justify=LEFT, variable= self.answer)
self.radio_button_1.pack(anchor=W)
self.radio_button_2 = Radiobutton(self.radio_button_frame, text=self.question.choice_b, padx=10, value=1, justify=LEFT, variable= self.answer)
self.radio_button_2.pack(anchor=W)
self.radio_button_3 = Radiobutton(self.radio_button_frame, text=self.question.choice_c, padx=10, value=2, justify=LEFT, variable= self.answer)
self.radio_button_3.pack(anchor=W)
self.radio_button_4 = Radiobutton(self.radio_button_frame, text=self.question.choice_d, padx=10, value=3, justify=LEFT, variable= self.answer)
self.radio_button_4.pack(anchor=W)
self.answer_button = Button(self, text="Answer")
self.answer_button.grid(row=5, column=1, columnspan=2)
The variable that you need to give to Radiobutton should be Tkinters IntVar()
. So instead of:
self.answer = 0
put:
self.answer = IntVar() #We declare it like this
self.answer.set(-1) #If you want none of your radionbuttons to be selected, give the IntVar value that isn't set in any Radiobutton.
To get the value of self.answer
, just call self.answer.get()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.