CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

CTkTabview Memory Issue

Open ElectricCandlelight opened this issue 2 years ago • 1 comments

When using .delete("Tab Name") the CTkFrame that is created during .add("Tab Name") isn't being destroyed, resulting in increased memory usage.

Changing line 333 in ctk_tabview.py to self._tab_dict[name].destroy() Seems to help and removes the frame created but there is still memory not being released.

ElectricCandlelight avatar Jan 12 '23 17:01 ElectricCandlelight

A small example

import customtkinter

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.tab_name = 0

        self.tabview = customtkinter.CTkTabview(self)
        self.tabview.grid()

        self.tabview.add("Add Tab")

        self.add_button = customtkinter.CTkButton(self.tabview.tab("Add Tab"), text="Add", width=100, command= self.add_tab)
        self.add_button.grid()

    def add_tab(self):
        self.tabview.add(self.tab_name)

        self.del_button = customtkinter.CTkButton(self.tabview.tab(self.tab_name), text="Del", width=100, command= self.del_tab)
        self.del_button.grid(row=1)

        self.tabview.set(self.tab_name)
        self.tab_name += 1

    def del_tab(self):
        self.current_tab = self.tabview.get()
        self.tabview.delete(self.current_tab)
        
        print(str(self.tabview.winfo_children()))

if __name__ == "__main__":
    app = App()
    app.mainloop()

ElectricCandlelight avatar Jan 13 '23 00:01 ElectricCandlelight

How did you measure? How big is the memory leak? What operating system, versions of Python and CustomTkinter did you use?

Wolf-SO avatar Feb 12 '23 20:02 Wolf-SO

Windows 10: Build19045 Python: 3.10.7 CustomTkinter: 5.1.2

Well the leak depends on what is stored in each tab. On the example I posted it's not that noticeable as each tab only has a button. *See Note

The above example was to demonstrate that the frames being created by .add() aren't being destroyed with .delete() which can cause slowdown and increased memory usage.

Line# 333 in ctk_tabview.py needs to be changed to self._tab_dict[name].destroy() as I have put in my PR #1083

*Unless you add a ridiculous number of tabs. Made a loop to add and delete 1000 tabs. Without the change to line#333 the program uses 73.1MB with the change it only uses 29.1MB

ElectricCandlelight avatar Feb 13 '23 15:02 ElectricCandlelight