mercredi 29 avril 2020

How to print respective checkbox name when they are checked in tkinter?

I want to print respective checkbox names in console when a checkbox is checked.

For example, when I check 'COPYRIGHT' checkbox:

What I want to be printed: 'copyright'

What is printed: 'creation'

I found that when I replace creation with other elements of metadata_list, that newly replaced element is always printed out. In general, last element of the metadata_list is always printed by the temp() function no matter which checkbox I check. I think there is some hulululu going on when I pass 'key' as command in the function.

But correct key value is displayed when key is printed from outside the temp() function like there is inside for loop. What am I doing wrong here?

This is main file:

import tkinter as tk
import variable_container as vc

metadata_frame = tk.Tk()

def temp(key):
    print(key)

for key,i in vc.metadata_list.items():
    i[1] = tk.IntVar()
    tk.Checkbutton(metadata_frame, text = key.upper(), variable = i[1], command = lambda: temp(key)).grid(row = i[0])
    print(key)

metadata_frame.mainloop()

And this is variable_container.py file:

# Values are the row information and respective checkbox variables
metadata_list = {
    'copyright': [0, var_copyright],
    'location': [1, var_location],
    'creation': [2,var_creation]
}




Aucun commentaire:

Enregistrer un commentaire