mercredi 2 mars 2022

Python Checkboxes with tkinter and If Condition

I would like to create some kind of prio generator. This is kept very simple. I have 8 different questions and depending on the answer a prioritization is output. This works so far also quite well. However, I have the error that my "Else" condition does not seem to work. Example: If question 1 and question 8 are selected, then the Else condition should output "not possible". Unfortunately this does not work. I have defined the possibilities and everything that is outside the defined rules should run into the else condition. Here is the code:

    from tkinter import *

ws = Tk()
ws.title('Prio Guide')
ws.geometry('600x680')
ws.configure(bg="white")

text = StringVar()
label = Label(  ws,
                text='empty',
                textvariable=text, 
                bg="green")
prio1 = StringVar()
prio1_text = Label( ws,text='empty',textvariable=prio1, bg="white")
prio1.set("Prio 1 - Description")

prio2 = StringVar()
prio2_text = Label( ws, text='empty', textvariable=prio2, bg="white")
prio2.set("Prio 2- Description")

prio3 = StringVar()
prio3_text = Label( ws, text='empty', textvariable=prio3, bg="white")
prio3.set("Prio 3- Description")

prio4 = StringVar()
prio4_text = Label( ws, text='empty', textvariable=prio4, bg="white")
prio4.set("Prio 4- Description")
#define the Prio
def prio():
    if cb.get() == 1 or cb1.get() == 1 or cb2.get() == 1 or cb.get() & cb1.get() == 1 or cb2.get() & cb1.get() == 1 or cb.get() & cb1.get() & cb2.get() == 1:
        text.set("Prio 1")

    elif cb3.get() == 1 or cb4.get() == 1 or cb3.get() & cb4.get() == 1:
        text.set("Prio 2")

    elif cb5.get() == 1 or cb5.get() & cb6.get() == 1:
        text.set("Prio 3")
    elif cb7.get() == 1 or cb6.get() & cb7.get() == 1:
        text.set("Prio 4")
    else:
        text.set("not possible")

#create IntVar fot the value of checkboxes
cb  = IntVar()
cb1 = IntVar()
cb2 = IntVar()
cb3 = IntVar()
cb4 = IntVar()
cb5 = IntVar()
cb6 = IntVar()
cb7 = IntVar()
#create checkbutton
Checkbutton(ws, 
            bg="white", 
            text="q1", 
            variable=cb, 
            onvalue=1,
            offvalue=0,
            command=prio,).pack(anchor=W,padx=(100, 10))
Checkbutton(ws,
            bg="white", 
            text="q2", 
            variable=cb1, 
            onvalue=1, 
            offvalue=0, 
            command=prio).pack(anchor=W,padx=(100, 10))
Checkbutton(ws, 
            bg="white", 
            text="q3", 
            variable=cb2, 
            onvalue=1, 
            offvalue=0, 
            command=prio).pack(anchor=W,padx=(100, 10))
Checkbutton(ws,
            bg="white", 
            text="q4", 
            variable=cb3, 
            onvalue=1, 
            offvalue=0, 
            command=prio).pack(anchor=W,padx=(100, 10))
Checkbutton(ws,
            bg="white", 
            text="q5", 
            variable=cb4, 
            onvalue=1, 
            offvalue=0, 
            command=prio).pack(anchor=W,padx=(100, 10))
Checkbutton(ws,
            bg="white", 
            text="q6", 
            variable=cb5, 
            onvalue=1, 
            offvalue=0, 
            command=prio).pack(anchor=W,padx=(100, 10))
Checkbutton(ws,
            bg="white", 
            text="q7", 
            variable=cb6, 
            onvalue=1, 
            offvalue=0, 
            command=prio).pack(anchor=W,padx=(100, 10))
Checkbutton(ws,
            bg="white", 
            text="q8", 
            variable=cb7, 
            onvalue=1, 
            offvalue=0, 
            command=prio).pack(anchor=W,padx=(100, 10))


label.pack(anchor=S)
prio1_text.pack(anchor=S,padx=(100, 10))
prio2_text.pack(anchor=S,padx=(100, 10))
prio3_text.pack(anchor=S,padx=(100, 10))
prio4_text.pack(anchor=S,padx=(100, 10))
ws.mainloop()

Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire