mercredi 16 août 2017

How to force user to select at least one check button

I have a program I am working on and want the user to select their interests, and when they are done press submit. How would I only allow the user to press submit if at least one button is checked.

from tkinter import *

check = Tk()
check.title("Interests")
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()


C1 = Checkbutton(check, text = "Horror", variable = CheckVar1, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C2 = Checkbutton(check, text = "Action", variable = CheckVar2, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C3 = Checkbutton(check, text = "Documentary", variable = CheckVar3, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C4 = Checkbutton(check, text = "Science fiction", variable = CheckVar4, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C5 = Checkbutton(check, text = "Comedy", variable = CheckVar5, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)

submit_btn = Button(check, text = "Submit", command = lambda: check.destroy())          #done button on the drop down menu window


C1.pack()
C2.pack()
C3.pack()
C4.pack()
C5.pack()
submit_btn.pack()
check.mainloop()

if CheckVar1.get():
    #dosomething
if CheckVar2.get():
    #dosomething
if CheckVar3.get():
    #dosomething
if CheckVar4.get():
    #dosomething
if CheckVar5.get():
    #dosomething




Aucun commentaire:

Enregistrer un commentaire