为什么在运行代码时所有单选按钮都已经被预选?我尝试取消选择,但无法正常工作。请

时间:2019-06-13 04:38:25

标签: python-3.x tkinter radio-button

为什么在运行代码时所有单选按钮都已预先选择?我尝试取消选择,但无法正常工作。同样,没有选择第一个单选按钮,但是当我将鼠标悬停在它上面时,它被选中。 请帮忙。更新了代码,并提供了更多模块供参考。我希望选择单选按钮,并且单击“提交”后,我想知道答案是否正确。但是当我使用self.var.get()时,总是得到0作为其值,这使我无法帮助检查我的答案。

class Question:
    def __init__(self, question, answers, correctLetter):
        self.question = question
        self.answers = answers
        self.correctLetter = correctLetter



    def check(self, letter, view):
        print(self.var2.get())
        global right
        if(letter == self.correctLetter):
            label = Label(view, text="RIGHT!",bg='black',fg='green',font=("Calibri 11 bold"))
            right += 1
        else:
            label = Label(view, text="WRONG!",bg='black',fg='red',font=("Calibri 11 bold"))
##        label.pack()
        label.grid(row=10, column=2)
        view.after(1000, lambda *args: self.unpackView(view))


    def getView(self, window):

        view = Frame(window)

        self.var=IntVar()
        self.var1=IntVar()
        self.var2=IntVar()
        self.var3=IntVar()



        Label(view, text="Question: ").grid(sticky = W)
        Label(view, text=self.question).grid(sticky = E)


        r1=Checkbutton(view,text=self.answers[0],variable=self.var,onvalue = 1, offvalue = 0,command=lambda *args: self.check("A", view))
        r1.grid(sticky=W)

        Label(screen,text="").pack()


        r2=Checkbutton(view,text=self.answers[1],variable=self.var1,onvalue = 1, offvalue = 0,command=lambda *args: self.check("B", view))
        r2.grid(sticky=W)

        Label(screen,text="").pack()

        r3=Checkbutton(view,text=self.answers[2],variable=self.var2,onvalue = 1, offvalue = 0,command=lambda *args: self.check("C", view))
        r3.grid(sticky=W)

        Label(screen,text="").pack()

        r4=Checkbutton(view,text=self.answers[3],variable=self.var3,onvalue = 1, offvalue = 0,command=lambda *args: self.check("D", view))
        r4.grid(sticky=W)

        Label(screen,text="").pack()



        #tk.Radiobutton(view,text=self.answers[0],bg='white',font=('calibri',11),command=lambda *args: self.check("A", view),height=1,width=10).grid(row=5, column=2)
        #tk.Radiobutton(view,text=self.answers[0],bg='white',font=('calibri',11),command=lambda *args: self.check("B", view),height=1,width=10).grid(row=6, column=2)
        #tk.Radiobutton(view,text=self.answers[2],bg='white',font=('calibri',11),command=lambda *args: self.check("C", view),height=1,width=10).grid(row=7, column=2)
        #tk.Radiobutton(view,text=self.answers[3],bg='white',font=('calibri',11),command=lambda *args: self.check("D", view),height=1,width=10).grid(row=8, column=2)
        #Button(view,text='SUBMIT',fg='black',bg='white',font=('calibri',11),command=lambda *args: self.check(view),height=1,width=10).grid(sticky=SE)
        return view

    def unpackView(self, view):

        view.pack_forget()
        askQuestion()


def askQuestion():

    global questions, window, index, button, right, number_of_questions

    if(len(questions) == index + 1):
        Label(window, text="Thank you for answering the questions. " + str(right) + " of " + str(number_of_questions) + " questions answered right").place(x=40,y=400)
        return
    button.pack_forget()
    index += 1
    questions[index].getView(window).pack(padx=100,pady=100)

2 个答案:

答案 0 :(得分:0)

您需要使用一个默认值初始化IntVar,如果您不想默认选择任何值,则该值不是选项。

self.var=IntVar(value=1) # select the first Radiobutton on boot
self.var=IntVar(value=99) # no Radiobutton is selected on boot

答案 1 :(得分:0)

组中的所有单选按钮需要共享一个tkinter变量(StringVarIntVar等)的单个实例。每个单选按钮还需要一个唯一的值。当您执行此操作并将变量设置为值之一时,只会选择具有该值的单选按钮。

在您的代码中,没有为任何单选按钮提供值,因此已设置默认值0(零)。由于它们都具有相同的值,因此它们都已设置。

示例:

import tkinter as tk

root = tk.Tk()

var = tk.IntVar(value=0)
r0 = tk.Radiobutton(root, text="Zero", value=0, variable=var)
r1 = tk.Radiobutton(root, text="One", value=1, variable=var)
r2 = tk.Radiobutton(root, text="Two", value=2, variable=var)
r3 = tk.Radiobutton(root, text="Three", value=3, variable=var)

r0.pack(side="top", anchor="w")
r1.pack(side="top", anchor="w")
r2.pack(side="top", anchor="w")
r3.pack(side="top", anchor="w")

tk.mainloop()