Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python tkinter: Handle multiple tabs

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?

like image 922
user2638731 Avatar asked Oct 22 '22 04:10

user2638731


1 Answers

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')
like image 57
A. Rodas Avatar answered Nov 03 '22 06:11

A. Rodas