簡體   English   中英

Python:專注於ttk.Notebook標簽

[英]Python: Focus on ttk.Notebook tabs

我還沒想出如何將焦點設置在ttk.Notebook的特定選項卡上。 focus_set不起作用。 有可能嗎?

提前致謝

我遇到了同樣的問題。 我發現筆記本的'select'方法(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 docs: https ://docs.python.org/2/library/ttk.html#ttk.Notebook

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

這段代碼基於wordsforthewise對這個問題的回答。 在這里您可以找到使用select作為get和set函數的示例,它通過在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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM