Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Focus on ttk.Notebook tabs

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

like image 590
dolby Avatar asked Jan 22 '15 11:01

dolby


1 Answers

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

like image 179
wordsforthewise Avatar answered Oct 09 '22 00:10

wordsforthewise