I am writing an app with CustomTkinter. I have created a collapsible frame with a button to collapse and uncollapse the frame. But I need to be able to disable this button. I wrote a function disable_uncollapse
to do this but the button remains clickable.
import tkinter
import customtkinter as ctk
import tkinter as tk
from tkinter import ttk
from PIL import Image
ctk.set_appearance_mode("Dark")
ctk.set_default_color_theme("dark-blue")
appWidth, appHeight = 600, 700
class CollapsibleFrame(ctk.CTkFrame):
"""
Class to create a collapsible frame. Starts as collapsed. Row and Column are row and column of parent frame it should be gridded to
"""
def __init__(self, master, row:int=0,column:int=0,collapsed_text:str='',**kwargs):
super().__init__(master, **kwargs)
self.collapsed_text=collapsed_text
# add widgets onto the frame, for example:
self.label = ctk.CTkLabel(self)
self.label.grid(row=0, column=0)
self.title_label = ctk.CTkLabel(self,
text=self.collapsed_text)
self.title_label.grid(row=0, column=0)
self.row=row
self.column=column
self.collapsed_widgets=[]
# load icons
self.down_chevron=ctk.CTkImage(Image.open("chevron_up.png"), size=(7, 7))
self.up_chevron = ctk.CTkImage(Image.open("chevron_down.png"), size=(7, 7))
# Add hide/unhide buttons
self.collapsebuton = ctk.CTkButton(self,
image=self.down_chevron,text="")
self.collapsebuton.grid(row=0, column=1,
padx=20, pady=20,
sticky="e")
self.collapsebuton.bind("<Button-1>", lambda event: self.collapse())
self.uncollapsebuton = ctk.CTkButton(self,
image=self.up_chevron,text="")
self.uncollapsebuton.grid(row=0, column=1,
padx=20, pady=20,
sticky="e")
self.uncollapsebuton.bind("<Button-1>", lambda event: self.uncollapse())
def hide(self):
""" Completely hides frame """
print('hiding')
self.grid_forget()
def unhide(self):
""" Completely unhides frame """
print('unhiding')
self.grid(row=self.row,column=self.column,sticky='ew')
self.columnconfigure(0,weight=1)
#self.rowconfigure(0, weight=1)
def collapse(self):
""" Collapses frame, leaving icon to uncollapse """
print('collapsing')
# hide all known items
sub_items=self.grid_slaves()
for item in sub_items:
self.collapsed_widgets.append(item) # save these items for if we want to un-hide them later
item.grid_remove()
# add back uncollapse button, title
self.uncollapsebuton.grid()
self.collapsebuton.grid_remove()
self.title_label.grid()
def uncollapse(self):
""" Uncollapses frame, leaving icon to collapse """
print('uncollapsing')
# unhide previously hidden widgets
for item in self.collapsed_widgets:
item.grid()
self.collapsed_widgets.clear()
self.unhide()
self.collapsebuton.grid()
self.uncollapsebuton.grid_remove()
def disable_uncollapse(self):
self.uncollapsebuton.configure(state=ctk.DISABLED)
self.collapsebuton.configure(state='disabled')
class App(ctk.CTk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("My awesome window")
self.geometry(f"{appWidth}x{appHeight}")
self.columnconfigure(0, weight=1)
create_vault_frame = CollapsibleFrame(self,row=0,column=0,collapsed_text="Panel1")
create_vault_frame.uncollapse()
new_label=ctk.CTkLabel(create_vault_frame,text='aaah')
new_label.grid(row=1,column=0)
create_vault_frame.disable_uncollapse()
if __name__ == "__main__":
app = App()
app.mainloop()
I have tried changing how I change the state including trying the following options:
self.uncollapsebuton.configure(state=ctk.DISABLED)
self.uncollapsebuton.configure(state=tk.DISABLED)
self.uncollapsebuton.configure(state="DISABLED")
I have also tried running self.update()
from within the frame.
But none of them seem to take effect. I have added a print statement to check, and this function is indeed getting called, the state is even changing, but the button remains clickable.
te dejo el codigo que funciona para mi def insert_view(self, view): index = self.views.index(view) boton = self.botones[index]