I am working with Tkinter and I am trying to use some checkbuttons.
Last time I wrote about them, our colleagues helped me in this question.
Here's what I am doing:
- get a list with some ingredients
- get a tkinter GUI with some checkbuttons (one for each ingredient)
- select some of the checkbuttons (tick them)
- press a button and obtain a list containing the ingredients I selected
What I am trying to do now is the following:
- make the checkbuttons unusable (so, disable them) after the "confirmation" button is pressed.
My code is basically the same as the accepted answer of my other question. I report it below:
import tkinter as tk
root = tk.Tk()
INGREDIENTS = ['cheese', 'ham', 'pickle', 'mustard', 'lettuce']
txt = tk.Text(root, width=40, height=20)
variables = []
for i in INGREDIENTS:
variables.append( tk.IntVar( value = 0 ) )
cb = tk.Checkbutton( txt, text = i, variable = variables[-1] )
txt.window_create( "end", window=cb )
txt.insert( "end", "\n" )
txt.pack()
def read_ticks():
# result = [( ing, cb.get() ) for ing, cb in zip( INGREDIENTS, variables ) ]
result = [ ing for ing, cb in zip( INGREDIENTS, variables ) if cb.get()>0 ]
print( result )
but = tk.Button( root, text = 'Read', command = read_ticks)
but.pack()
root.mainloop()
Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire