如何确保选中单选按钮

时间:2019-06-02 13:33:17

标签: python python-2.7 tkinter radio-button

我正在做一个实验,向参与者展示刺激物,并在每次刺激后问他们一些问题。我有四个问题,每个问题在单独的框架中。每个问题的答案是选择七个单选按钮之一。我在单独的框架中也有一个按钮,用于保存选择的值并显示下一个刺激。这里的问题是,如果参与者忘记选择单选按钮之一,则该按钮的值将另存为0,我想避免这种情况。我试图创造一个条件,仅在所有单选按钮都被选中且失败的情况下才启用下一个刺激按钮。有办法吗?

import Tkinter as tk
import PIL.Image 
import PIL.ImageTk
import tkMessageBox
from Tkinter import *

def present(tacton):
    qual_frame = tk.LabelFrame(group, padx=30, pady=30)
    qual_frame.grid(row=0, column=1)


    urgency_frame = tk.LabelFrame(qual_frame, text="How Urgent was the 
    stimulus?", padx=15, pady=30, font = "Verdana 14")
    urgency_frame.grid(row=1, column=0)
    urgency  = tk.IntVar()
    Radiobutton(master=urgency_frame, text="1 (Not at all)          ", 
    variable=urgency , value=1, font = "Verdana 12").grid(row=0, column=0)
    Radiobutton(master=urgency_frame, text="2                       ", 
    variable=urgency , value=2, font = "Verdana 12").grid(row=0, column=1)
    Radiobutton(master=urgency_frame, text="3                       ", 
    variable=urgency , value=3, font = "Verdana 12").grid(row=0, column=2)
    Radiobutton(master=urgency_frame, text="4 (Neutral)             ", 
    variable=urgency , value=4, font = "Verdana 12").grid(row=0, column=3)
    Radiobutton(master=urgency_frame, text="5                       ", 
    variable=urgency , value=5, font = "Verdana 12").grid(row=0, column=4)
    Radiobutton(master=urgency_frame, text="6                       ", 
    variable=urgency , value=6, font = "Verdana 12").grid(row=0, column=5)
    Radiobutton(master=urgency_frame, text="7 (Very much)", variable=urgency , 
    value=7, font = "Verdana 12").grid(row=0, column=6) 

    annoyance_frame = tk.LabelFrame(qual_frame, text="How Annoying was the 
    stimulus?", padx=15, pady=30, font = "Verdana 14")
    annoyance_frame.grid(row=5, column=0, sticky="nsew")
    annoyance = tk.IntVar()
    Radiobutton(master=annoyance_frame, text="1 (Not at all)          ", 
    variable=annoyance, value=1, font = "Verdana 12").grid(row=0, column=0)
    Radiobutton(master=annoyance_frame, text="2                       ", 
    variable=annoyance, value=2, font = "Verdana 12").grid(row=0, column=1)
    Radiobutton(master=annoyance_frame, text="3                       ", 
    variable=annoyance, value=3, font = "Verdana 12").grid(row=0, column=2)
    Radiobutton(master=annoyance_frame, text="4 (Neutral)             ", 
    variable=annoyance, value=4, font = "Verdana 12").grid(row=0, column=3)
    Radiobutton(master=annoyance_frame, text="5                       ", 
    variable=annoyance, value=5, font = "Verdana 12").grid(row=0, column=4)
    Radiobutton(master=annoyance_frame, text="6                       ", 
    variable=annoyance, value=6, font = "Verdana 12").grid(row=0, column=5)
    Radiobutton(master=annoyance_frame, text="7 (Very much)",  
    variable=annoyance, value=7, font = "Verdana 12").grid(row=0, column=6) 


    valence_frame = tk.LabelFrame(qual_frame, text = 'How Pleasant was the 
    stimulus?', padx = 15, pady = 30, font = "Verdana 14")
    valence_frame.grid(row=10, column=0, sticky="nsew")
    valence = tk.IntVar()
    Radiobutton(master=valence_frame, text="1 (Not at all)          ", 
    variable=valence, value=1, font = "Verdana 12").grid(row=0, column=0)
    Radiobutton(master=valence_frame, text="2                       ", 
    variable=valence, value=2, font = "Verdana 12").grid(row=0, column=1)
    Radiobutton(master=valence_frame, text="3                       ", 
    variable=valence, value=3, font = "Verdana 12").grid(row=0, column=2)
    Radiobutton(master=valence_frame, text="4 (Neutral)             ", 
    variable=valence, value=4, font = "Verdana 12").grid(row=0, column=3)
    Radiobutton(master=valence_frame, text="5                       ", 
    variable=valence, value=5, font = "Verdana 12").grid(row=0, column=4)
    Radiobutton(master=valence_frame, text="6                       ", 
    variable=valence, value=6, font = "Verdana 12").grid(row=0, column=5)
    Radiobutton(master=valence_frame, text="7 (Very much)",  variable=valence, 
    value=7, font = "Verdana 12").grid(row=0, column=6) 


    arousal_frame = tk.LabelFrame(qual_frame, text = 'How Exciting was the 
    stimulus?', padx = 15, pady = 30, font = "Verdana 14")
    arousal_frame.grid(row=15, column=0, sticky="nsew")
    arousal = tk.IntVar()
    Radiobutton(master=arousal_frame, text="1 (Not at all)          ", 
    variable=arousal, value=1, font = "Verdana 12").grid(row=0, column=0)
    Radiobutton(master=arousal_frame, text="2                       ", 
    variable=arousal, value=2, font = "Verdana 12").grid(row=0, column=1)
    Radiobutton(master=arousal_frame, text="3                       ", 
    variable=arousal, value=3, font = "Verdana 12").grid(row=0, column=2)
    Radiobutton(master=arousal_frame, text="4 (Neutral)             ", 
    variable=arousal, value=4, font = "Verdana 12").grid(row=0, column=3)
    Radiobutton(master=arousal_frame, text="5                       ", 
    variable=arousal, value=5, font = "Verdana 12").grid(row=0, column=4)
    Radiobutton(master=arousal_frame, text="6                       ", 
    variable=arousal, value=6, font = "Verdana 12").grid(row=0, column=5)
    Radiobutton(master=arousal_frame, text="7 (Very much)",  variable=arousal, 
    value=7, font = "Verdana 12").grid(row=0, column=6) 



    next_frame = tk.LabelFrame(group, padx=20, pady=20)
    next_frame.grid(row=6, column=1)

    Next_Stimulus = tk.Button(master=next_frame, text="Next Stimulus",  
    bg="green", padx=20, pady=20, width=20, font = "Verdana 12")
    Next_Stimulus.pack()



    root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以让按钮始终处于活动状态,并在按下按钮时检查条件,只有在所有变量均已设置的情况下,才转到下一个刺激:

if all([urgency.get(), annoyance.get()] ... etc):
    # go to next stimulus

或者您可以仅在全部设置后跟踪IntVars中的更改并启用按钮。为每个IntVar设置一个跟踪:

urgency.trace('w', enable_button)

然后在回调函数中检查IntVar状态:

def enable_button(*args):
    if all([urgency.get(), annoyance.get()] ... etc):
        Next_Stimulus.config(state=tk.NORMAL)