I am trying to create multiple tabs using ttk.Notebook
widget. I am creating a simple multi-tab notepad. but I don't know how to deal with the NoteBook widget. but when I click on save button it overrides all tabs text area that because of I declared there self.tx.get("1.0","end-1c")
.all written file change their text according to last text. Thanks for helping me
#-*- coding: utf-8 -*-
import tkinter.ttk as ttks
from tkinter import LEFT,RIGHT,X,Y,BOTH
class MainUI:
def __init__(self,master):
self.master = master
self.nb = ttks.Notebook(self.master)
self.nb.pack(fill='both',expand=1)
self.name = ttks.Entry(self.master)
self.name.pack()
self.save_tab = ttks.Button(self.master,text="save",command=lambda:self.save_file()).pack()
#tab1
self.page1 = ttks.Frame(self.nb)
self.txt = ttks.tkinter.Text(self.page1)
self.txt.pack(fill='both',expand=1)
self.nb.add(self.page1,text="tab1")
self.page2 = ttks.Frame(self.nb)
self.nb.add(self.page2,text="tab2")
self.master.bind('',self.add_tabs)
def add_tabs(self,event):
self.page_name = ttks.Frame(self.nb)
self.tx = ttks.tkinter.Text(self.page_name)
self.tx.pack(fill=BOTH,expand=1)
self.nb.add(self.page_name,text="pagename")
def save_file(self):
self.fname = self.name.get()
self.txtinput = self.tx.get("1.0","end-1c")
with open(self.fname,'w') as f:
f.write(self.txtinput)
if __name__ == "__main__":
root = ttks.tkinter.Tk()
root.title('Tabs>>')
root.geometry('500x500')
MainUI(root)
root.mainloop()
In add_tabs
, you are replacing the value of self.tx
each time that a new tab is added. I.e. you only remember the last created text widget. You need to:
In __init__
, add:
self.txs = [] # "s" suffix to discern list and elements
in add_tabs
, use:
tx = ttks.tkinter.Text(self.page_name)
self.txs.append(tx) # append to list
tx.pack(...)
in save_file
, use:
tab_index = self.nb.index(self.nb.select())
tx = self.txs[tab_index + 1] # may need to adjust depending on how much "static" tabs are in front
txtinput = self.tx.get("1.0", "end-1c")
# ...