Take a look at this GitHub issue, you actually can rename tabs in customtkinter's Tabview.
This is the solution mentioned:
tabview._segmented_button._buttons_dict["Old tab name"].configure(text="New tab name")
Combine with your code:
Without function:
self.tabview._segmented_button._buttons_dict[tabname].configure(text=new_name)
With function:
def rename_tab(self, tabname):
dialog = customtkinter.CTkInputDialog(text="What is the tab name?", title="Rename tab")
new_name = dialog.get_input()
self.tabview._segmented_button._buttons_dict[tabname].configure(text=new_name)
Note: If a KeyError
is raised, that's because this method only works if you only change the tab's name once throughout the whole code. If you want to rename multiple times like this:
tabview._segmented_button._buttons_dict["Old tab name"].configure(text="New tab name")
tabview._segmented_button._buttons_dict["New tab name"].configure(text="Even newer tab name")
you can create a new variable that will hold the active (current) tab's name by using the .get()
method and use it to access the _buttons_dict
variable and change the tab's name:
active_tab = tabview.get()
tabview._segmented_button._buttons_dict[active_tab].configure(text="New tab name")
# [Imagine there were a lot of codes here...]
tabview._segmented_button._buttons_dict[active_tab].configure(text="Even newer tab name")
- This is the perfect code if you just want to rename a tab and don't want to touch anything else: (Thank @Fuzzy a lot for contributing to this answer)
def rename_tab(self):
dialog = customtkinter.CTkInputDialog(text="What is the tab name?", title="Add a tab")
active_tab=self.tabview.get()
new_name=dialog.get_input()
self.tabview._segmented_button._buttons_dict[active_tab].configure(text=new_name)
Or, if you want to get the tab's name later in your code, you will need to update the _buttons_dict
(dictionary) with the new tab name (method used):
tabview._segmented_button._buttons_dict["Old tab name"].configure(text="New tab name")
tabview._segmented_button._buttons_dict["New tab name"] = tabview._segmented_button._buttons_dict["Old tab name"]
del tabview._segmented_button._buttons_dict["Old tab name"]
tabview._segmented_button._buttons_dict["New tab name"].configure(text="Even newer tab name")
- This is the perfect code if you want to rename a tab but also want to get that tab's name later in your code:
def rename_tab(self, tabname):
dialog = customtkinter.CTkInputDialog(text="What is the tab name?", title="Rename tab")
new_name = dialog.get_input()
self.tabview._segmented_button._buttons_dict[tabname].configure(text=new_name)
self.tabview._segmented_button._buttons_dict[new_name] = self.tabview._segmented_button._buttons_dict[tabname]
del self.tabview._segmented_button._buttons_dict[tabname]
The code above is quite complicated. But I will try my best to explain to you.
First, let's divide this piece of code into 3 parts:
Part 1:
tabview._segmented_button
Every time you create a CTkTabview
, it will create a variable named _segmented_button
, which is a CTkSegmentedButton
with some CTkFrame
which will be shown when you click on the tab and hidden every time you click on an another tab. Basically, that CTkSegmentedButton
is the buttons on the CTkTabview
that you can click on to move to an another tab.
Part 2:
._buttons_dict["Old tab name"]
Every time you create a CTkSegmentedButton
, a new variable named _buttons_dict
, which is a Python dictionary (dict
) is created. When you use tabview.add(...)
to add a tab into the tabview, the name of that tab will be added into _buttons_dict
as a key and the button that holds that name will be added as a value. So _buttons_dict
would look like this: {"tab name": <CTkButton object>}
. So, if you want to rename a tab, you just need to call that button from _buttons_dict
by using _buttons_dict["tab name"]
Part 3:
.configure(text="New tab name")
After accessing into the button object, you simply just need to change the text
argument into your custom name and that's it, you've just renamed your tab!
NOTE(s):
- This is not an official way to rename your tabs since this method can stop working with new updates anytime (Last checked customtkinter version: 5.2.0
).