Search code examples
pythonlisttexttkinterttk

Text widgets as List


I'm trying to create tabs with Text using tkinter Notebook widget and Text widget. I have created list of tabs and Text widgets everything going as far as good except add_tabs method. Whenever I press control-n for adding new tabs only for first time i got this exception:

Exception

I have no idea how can i fix this problem thanks for helping me.

Thank you.

C:\Users\Imtiyaz\Desktop>python maintabtest.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
    return self.func(*args)
  File "maintabtest.py", line 32, in add_tabs
    self.nb.add(self._tabs[self.i],text="untitled")
IndexError: list index out of range

code:

import tkinter.ttk as ttks
from tkinter import BOTH,LEFT
class mainbody:
    def __init__(self,master):
        self.master = master
        self._tabs = []
        self._text = []
        self.i = 1
        self.body = ttks.Frame(self.master)
        self.nb = ttks.Notebook(self.master)
        self.nb.pack(fill=BOTH,expand=1)
        self.body.pack(fill=BOTH,expand=1)

        self.initial_tab = ttks.Frame(self.nb)
        self.Inittext = ttks.tkinter.Text(self.initial_tab)
        self.Inittext.pack(fill=BOTH,expand=1)
        self.initial_tab.pack(fill=BOTH,expand=1)
        self._text.append()
        self.nb.add(self.initial_tab,text="first_tab")

        self.File_name = ttks.Entry(self.master)
        self.File_name.pack(side=LEFT)
        self.sbtn = ttks.Button(self.master,text="save_btn",command=lambda:self.save_())
        self.sbtn.pack(side=LEFT)
        self.master.bind('<Control-n>',self.add_tabs)

    def add_tabs(self,event):
        self._tabs.append(ttks.Frame())
        self.nb.add(self._tabs[self.i],text="untitled")
        self._text.append(ttks.tkinter.Text(self._tabs[self.i]))
        self._text[self.i].pack(fill=BOTH,expand=1)
        self.i = self.i + 1

    def save_(self):
        self.fname = self.File_name.get()
        self._txt_id = self.nb.index('current')
        self.get_input = self._text[self._txt_id].get("1.0","end-1c")
        with open(self.fname,'w') as f:
            f.write(self.get_input)
if __name__ == "__main__":
    root = ttks.tkinter.Tk()
    mainbody(root)
    root.mainloop()

Solution

  • Your problem is that self.i did not synchronize with the size of the 2 lists: self._tabs and self._text.

    Basically you don't need the self.i to track the index to the last item of the 2 lists. Just use -1 instead of self.i in add_tabs() to refer the last item in the list as below:

    import tkinter.ttk as ttks
    from tkinter import BOTH,LEFT
    from tkinter.messagebox import showinfo
    
    class mainbody:
        def __init__(self,master):
            self.master = master
            self._tabs = []
            self._text = []
            self.body = ttks.Frame(self.master)
            self.nb = ttks.Notebook(self.master)
            self.nb.pack(fill=BOTH, expand=1)
            self.body.pack(fill=BOTH, expand=1)
    
            self.add_tabs("first_tab") # add the initial tab
    
            self.File_name = ttks.Entry(self.master)
            self.File_name.pack(side=LEFT)
            self.sbtn = ttks.Button(self.master, text="save_btn", command=self.save_file)
            self.sbtn.pack(side=LEFT)
            self.master.bind('<Control-n>', lambda e:self.add_tabs())
    
        def add_tabs(self, name="untitled"):
            self._tabs.append(ttks.Frame())
            self.nb.add(self._tabs[-1], text=name)
            self._text.append(ttks.tkinter.Text(self._tabs[-1]))
            self._text[-1].pack(fill=BOTH, expand=1)
    
        def save_file(self):
            self.fname = self.File_name.get().strip()
            if not self.fname == '':
                self._txt_id = self.nb.index('current')
                self.get_input = self._text[self._txt_id].get("1.0","end-1c")
                with open(self.fname, 'w') as f:
                    f.write(self.get_input)
            else:
                showinfo('Warning', 'Please input filename')
    
    if __name__ == "__main__":
        root = ttks.tkinter.Tk()
        mainbody(root)
        root.mainloop()