Local variable 'state' value is not used
Question:
I don’t understand why my variable is not recognized in my function and why it is not updated after the function is executed. I have applied the same procedure throughout my project and it has worked every time. Here is my code:
all_button = []
state = "normal"
for x in range(5):
all_button.append(tk.Button(new_scrollable_frame, text=str(x)))
def show_or_destroy_3d(state_3d):
def hide():
print("Destroy")
for i in all_button:
i.destroy()
def show():
print("Show")
for i in all_button:
i.pack(side="left")
if state_3d == "normal":
state = "destroyed" # don't work
hide()
else:
state = "normal" # don't work
show()
tk.Button(new_scrollable_frame, height=3, width=10, text="Fichier 3D",
command=lambda test=state: show_or_destroy_3d(test)).pack(side="left")
I tried to replace the 2 "state =" by state.replace() but it doesn’t change anything.. I tried many other thing but nothing worked. Surely a basic mistake, if someone could help me..
Answers:
Use the global
keyword:
def show_or_destroy_3d(state_3d):
global state
# ...
I don’t understand why my variable is not recognized in my function and why it is not updated after the function is executed. I have applied the same procedure throughout my project and it has worked every time. Here is my code:
all_button = []
state = "normal"
for x in range(5):
all_button.append(tk.Button(new_scrollable_frame, text=str(x)))
def show_or_destroy_3d(state_3d):
def hide():
print("Destroy")
for i in all_button:
i.destroy()
def show():
print("Show")
for i in all_button:
i.pack(side="left")
if state_3d == "normal":
state = "destroyed" # don't work
hide()
else:
state = "normal" # don't work
show()
tk.Button(new_scrollable_frame, height=3, width=10, text="Fichier 3D",
command=lambda test=state: show_or_destroy_3d(test)).pack(side="left")
I tried to replace the 2 "state =" by state.replace() but it doesn’t change anything.. I tried many other thing but nothing worked. Surely a basic mistake, if someone could help me..
Use the global
keyword:
def show_or_destroy_3d(state_3d):
global state
# ...