dimanche 4 juillet 2021

How to present user check boxes and then save a list to the checked ones?

enter image description here

How to do this without writing separate function for each check box as i have done in the code commented out, what is wrong with the "func" function why doesn't it work?

from tkinter import *

ws = Tk()
ws.title('Terminator')
ws.geometry('200x80')


# def vlc_func():
#     if vlc.get() == 1:
#         print("selected")
#     elif vlc.get() == 0:
#         print("deselected")

# def notepad_func():
#     if notepad.get() == 1:
#         print("selected")
#     elif notepad.get() == 0:
#         print("deselected")

def func(associated_variable):
    if associated_variable.get() == 1:
        print("selected")
    elif associated_variable.get() == 0:
        print("deselected")


vlc = IntVar()
vlc.set(0)
Checkbutton(ws, text= "vlc.exe", variable=vlc, onvalue=1, offvalue=0, command= func(vlc)).pack()

notepad = IntVar()
Checkbutton(ws, text= "notepad.exe", variable=notepad, onvalue=1, offvalue=0, command= func(notepad)).pack()




ws.mainloop()

    



Aucun commentaire:

Enregistrer un commentaire