Home > database >  Python Checkboxes with tkinter and If Condition
Python Checkboxes with tkinter and If Condition

Time:03-02

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!

CodePudding user response:

You have a problem with your "or", whenever you have one of the or condition is equal with true, it will not return your else condition.

I would like to suggest a tricky solution, which is using a pattern to get the priority.

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 getCbValue():
    return "".join(str(x) for x in [cb.get(), cb1.get(), cb2.get(), cb3.get(), cb4.get(), cb5.get(), cb6.get(), cb7.get()])


def getPrio(cbValue):
    prioConf = {'Prio 1': ['10000000', '01000000', '00100000', '11000000', '01100000'],
                'Prio 2': ['00010000', '00001000', '00011000'],
                'Prio 3': ['00000100', '00000110'],
                'Prio 4': ['00000001', '00000011']}
    key_list = list(prioConf.keys())
    value_list = list(prioConf.values())
    for i, d in enumerate(value_list):
        if cbValue in d:
            return key_list[i]


def prio():
    prio = getPrio(getCbValue())
    if prio:
        text.set(prio)
    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()

You can check there are 2 new methods "getCbValue" and "getPrio".

I also refactor your if-else, so whenever the pattern match it will return the priority if not it will return the error message "not possible'.

  • Related