Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 初始化单选按钮Tkinter_Python_Tkinter_Radio Button - Fatal编程技术网

Python 初始化单选按钮Tkinter

Python 初始化单选按钮Tkinter,python,tkinter,radio-button,Python,Tkinter,Radio Button,我正在尝试创建一个包含四个选项的问题选择菜单。我正在实例化四个单选按钮,每个选项一个。由于某种原因,当表单显示时,最后三个已被选中。当你点击一个按钮时,只有那个按钮被选中,其他的按钮被取消选中。我希望它的原始版本的形式没有他们选择 self.radio_button_frame = Frame(self) self.radio_button_frame.grid(row=1, column=1, rowspan=4) sel

我正在尝试创建一个包含四个选项的问题选择菜单。我正在实例化四个单选按钮,每个选项一个。由于某种原因,当表单显示时,最后三个已被选中。当你点击一个按钮时,只有那个按钮被选中,其他的按钮被取消选中。我希望它的原始版本的形式没有他们选择

            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)

您需要给Radiobutton的变量应该是Tkinters
IntVar()
。 因此,不是:

self.answer = 0
付诸表决:


要获取
self.answer
的值,只需调用
self.answer.get()。
因此,不是:

self.answer = 0
付诸表决:

要获取
self.answer
的值,只需调用
self.answer.get()