Python:专注于ttk.Notebook标签

时间:2015-01-22 11:53:49

标签: python tkinter focus ttk

我还没想出如何将焦点设置在ttk.Notebook的特定标签上。 focus_set不起作用。有可能吗?

提前致谢

2 个答案:

答案 0 :(得分:5)

我遇到了同样的问题。我发现的是选择'笔记本的方法(ttk.Notebook.select(someTabFrame))解决了这个问题:

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()

ttk.Notebook的python文档: https://docs.python.org/2/library/ttk.html#ttk.Notebook

我还将此博客文章用作我的代码的模型: http://poquitopicante.blogspot.com/2013/06/blog-post.html

答案 1 :(得分:0)

此代码基于 wordsforthewise 对此问题的回答。在这里您可以找到使用选择作为获取和设置功能的示例,它通过在2个选项卡之间切换的按钮显示。

小改进:

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()