如何在 tkinter 中强制选择单选按钮 python
How to make radio buttons mandatory for selection in tkinter python
我正在做一个小项目来创建一个小的 window,它将提出 3 个问题和 2 个选项,因为单选按钮将可用于回答。 selecting 答案后,它将打印用户详细信息以及稍后我将添加到数据库中的答案。截至目前,它与 selections 一起工作正常,但如果没有按钮被 selected 并单击提交按钮,它仍然会选择第二个选项并允许输入。在这里我想添加一个检查,如果没有按钮被 selected 那么应该出现一个弹出窗口并且应该询问 select 按钮。我在谷歌上搜索了很多关于强制使用单选按钮的内容,但没有找到任何合适的答案。有人可以帮忙吗?下面是我的代码
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()
您可以使用 if 语句查看它是否为空然后执行您想要的操作,您可以更改标签上的文本以告诉用户。然后做
root.after(1000, lambda: lab2.config(text=somethingsomething))
返回原始文本...好吧单选按钮只有 2 个选项,开和关,所以如果用户不触摸它,第二个选项总是被选中。
我正在做一个小项目来创建一个小的 window,它将提出 3 个问题和 2 个选项,因为单选按钮将可用于回答。 selecting 答案后,它将打印用户详细信息以及稍后我将添加到数据库中的答案。截至目前,它与 selections 一起工作正常,但如果没有按钮被 selected 并单击提交按钮,它仍然会选择第二个选项并允许输入。在这里我想添加一个检查,如果没有按钮被 selected 那么应该出现一个弹出窗口并且应该询问 select 按钮。我在谷歌上搜索了很多关于强制使用单选按钮的内容,但没有找到任何合适的答案。有人可以帮忙吗?下面是我的代码
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()
您可以使用 if 语句查看它是否为空然后执行您想要的操作,您可以更改标签上的文本以告诉用户。然后做
root.after(1000, lambda: lab2.config(text=somethingsomething))
返回原始文本...好吧单选按钮只有 2 个选项,开和关,所以如果用户不触摸它,第二个选项总是被选中。