TKinter问题与获得单选按钮值

时间:2018-08-28 22:58:52

标签: python tkinter

我定义了一个函数,其中包含4个单选按钮,其中包含对随机询问的答案;另外,我还有一个功能,用于检查所选单选按钮是否具有正确答案,如果是,则将变量“分数”加1。 score变量无法正确递增,并且似乎是随机更新:

def answers():
    global text, rb1, rb2, rb3, rb4, v

    v = int() 

    rb1 = Radiobutton(root, text="One", variable=v, value=1, bg = "white", font = ("Segoe UI", 11))
    rb1.place(x=20, y=80)

    rb2 = Radiobutton(root, text="Two", variable=v, value=2, bg = "white", font = ("Segoe UI", 11))
    rb2.place(x=20, y=120)

    rb3 = Radiobutton(root, text="Three", variable=v, value=3, bg = "white", font = ("Segoe UI", 11))
    rb3.place(x=20, y=160)

    rb4 = Radiobutton(root, text="Four", variable=v, value=4, bg = "white", font = ("Segoe UI", 11))
    rb4.place(x=20, y=200)

    next() 





def next():   
    val=(int(v))

    if question_variable == "Describe what the instruction 'BRP' does.":
        rb1['text'] = "If the contents of the accumulator are zero or positive, set the program counter to address xx."
        rb2['text'] = "If the contents of the accumulator are zero only, set the program counter to address xx."
        rb3['text'] = "If the contents of the accumulator are positive only, set the program counter to address xx."
        rb4['text'] = "If the accumulator is empty, set the program counter to address xx."
        if val == 1: 
            score += 1


    elif question_variable == "Describe what is meant by the term 'Open Source Software'.":
        rb1['text'] = "Open source software is software that is paid-for only."
        rb2['text'] = "Open source software is software that is free."
        rb3['text'] = "Open source software is software with source code that anyone can inspect, modify, and enhance."
        rb4['text'] = "Open source software is copyrighted software."
        if val == 3:
            score += 1

    elif question_variable == "What is meant by the term 'Lossy Compression'?":
        rb1['text'] = "Lossy compression is a compression method that removes metadata from the file, resulting in smaller file sizes."
        rb2['text'] = "Lossy compression is a compression method that works by removing redundant and unrequired data."
        rb3['text'] = "Lossy compression is a compression method which reduces the file size without any noticeable quality loss."
        rb4['text'] = "Lossy compression is a risky compression method which could result in data corruption if used incorrectly."     
        if val == 2:
            score += 1

    elif question_variable == "What is the number '55' as an 8-bit unsigned binary integer?":
        rb1['text'] = "00110111"
        rb2['text'] = "11101100"
        rb3['text'] = "11001011"
        rb4['text'] = "00110100"
        if val == 1:
            score += 1

    elif question_variable == "What might a printer use RAM for?":
        rb1['text'] = "To store its OS."
        rb2['text'] = "To store most frequently printed pages."
        rb3['text'] = "To hold the current and subsequent jobs."
        rb4['text'] = "To take RAM load off the main computer."
        if val == 3:
            score += 1

    elif question_variable == "Describe the term 'firewall'.":
        rb1['text'] = "A firewall prevents harmful software from accessing a computer."
        rb2['text'] = "A firewall redirects data packets via other nodes before reach its designated computer."
        rb3['text'] = "A firewall limits the speed that the packets are able to travel at."
        rb4['text'] = "A firewall enforces a set of rules about what data packets will be allowed to enter or leave a network."
        if val == 4:
            score += 1

    else:
        rb1['text'] = "Memory Address Register"
        rb2['text'] = "Arithmetic Logic Unit"
        rb3['text'] = "Memory Data Register"
        rb4['text'] = "Accumulator"
        if val == 4:
            score += 1

有人可以发现我要去哪里了吗?

2 个答案:

答案 0 :(得分:1)

由于v是全局变量,因此您不必在决定按下哪个按钮的if语句中将其表示为另一个变量。另外,v已经是整数,因此也不必在int()中使用val函数

答案 1 :(得分:1)

variable参数不能使用普通的python变量。由于您使用的是整数值,因此它必须是IntVar的实例。要获取该值,您需要在该实例上调用get()方法。