6

I haven't figured out how to set the focus on a specific tab of a ttk.Notebook. focus_set does not work. Is there any possibility?

Thanks in advance

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
dolby
  • 643
  • 1
  • 8
  • 10

2 Answers2

10

I was having the same problem. What I found is the 'select' method for notebooks (ttk.Notebook.select(someTabFrame)) solves this problem:

import ttk, Tkinter

mainWindow = Tkinter.Tk()

mainFrame = Tkinter.Frame(mainWindow, name = 'main-frame')
mainFrame.pack(fill = Tkinter.BOTH) # fill both sides of the parent

nb = ttk.Notebook(mainFrame, name = 'nb')
nb.pack(fill = Tkinter.BOTH, padx=2, pady=3) # fill "master" but pad sides

tab1Frame = Tkinter.Frame(nb, name = 'tab1')
Tkinter.Label(tab1Frame, text = 'this is tab 1').pack(side = Tkinter.LEFT)
nb.add(tab1Frame, text = 'tab 1')

tab2Frame = Tkinter.Frame(nb, name = 'tab2')
Tkinter.Label(tab2Frame, text = 'this is tab 2').pack(side = Tkinter.LEFT)
nb.add(tab2Frame, text = 'tab 2')

nb.select(tab2Frame) # <-- here's what you're looking for

mainWindow.mainloop()

python docs for ttk.Notebook: https://docs.python.org/2/library/ttk.html#ttk.Notebook

I also used this blog post as a model for my code: http://poquitopicante.blogspot.com/2013/06/blog-post.html

wordsforthewise
  • 13,746
  • 5
  • 87
  • 117
0

This code based on wordsforthewise's answer to this question. Here you can find example of using select as get and set function, it shown by button that switch between 2 tabs.

small improvement:

import ttk, Tkinter
from pango import Weight
from Tkinter import Button


tab2Frame = None
tab1Frame = None

def switchTab():
    if nb.select()[-1] == "1":
        nb.select(tab2Frame)
    elif nb.select()[-1] == "2":
        nb.select(tab1Frame)

mainWindow = Tkinter.Tk()

mainWindow.geometry("%dx%d+0+0" % (200, 200))
mainFrame = Tkinter.Frame(mainWindow, name = 'main-frame')
mainFrame.pack(fill = Tkinter.BOTH) # fill both sides of the parent

button = Button(mainWindow, text = "Switch", command = switchTab)
button.configure(width = 15, activebackground = "#6f6Fff")
button.pack()

nb = ttk.Notebook(mainFrame, name = 'nb')
nb.pack(fill = Tkinter.BOTH, padx=2, pady=3) # fill "master" but pad sides

tab1Frame = Tkinter.Frame(nb, name = 'tab1')
Tkinter.Label(tab1Frame, text = 'this is tab 1').pack(side = Tkinter.LEFT)
nb.add(tab1Frame, text = 'tab 1')

tab2Frame = Tkinter.Frame(nb, name = 'tab2')
Tkinter.Label(tab2Frame, text = 'this is tab 2').pack(side = Tkinter.LEFT)
nb.add(tab2Frame, text = 'tab 2')

mainWindow.mainloop()
ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
  • You wrote "small improvement". Small improvement to what? Can you explain the improvement so people don't have to compare your solution line-by-line to whatever it is an improvement of? – Bryan Oakley Oct 03 '15 at 01:54
  • Yeah, if it's a small improvement, probably a better idea to just edit my post – wordsforthewise Sep 12 '16 at 19:06
  • @Bryan Oakley, I didn't see your comment somehow. Sorry guys, I can't remember now. I'll try to come back to in later. – ChaosPredictor Sep 13 '16 at 20:32
  • I know this is an old post but `nb.select()[-1] ` for me returns `e` for all tabs. Just thought I would point that out as it appears to not return index or numbers. – Mike - SMT Sep 20 '19 at 13:33