vendredi 17 juin 2022

Disable tkinter checkbuttons when a button is pressed

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:

  1. get a list with some ingredients
  2. get a tkinter GUI with some checkbuttons (one for each ingredient)
  3. select some of the checkbuttons (tick them)
  4. press a button and obtain a list containing the ingredients I selected

What I am trying to do now is the following:

  1. 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