2013-07-31 75 views
2

好吧,我有這個簡單的代碼:Python的Tkinter的:處理多個標籤

import tkinter.filedialog 
from tkinter import * 
import tkinter.ttk as ttk 

root = Tk() 
root.title('test') 

nb = ttk.Notebook(root) 
nb.pack(fill='both', expand='yes') 

f1 = Text(root) 
f2 = Text(root) 
f3 = Text(root) 

nb.add(f1, text='page1') 
nb.add(f2, text='page2') 
nb.add(f3, text='page3') 

root.mainloop() 

,我只是想知道,什麼是處理與Tkinter的對他們的文本多個標籤的最佳方式?就像我想刪除'page2'上的所有文本或在'page3'上插入某些內容'那麼我該怎麼做?

回答

2

你已經在f1f2f3到文本組件的引用,這樣你就可以直接調用它們的方法:

f2.delete(1.0, 'end') # erase all the text on the 2nd tab 

f3.insert('end', 'hello, world') # insert text on the 3rd tab 

您可能還希望將小部件添加到列表中,如果你希望爲所有人執行相同的操作。

texts = [f1, f2, f3] 
for text in texts: 
    text.delete(1.0, 'end')