Reputation: 13
Hey guys I've been learning about Tkinter and making some GUI apps. I'm experimenting with the notebook widget and I'm stuck at one spot. I was wondering if there is a way to use a for loop for all of the current tabs and return the text names of all the tabs instead of the number id. I would like to use it to keep track of the frames. I have searched around and found out how to show tab text for the current tab but I need all the tabs. I haven't been able to find anything specifically for that. Let me know if you have an idea. Thanks.
[EDIT] Code snippet
from tkinter import *
from tkinter import messagebox
from tkinter.scrolledtext import ScrolledText
from tkinter import ttk
class Window(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
self.init_window()
self.second_window()
self.pm_tabs = []
def init_window(self):# Builds the UI for the main window
self.n = ttk.Notebook(root)
self.get_tabs_button = Button(self.textboxframe,
width=20,
text='Get Tab Names',
command=lambda:self.get_tab_names())
self.get_tabs_button.grid(row = 0,
column= 1,
padx=(5,5),
pady=(5,150),
sticky=N+S+E+W)
self.n.add(self.textboxframe, text='Chat')
self.n.grid(row=0, column=0, sticky=N+S+E+W)
def second_window(self):# UI for second window
self.get_tabs_button = Button(self.textboxframe,
width=20,
text='Get Tab Names',
command=lambda:self.get_tab_names())
self.get_tabs_button.grid(row = 0,
column= 1,
padx=(5,5),
pady=(5,150),
sticky=N+S+E+W)
self.n.add(self.textboxframe, text='Second')
def get_tab_names(self): # get the names of the tabs. Hoping to get the text names of the tab.
for tabs in self.n.tabs():
print(tabs)
if __name__ == '__main__':
root = Tk()
app = Window(root)
root.mainloop()
Upvotes: 0
Views: 4555
Reputation: 386010
According to the official documentation, the tab method of the notebook can be used to return information about a tab, such as the text on the tab.
Thus, you can get a list of the text for all of the tabs with something like this:
notebook = ttk.Notebook(...)
...
tab_names = [notebook.tab(i, option="text") for i in notebook.tabs()]
If you're new to list comprehensions, the above is the same as this:
tab_names = []
for i in notebook.tabs():
tab_names.append(notebook.tab(i, "text"))
Upvotes: 7