-1

I want to have a series of tab in a Tkinter GUI that can be toggled between a 'normal' and a 'disabled' state using a checkbox. Here is what i have so far :

import tkinter as tk

def simulation_mainloop():    
    print("c")   
    # do stuff

window = tk.Tk()

def change_widget_state(notebook, position, variable) :
    print("d")
    if variable.get() == 1 :
        notebook.tab(position, state='disabled')
    else :
        notebook.tab(position, state='normal')

nb = tk.ttk.Notebook(window)

tab_cristallinity = tk.ttk.Frame(nb)
nb.add(tab_cristallinity, text="Crystallinity", state="normal")
tab_2 = tk.ttk.Frame(nb)
nb.add(tab_2, text='lorem ipsum')

Button_start = tk.Button(window, text="Start", command=simulation_mainloop)

# creation of the widget checkboxes menu
LabelFrame_Widgets = tk.LabelFrame(window, text="widgets")
var0 = tk.IntVar()
Checkbutton_cristallinity = tk.Checkbutton(LabelFrame_Widgets, variable=var0, text="Crystallinity", command=change_widget_state(nb, 0, var0))

Button_start.grid(row = 1, column = 1, columnspan = 2)

LabelFrame_Widgets.grid(row = 2, column = 1, columnspan = 2)
Checkbutton_cristallinity.grid(row=1, column=1)

nb.grid(row=1, column=0, sticky='NESW')

window.mainloop()

The Start button works but not the checkbox.

Thanks in advance

  • Possible duplicate of [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/q/5767228) – fhdrsdg Jun 07 '18 at 14:48

1 Answers1

0

Use lambda function in your code:

Checkbutton_cristallinity = tk.Checkbutton(LabelFrame_Widgets, variable=var0, text="Crystallinity", command=lambda:change_widget_state(nb, 0, var0))`
kavko
  • 2,751
  • 13
  • 27