繁体   English   中英

Python tkinter:处理多个选项卡

[英]Python tkinter: Handle multiple tabs

好的,我有这个简单的代码:

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'上插入一些内容一样,我该怎么做?

您已经在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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM