Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When you press the Checkbutton, i want the name to be printed out

When you press the Checkbutton, i want the name to be printed out.

Please help me find a solution of the problem, thanks.

from tkinter import *


def on_click():

    lst = [interests[i] for i, chk in enumerate(chks) if chk.get()]
    print(lst)
    print(",".join(lst))

def check():
    print()
    pass
interests = ['Music', 'Book', 'Movie', 'Photography', 'Game', 'Travel']
root = Tk()
root.option_add("*Font", "impact 30")
chks = [BooleanVar() for i in interests]

Label(root, text="Your interests", bg="gold").pack()
for i, s in enumerate(interests):
    Checkbutton(root, text=s, variable=chks[i] , command=check).pack(anchor=W)  # W = West

Button(root, text="submit", command=on_click).pack()
root.mainloop()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Like this:

from tkinter import *

def on_click():
    lst = [interests[i] for i, chk in enumerate(chks) if chk.get()]
    print(lst)
    print(",".join(lst))

def check(s):
    print(s)

interests = ['Music', 'Book', 'Movie', 'Photography', 'Game', 'Travel']
root = Tk()
root.option_add("*Font", "impact 30")
chks = [BooleanVar() for i in interests]

Label(root, text="Your interests", bg="gold").pack()
for i, s in enumerate(interests):
    Checkbutton(root, text=s, variable=chks[i] , command=lambda s=s: check(s)).pack(anchor=W)  # W = West

Button(root, text="submit", command=on_click).pack()
root.mainloop()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading