Finding the currently selected tab of Ttk Notebook

Matthew picture Matthew · Dec 22, 2012 · Viewed 18.6k times · Source

I have a Ttk Notebook widget containing 8 Frames - so, 8 tabs. Each frame contains a Text widget. I have a button outside the Notebook widget, and I want to insert text into the current tabs Text widget when this button is pressed.

This would seem to require working out which widget in the Notebook is currently selected, but I can't seem to find how to do this. How would I find the currently selected tab?

Alternatively, how can I implement what I want to?

If it helps, here's the code for my notebook:

self.nb = Notebook(master)
self.nb.pack(fill='both', expand='yes', padx=10, pady=10)
self.frames = []
self.texts = []
for i in xrange(8):
  self.frames.append(Frame())
  self.nb.add(self.frames[i])
  self.texts.append(Text(self.frames[i]))
  self.texts[i].pack(fill='both')

Answer

FabienAndre picture FabienAndre · Dec 23, 2012

You can retrieve the selected tab through select method. However, this method returns a tab_id which is not much useful as is. index convert it to the number of the selected tab.

>>> nb.select()
'.4299842480.4300630784'
>>> nb.index(nb.select())
2

Note that you coud also get more information about the selected tab using tab

>>> nb.tab(nb.select(), "text")
'mytab2'

You might look at Notebook reference documentation : http://docs.python.org/3/library/tkinter.ttk.html#notebook