Skip to main content
 首页 » 编程设计

python之管理 ttk.Notebook 中的选项卡(启用、禁用等)

2025年05月04日12findumars

我想知道如何在 ttk.Notebook 创建后更改选项卡的状态以及如何正确管理多个选项卡。

例子:

import Tkinter as tk 
import ttk 
from myWidgets import Widget1, Widget2, Widget3 
 
def enableTabs(notebook): 
    tabs = notebook.tabs() 
    for i, item in enumerate(tabs): 
        item['state'] = 'enabled' #This doesn't work 
        item.configure(state='enabled') #Doesn't work either 
if __name__ == '__main__': 
    root = tk.Tk() 
 
    notebook = ttk.Notebook(root) 
 
    w1 = Widget1() 
    w2 = Widget2() 
    w3 = Widget3() 
 
    notebook.add(w1, text='tab1', state='disabled') 
    notebook.add(w2, text='tab2', state='disabled') 
    notebook.add(w3, text='tab3', state='disabled') 
 
    enableTabs(notebook) #This would be called upon certain events in the real     application 
 
    root.mainloop() 

在此示例中,我使用禁用 - 启用,但通常我希望能够同时更改某些设置。

请您参考如下方法:

你所说的 item 只是一个标识符(一个 float ),它没有 state 键或 configure 方法。此外,在此上下文中选项卡状态的可能值为 normal、disabled 和 hidden,而不是 enabled。试试这个:

for i, item in enumerate(tabs):  
    notebook.tab(item, state='normal') # Does work