How to change the tab of ttk.Notebook

10,697

see the following link:
Python Docs if you see the select method that should do what you're after, so something like this:

notebook.select(tab_id)

where the tab id can take a number of forms (see section 24.2.5.3) but the most useful ones are an integer (i think this is the index similar to how lists use indexes) or the name of the tab you wish to switch to.

Share:
10,697
Hangon
Author by

Hangon

Updated on June 29, 2022

Comments

  • Hangon
    Hangon almost 2 years

    I have a ttk.Notebook and with a button I would like to switch to another tab.
    How can I achieve this?

    It looks that changing the tab state (normal, disabled, and hidden) will not solve my problem since I don't want to disable any tabs.

    Here is my code:

    import time
    import ttk as ttk
    import Tkinter as tk
    
    root=tk.Tk()
    
    root.config(width=300,height=220)
    
    notebook=ttk.Notebook(root)
    notebook.place(x=0,y=0)
    
    tabList=[]
    i=0
    while i<6:    
         tabList.append(tk.Frame(root))
         tabList[i].config(width=300,height=150,background='white')
         i+=1
    
    i=0
    while i<6: 
        notebook.add(tabList[i],text='tab'+str(i))
        i+=1
    
    def fLoopTabs():
        i=0
        while i<6:
             notebook.select(i)
             time.sleep(2)
             #Here goes the Screenshot function
             i+=1
    
     button=ttk.Button(root,text='Loop',command=fLoopTabs)
     button.place(x=20,y=180)
    
     root.mainloop()
    
  • Hangon
    Hangon over 9 years
    Thanks for the answer. Im trying to use it inside a loop so I can screenshot every open notebook tab. The select(i) doesnt work inside a loop. I already tried to use time.sleep(5) to check if all tabs are then open. Just the last one will open.
  • James Kent
    James Kent over 9 years
    as you are usiong this in a loop can i ask how you are putting the user interface into its message loop? time.sleep is a blocking call, and without entering the message loop (mainloop) the interface will not update until the loop exits, assuming thats the case you could either put in a call to root.update before the sleep, or you could use threading
  • Hangon
    Hangon over 9 years
    thanks for your help I updated the code in my question.
  • James Kent
    James Kent over 9 years
    as i thought, you need to make the user interface respond to messages, the simplest way to do this for your current purpose would be to add a line "root.update()" between the notebook.select line and the time.sleep line