Text widgets as ListHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How do you split a list into evenly sized chunks?How to make a flat list out of list of listsHow do I get the number of elements in a list?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How do I find all files containing specific text on Linux?

Giant space birds hatching out of planets; short story

Why does RPM for a fixed-pitch propeller change with an aircraft's pitch?

Decreasing star size

Word for showing a small part of something briefly to hint to its existence or beauty without fully uncovering it

At what rate does the volume (velocity) of a note decay?

Does academia have a lazy work culture?

What do I do when a student working in my lab "ghosts" me?

What self-defense weapons are legal in London?

3D Statue Park: U shapes

Why not use a diode instead of a resistor for current sense circuits?

What's the difference between 2a and 10a charging options?

How can I stop myself from micromanaging other PCs' actions?

How can I prevent corporations from growing their own workforce?

Is it legal to use cash pulled from a credit card to pay the monthly payment on that credit card?

Is it normal practice to screen share with a client?

How do I stop my characters falling in love?

Terence Tao–type books in other fields?

Anybody know what this small Nintendo stand is for?

Why isn't there a serious attempt at creating a third mass-appeal party in the US?

Why isn't there a ";" after "do" in sh loops?

Writing a clean implementation of Rock, Paper, Scissors game in c++

Why are all my history books dividing Chinese history after the Han dynasty?

Send a single HTML email from Thunderbird, overriding the default "plain text" setting

Request for a Latin phrase as motto "God is highest/supreme"



Text widgets as List


How do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How do you split a list into evenly sized chunks?How to make a flat list out of list of listsHow do I get the number of elements in a list?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How do I find all files containing specific text on Linux?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















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:UsersImtiyazDesktop>python maintabtest.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python34libtkinter__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()









share|improve this question
























  • You forgot to add the first tab to the list. Add self._text.append(self.Inittext) somewhere in your __init__.

    – Aran-Fey
    Mar 26 at 17:31












  • Takes only 10 seconds to debug, by the way. All I did was add a print(self._txt_id, self._text) in save_ and that printed an empty list.

    – Aran-Fey
    Mar 26 at 17:34











  • @Aran-Fey i add self._text.append(self._Inittext) now its working but its don't add Text widget to second but add widget to third one. so i set self.i = 1now its working properly but when i add new tab only for first time its throw index out of range exception then later on its work perfect

    – ImtiyazKhan
    Mar 26 at 17:39












  • You did not add the first tab into self._tabs as well.

    – acw1668
    Mar 27 at 3:12

















0















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:UsersImtiyazDesktop>python maintabtest.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python34libtkinter__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()









share|improve this question
























  • You forgot to add the first tab to the list. Add self._text.append(self.Inittext) somewhere in your __init__.

    – Aran-Fey
    Mar 26 at 17:31












  • Takes only 10 seconds to debug, by the way. All I did was add a print(self._txt_id, self._text) in save_ and that printed an empty list.

    – Aran-Fey
    Mar 26 at 17:34











  • @Aran-Fey i add self._text.append(self._Inittext) now its working but its don't add Text widget to second but add widget to third one. so i set self.i = 1now its working properly but when i add new tab only for first time its throw index out of range exception then later on its work perfect

    – ImtiyazKhan
    Mar 26 at 17:39












  • You did not add the first tab into self._tabs as well.

    – acw1668
    Mar 27 at 3:12













0












0








0








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:UsersImtiyazDesktop>python maintabtest.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python34libtkinter__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()









share|improve this question
















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:UsersImtiyazDesktop>python maintabtest.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python34libtkinter__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()






python list text tkinter ttk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 19:11









Ruturaj

1,0839 silver badges18 bronze badges




1,0839 silver badges18 bronze badges










asked Mar 26 at 17:25









ImtiyazKhanImtiyazKhan

227 bronze badges




227 bronze badges












  • You forgot to add the first tab to the list. Add self._text.append(self.Inittext) somewhere in your __init__.

    – Aran-Fey
    Mar 26 at 17:31












  • Takes only 10 seconds to debug, by the way. All I did was add a print(self._txt_id, self._text) in save_ and that printed an empty list.

    – Aran-Fey
    Mar 26 at 17:34











  • @Aran-Fey i add self._text.append(self._Inittext) now its working but its don't add Text widget to second but add widget to third one. so i set self.i = 1now its working properly but when i add new tab only for first time its throw index out of range exception then later on its work perfect

    – ImtiyazKhan
    Mar 26 at 17:39












  • You did not add the first tab into self._tabs as well.

    – acw1668
    Mar 27 at 3:12

















  • You forgot to add the first tab to the list. Add self._text.append(self.Inittext) somewhere in your __init__.

    – Aran-Fey
    Mar 26 at 17:31












  • Takes only 10 seconds to debug, by the way. All I did was add a print(self._txt_id, self._text) in save_ and that printed an empty list.

    – Aran-Fey
    Mar 26 at 17:34











  • @Aran-Fey i add self._text.append(self._Inittext) now its working but its don't add Text widget to second but add widget to third one. so i set self.i = 1now its working properly but when i add new tab only for first time its throw index out of range exception then later on its work perfect

    – ImtiyazKhan
    Mar 26 at 17:39












  • You did not add the first tab into self._tabs as well.

    – acw1668
    Mar 27 at 3:12
















You forgot to add the first tab to the list. Add self._text.append(self.Inittext) somewhere in your __init__.

– Aran-Fey
Mar 26 at 17:31






You forgot to add the first tab to the list. Add self._text.append(self.Inittext) somewhere in your __init__.

– Aran-Fey
Mar 26 at 17:31














Takes only 10 seconds to debug, by the way. All I did was add a print(self._txt_id, self._text) in save_ and that printed an empty list.

– Aran-Fey
Mar 26 at 17:34





Takes only 10 seconds to debug, by the way. All I did was add a print(self._txt_id, self._text) in save_ and that printed an empty list.

– Aran-Fey
Mar 26 at 17:34













@Aran-Fey i add self._text.append(self._Inittext) now its working but its don't add Text widget to second but add widget to third one. so i set self.i = 1now its working properly but when i add new tab only for first time its throw index out of range exception then later on its work perfect

– ImtiyazKhan
Mar 26 at 17:39






@Aran-Fey i add self._text.append(self._Inittext) now its working but its don't add Text widget to second but add widget to third one. so i set self.i = 1now its working properly but when i add new tab only for first time its throw index out of range exception then later on its work perfect

– ImtiyazKhan
Mar 26 at 17:39














You did not add the first tab into self._tabs as well.

– acw1668
Mar 27 at 3:12





You did not add the first tab into self._tabs as well.

– acw1668
Mar 27 at 3:12












1 Answer
1






active

oldest

votes


















0














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()





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55362981%2ftext-widgets-as-list%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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()





    share|improve this answer



























      0














      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()





      share|improve this answer

























        0












        0








        0







        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()





        share|improve this answer













        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()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 3:39









        acw1668acw1668

        3,0852 gold badges7 silver badges16 bronze badges




        3,0852 gold badges7 silver badges16 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55362981%2ftext-widgets-as-list%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript