Search code examples
python-3.xtext-widget

Get the Text content from a ttk.Notebook


im trying to access text content from a ttk.Notebook.

I read an unkonwn number of text files (<20), and create a new tab for every .txt-file. And add the content of the .txt to a Text-widget for every tab.

os.chdir('C://Users//Public//Documents')
    myNotes = glob.glob('*.txt')
    myNotes.append('+')
    
    self.notebook = ttk.Notebook(self.master)
    for files in myNotes:
        if files != '+':
            with open('C://Users//Public//Documents//'+files,'r') as f:
                value = f.read()
        else:
            value=''
        self.notebookTab = ttk.Frame(self.notebook)
        self.notebook.add(self.notebookTab, text=files)
        self.text = Text(self.notebookTab, bd=0, wrap='word')
        self.text.pack(fill='both', expand=True)
        self.text.insert('1.0', value)
self.notebook.pack(fill='both', expand=True)

I can get the name of the active tab (Eg. name of the text-file) with this:

activeTabName = self.notebook.tab(self.notebook.select(), "text")

But I cant figure out how to get the text of the Text-widget associated with the active tab. What I like to accomplish is to be able to modify the content of one or several text-files, and save the new content to the correct .txt-file.

Anyone have any idéas?


Solution

  • I found a way to achive what i wished to do.

    I saved my Text-widgets in a list. And then fetched the index from the tabs and got that index from my list. Eg:

    self.textLista = []
    ...
    self.textLista.append(self.text)
    ...
    activeTabNo = self.notebook.index("current")
    self.textLista[activeTabNo].get('1.0', END+'-1c'))
    

    This might not be a good way of doing it, but atleast it works for my purpouse.