0

I have a code below where you can create tabs upon clicking button on menu. Also, there's a print button that prints all the contents of all the tabs.

from tkinter import *
from tkinter.ttk import *
from tkinter.scrolledtext import ScrolledText

root = Tk()


class F(Frame):
    def __init__(self):
    super().__init__()
    root.geometry("500x500")
    self.master.resizable(False,False)


    self.tab = Notebook(root)
    self.tab.grid(row=1, column=33, columnspan=10, rowspan=50, padx=5, pady=5, sticky='NS')

    self.mb = Menu( root )
    root.config( menu=self.mb )
    self.sub_mb = Menu( self.mb, tearoff=0 )
    self.mb.add_command( label='create tab', command = self.create_tab )
    self.mb.add_command( label='print', command=self.print_contents_of_all_tabs )

    def create_tab(self):
        self.new_tab = ScrolledText(height=20, width=50)
        self.tab.add( self.new_tab, text='tab' )

    **# this should print the contents inside all the tabs**
    def print_contents_of_all_tabs(self):
        all_tabs = self.tab.tabs()            # get frame name of all tabs
        for x in range(len(all_tabs)):
            print(self.tab.index(all_tabs[x])) # print the index using frame name
            print(self.new_tab.get(1.0, END))  # This prints only the content of recently created tab

def main():
    F().mainloop()

main()

Now, I want to print all the contents of all the tabs using def print_contents_of_all_tabs(self). I already got the index of all tabs but I don't know how to use each index to get the text per tab. Also, if I use self.new_tab.get(1.0, END), It takes only the contents inside the recently created tab.

Say I have 3 tabs, first tab contains "this is first tab", second contains "this is second tab" and lastly third tab contains "this is the third tab". When I click print, the output from the code above would be like

0   # index of first tab
this is the third tab

1    # index of second tab
this is the third tab

2    # index of third tab
this is the third tab

Any help would be very much appreciated

1 Answer 1

0

As per this post which explains widget retrieval:

from tkinter import *
from tkinter.ttk import *
from tkinter.scrolledtext import ScrolledText

root = Tk()


class F(Frame):
    def __init__(self):
        super().__init__()
        root.geometry("500x500")
        self.master.resizable(False,False)


        self.tab = Notebook(root)
        self.tab.grid(row=1, column=33, columnspan=10, rowspan=50, padx=5, pady=5, sticky='NS')

        self.mb = Menu( root )
        root.config( menu=self.mb )
        self.sub_mb = Menu( self.mb, tearoff=0 )
        self.mb.add_command( label='create tab', command = self.create_tab )
        self.mb.add_command( label='print', command=self.print_contents_of_all_tabs )

    def create_tab(self):
        new_tab = ScrolledText(height=20, width=50)
        self.tab.add( new_tab, text='tab' )

    def print_contents_of_all_tabs(self):
        # this should print the contents inside all the tabs**
        all_tabs = self.tab.tabs()            # get frame name of all tabs
        for x in range(len(all_tabs)):
            print(self.tab.index(all_tabs[x])) # print the index using frame name
            frame = self.tab._nametowidget(all_tabs[x])
            scrolled_text = frame.winfo_children()[1]
            print(scrolled_text.get(1.0, END))  # This prints the content of the tab

def main():
    F().mainloop()

main()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.