如何检查在python上选择了哪个单选按钮

2024-05-17 13:19:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个基本的抽认卡应用程序,决定给某人一个正确或错误的选项来回答这个问题。我正在尝试使用tkinter中的单选按钮小部件,但我需要帮助检测选择了哪个按钮。(平衡列表具有要从中提取的文件名。单选按钮的平衡和不平衡表示正确或错误)

def random_balancing():

    #global balancing list creates list to pull from
    global balancing_list
    balancing_list = ['unbalanced1', 'unbalanced2', 'balanced1', 'balanced2']

    #globalizes the random number from element_list
    global rando
    rando = randint(0, 3)

    #globalizes the elements list
    global balancing1_list
    balancing_list1 = balancing_list[rando]

    #create element models
    global balancing1image
    balancing1_image = ImageTk.PhotoImage(Image.open('C:/Users/Kisitu/Desktop/project/balancing/' + balancing_list1 + '.png'))
    show_balancing1.config(image=balancing1_image)


def balancing():
    intro.pack_forget()
    hide_all_frames()
    balancing_frame.pack(fill="both", expand=1)

    global show_balancing1
    show_balancing1 = Label(balancing_frame)
    show_balancing1.pack(pady=15)

    #button to randomize state image
    rando_button3 = Button(balancing_frame, text="I Dont Know", command=balancing)
    rando_button3.pack(pady=10)

    global radio
    balanced = Radiobutton(balancing_frame,text="balanced", font=("Helvetica", 18))
    balanced.pack(pady=15)

    unbalanced = Radiobutton(balancing_frame,text="unbalanced", font=("Helvetica", 18))
    unbalanced.pack(pady=17)

    #answer button
    answer_button3 = Button(balancing_frame, text="answer", command=balancing_answer)
    answer_button3.pack(pady=5)

    #response label
    global answer_label3
    answer_label3 = Label(balancing_frame, text="", font=("Helvetica",18), bg='#B3FDFF')
    answer_label3.pack(pady=15)
    random_balancing()


def balancing_answer():

我不知道我应该在平衡答案中输入什么来检测按下了哪个按钮。提前感谢您的帮助


Tags: textanswerimagedefshow按钮globalframe
1条回答
网友
1楼 · 发布于 2024-05-17 13:19:25

这有用吗

def sel(): #Command called whenever state of radiobutton is modified
    print(var.get)

var = StringVar() #Stringvariable to be slaved to both buttons
R1 = Radiobutton(root, text="Option 1", variable=var, value="Button1 selected", command=sel) #variable, value to set variable when selected and callback all defined.
R2 = Radiobutton(root, text="Option 2", variable=var, value="Button1 selected", command=sel)

将启动字符串变量并将其设置为保存复选框的值。 无论何时修改按钮的状态,都会调用sel()并打印出值

如果这些都不合理,请告诉我

相关问题 更多 >