简体   繁体   English

如何在 tkinter python 中强制选择单选按钮

[英]How to make radio buttons mandatory for selection in tkinter python

I am working on a small project to create a small window which will ask 3 questions and 2 options as radio buttons will be available for answers.我正在做一个小项目来创建一个小的 window 它将提出 3 个问题和 2 个选项,因为单选按钮将可用于答案。 After selecting the answers it will print the users details along with the answers which later i will add into a database.选择答案后,它将打印用户详细信息以及稍后我将添加到数据库中的答案。 As of now it is working fine with selections, but if no button is selected and clicked on submit button it still picks the 2nd option and allows to enter.到目前为止,它与选择工作正常,但如果没有选择任何按钮并单击提交按钮,它仍然会选择第二个选项并允许输入。 Here i want to add a check if no button is selected then a pop up should appear and should ask to select the buttons.在这里,我想添加一个检查,如果没有选择任何按钮,那么应该会出现一个弹出窗口,并且应该询问 select 按钮。 I have googled a lot on making radio buttons mandatory but didn't find any appropriate answer.我在谷歌上搜索了很多关于强制单选按钮的信息,但没有找到任何合适的答案。 Can some one please help.有人可以帮忙吗? Below is my code下面是我的代码

from tkinter import *
import tkinter.messagebox
import getpass
import socket
from datetime import datetime

root=Tk()
root.title("Survey")
root.geometry("225x225")
root.config(bg="antique white")
root.resizable(width="false", height="false")


day_selected = IntVar()
mood_selected = IntVar()
work_selected = IntVar()

# creating values for entry

staffid = getpass.getuser()
machine = socket.gethostname()
now = datetime.now()
date_str = now.strftime("%d/%m/%Y %H:%M:%S")
day_sel = day_selected.get()

def done():
    print(staffid)
    print(machine)
    print(date_str)
    print(day_sel)
    print("Good" if day_selected.get()==1 else "BAD")
    print("Good" if mood_selected.get()==3 else "BAD")
    print("Good" if work_selected.get()==5 else "BAD")
    tkinter.messagebox.showinfo("Thank you", "Thank you for completing the survey !!!")

lab1 = Label(root, text="  How Was ?", font=("calibri", 20, "bold"), bg="antique white", fg="brown").pack()

lab2 = Label(root, text="Day", font=("calibri", 14, "bold"),bg="antique white")
lab2.place(x=8, y=50)
r1=Radiobutton(root,text="Good", font=("calibri",12), variable=day_selected, value=1,bg="antique white").place(x=110,y=52)
r2=Radiobutton(root,text="Bad",font=("calibri",12), variable=day_selected, value=2,bg="antique white").place(x=170,y=52)
lab3 = Label(root, text="Mood", font=("calibri", 14, "bold"),bg="antique white")
lab3.place(x=8, y=100)
r3=Radiobutton(root,text="Good", font=("calibri",12), variable=mood_selected, value=3,bg="antique white").place(x=110,y=102)
r4=Radiobutton(root,text="Bad",font=("calibri",12), variable=mood_selected, value=4,bg="antique white").place(x=170,y=102)
lab4 = Label(root, text="Work", font=("calibri", 14, "bold"),bg="antique white")
lab4.place(x=8, y=150)
r5=Radiobutton(root,text="Good", font=("calibri",12), variable=work_selected, value=5,bg="antique white").place(x=110,y=152)
r6=Radiobutton(root,text="Bad",font=("calibri",12), variable=work_selected, value=6,bg="antique white").place(x=170,y=152)

sub_bt=Button(root, text="Submit",font=("calibri",12, "bold"), command=done, bg="brown", fg="white").pack(side=BOTTOM)

root.mainloop()

you can use an if statement to see if it is empty then do what you want, you can change the text on labels to tell the user.您可以使用 if 语句来查看它是否为空,然后执行您想要的操作,您可以更改标签上的文本以告诉用户。 then do然后做

root.after(1000, lambda: lab2.config(text=somethingsomething))

to go back to original text... well radio buttons only have 2 options, on and off, so 2nd option is always selected if the user does not touch it.到 go 返回原始文本...单选按钮只有 2 个选项,打开和关闭,因此如果用户不触摸它,则始终选择第二个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM