2017-09-23 80 views
-1

我目前正在研究A-Level计算机科学课程,并且直接出门,我遇到了一些问题。计划它有一个与其他多种功能的测验,但是我不能在开发过程中继续进行下去,直到我能够得到问题的工作,他们将被单选按钮的使用回答,但是当我尝试检索已选定(即使它是正确的)单选框我得到的价值是PY_VAR0而非实际值无法从单选按钮检索价值

def gettingDecision(): 
    var.get() 
    if var == 'True': 
     messagebox.showinfo('Congrats', message='You Are Correct') 
    else: 
     messagebox.showinfo('Lose', message='You Are Wrong.') 

def ques1(): 
    root = Tk() 
    window = Toplevel(root) 
    Question1 = Label(window, text='Q.1.What data type is a whole number?') 
    Question1.grid(row=1, column=0) 

    Q1A = Radiobutton(window, text='[A] Str', variable=var, value='False1') 
    Q1A.grid(row=2, column=0) 

    Q1B = Radiobutton(window, text='[B] Float', variable=var, value='False2') 
    Q1B.grid(row=3, column=0) 

    Q1C = Radiobutton(window, text='[C] Int', variable=var, value='True') 
    Q1C.grid(row=4, column=0) 

    Q1D = Radiobutton(window, text='[D] Char', variable=var, value='False3') 
    Q1D.grid(row=5, column=0) 

    submit = Button(window, text='Submit', command=gettingDecision) 
    submit.grid() 

我知道,要解决这个问题可能很简单,但我只是不能得到它出于某种原因,我也看过这里的帖子寻求解决方案,但我无法找到解决办法。对不起,造成不便,但我真的需要知道如何获得价值以取得任何进展。 (我会改变我的代码是如何在稍后使用类的结构,但现在我只需要这个工作)。

+1

您是否尝试过寻找这个网站'PY_VAR0'? –

回答

1

几件事情:所述Tutorials Point documentation for RadioButton说:

如果控制变量是一个IntVar,得到 组中的每个单选按钮不同的整数值

其次,这种不能工作:

var.get() 
if var == 'True': 

你真的想:

if var.get() == ... 

返工,和简化,你的榜样的:

from tkinter import * 
from tkinter import messagebox # has to be explicitly imported 

def checkDecision(): 
    if var.get() == answer: 
     messagebox.showinfo('Congrats', message='You Are Correct') 
    else: 
     messagebox.showinfo('Incorrect', message='You Are Wrong.') 

def question_1(window): 

    Label(window, text='Q.1.What data type is a whole number?').pack() 

    Radiobutton(window, text='[A] Str', variable=var, value=1).pack(anchor=W) 
    Radiobutton(window, text='[B] Float', variable=var, value=2).pack(anchor=W) 
    Radiobutton(window, text='[C] Int', variable=var, value=3).pack(anchor=W) 
    Radiobutton(window, text='[D] Char', variable=var, value=4).pack(anchor=W) 

    Button(window, text='Submit', command=checkDecision).pack() 

    return 3 

root = Tk() 

var = IntVar() 

answer = question_1(root) 

mainloop() 

enter image description here