TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
10.84k stars 1.02k forks source link

CTkTabview - cannot change name of open tab #2277

Open mikolaj-rokicki opened 4 months ago

mikolaj-rokicki commented 4 months ago

Hi,

I've noticed that you cannot change a name of currently opened tab of CTkTabview by rename function. It responds with errors:

_

Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64qbz5n2kfra8p0\Lib\tkinter\init.py", line 1967, in call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "C:\Users\mikol\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\customtkinter\windows\widgets\ctk_button.py", line 554, in _clicked self._command() File "C:\Users\mikol\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\customtkinter\windows\widgets\ctk_segmented_button.py", line 167, in command=lambda v=value: self.set(v, from_button_callback=True), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mikol\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\customtkinter\windows\widgets\ctk_segmented_button.py", line 381, in set self._command(self._current_value) File "C:\Users\mikol\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\customtkinter\windows\widgets\ctk_tabview.py", line 104, in _segmented_button_callback self._tab_dict[self._current_name].grid_forget()

_

Although its not severe issue, couse one can still change name when tab is inactive

ByteJoseph commented 4 months ago

Can you please provide the source code?

mikolaj-rokicki commented 4 months ago

I've made some basic code to demonstrate this issue:

import customtkinter as ctk

def rename():
    tabview.rename('1', '3')

app = ctk.CTk()

tabview = ctk.CTkTabview(app)
tab1 = tabview.add('1')
tab2 = tabview.add('2')
tabview.grid(row = 0, column = 0)

button = ctk.CTkButton(app, command=rename)
button.grid(row = 1, column = 0)

label1 = ctk.CTkLabel(tab1, text='test label')
label1.grid(row = 0, column = 0)
label2 = ctk.CTkLabel(tab2, text='test label')
label2.grid(row = 0, column = 0)

app.mainloop()

When I'm changing name of not active tab, everything works well. But if i try to change name of opened tab, the button on top stops being blue and after i try to change tab i get same KeyError. image ('3' on image should be blue)

I've managed to avoid error by adding bottom 2 lines to rename function of CTkTabview:

def rename(self, old_name: str, new_name: str):
        if new_name in self._name_list:
            raise ValueError(f"new_name '{new_name}' already exists")

        # segmented button
        old_index = self._segmented_button.index(old_name)
        self._segmented_button.delete(old_name)
        self._segmented_button.insert(old_index, new_name)

        # name list
        self._name_list.remove(old_name)
        self._name_list.append(new_name)

        # tab dictionary
        self._tab_dict[new_name] = self._tab_dict.pop(old_name)

        # changing current tab name
        if old_name == self._current_name:
            self._current_name = new_name

But it still doesn't work fully as intended