sam
sam

Reputation: 5

How do I create 'n' tabs in ttk notebook? n is a variable(user-input)

I accept the number n from the user, and I want to display n tabs with certain information, how could I do that?

import tkinter as tk                     
from tkinter import ttk 

root = tk.Tk() 
tabControl = ttk.Notebook(root) 

tab1 = ttk.Frame(tabControl) 
tab2 = ttk.Frame(tabControl) 

tabControl.add(tab1, text ='Tab 1') 
tabControl.add(tab2, text ='Tab 2') 
tabControl.pack(expand = 1, fill ="both") # i want to make n such tabs

ttk.Label(tab1, text ="some info here").grid(column = 0, row = 0) 
ttk.Label(tab2, text ="more info here").grid(column = 0, row = 0) 

root.mainloop() 

Upvotes: 0

Views: 111

Answers (1)

dustin-we
dustin-we

Reputation: 498

Maybe try this:

n = int(input("How many Tabs do you want to open?"))
tabs = []
for i in range(n):
    tabs.append(ttk.Frame(tabControl))

for i, tab in enumerate(tabs):
    tabControl.add(tab, text=f"Tab {str(i+1)}")

You can then edit every tab by looping over the tabs list.

Upvotes: 2

Related Questions