Ok, I have this simple code:
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()
and I am just wondering, what is the best way to handle multiple tabs with text on them in tkinter? Like if I wanted to erase all the text on just 'page2' or insert something on 'page3' how would I do that?
You already have the references to the text widgets in f1
, f2
and f3
, so you can call their methods directly:
f2.delete(1.0, 'end') # erase all the text on the 2nd tab
f3.insert('end', 'hello, world') # insert text on the 3rd tab
You might also want to add the widgets to a list, in case you want to perform the same action for all.
texts = [f1, f2, f3]
for text in texts:
text.delete(1.0, 'end')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With