Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Adding New TAB to NoteBook Widget by Clicking
#1
from tkinter import *
from tkinter import ttk

class Notebook:
    def __init__(self):
        self.root = Tk()
        self.root.title('Note Book')
        self.build_Menu()
        self.build_NoteBook()
        self.build_page_1()
        self.root.mainloop()

    def build_Menu(self):
        # MENU WIDGET CONFIGURATION
        self.menu = Menu(self.root)
        self.root.configure(menu=self.menu)
        self.filemenu = [None] * 3
        for f in range(1,3):
            self.filemenu[f] = Menu(self.menu,tearoff=False)
        #FILE MENU
        self.menu.add_cascade(label='File', menu=self.filemenu[1])
        for i in ['New','Open','Save','Print','Exit']:
            if i == 'New':
                self.filemenu[1].add_command(label=i,command=self.Add_New_Tab)
            elif i == 'Print':
                self.filemenu[1].add_command(label=i)
                self.filemenu[1].add_separator('')
            elif i == 'Exit':
                self.filemenu[1].add_command(label=i,command=quit)
            else:
                self.filemenu[1].add_command(label=i)
        #EDIT MENU
        self.menu.add_cascade(label='Edit',menu=self.filemenu[2])
        for i,k,l in [('Copy','Ctrl+C',''),('Cut','Ctrl+X','s'),('Paste','Ctrl+P',''),('Select All','Ctrl+A','s'),('Delete','Delete','')]:
            if i == 'Delete':
                self.filemenu[2].add_command(label=(f'{i} {k:>20}'),command=lambda:self.canvas.delete(ALL))
            elif l == 's':
                self.filemenu[2].add_command(label=(f'{i} {k:>20}'))
                self.filemenu[2].add_separator('')
            else:
                self.filemenu[2].add_command(label=(f'{i} {k:>20}'))
    # ADD NEW TAG CODE
    def Add_New_Tab(self):
            for k in range(2,3):
                self.notebook.add(self.tab[k],text=f'Page {k}')

    def build_NoteBook(self):
        # NOTEBOOK WIDGET CONFIGURATION
        self.notebook = ttk.Notebook(self.root,height=400,width=800)
        self.tab = [None] * 10
        global t
        for t in range(1,10):
            self.tab[t] = ttk.Frame(self.notebook)

        self.notebook.add(self.tab[1],text='Page 1',underline=0)
        # self.notebook.add(self.tab[2], text='Page 2')
        # self.notebook.add(self.tab[3], text='Page 3')
        # self.notebook.add(self.tab[4], text='Page 4')
        self.notebook.pack(fill=BOTH, expand=YES, padx=5, pady=5)

        #PAGE ONE CONTENTS
    def build_page_1(self):
        self.canvas = Canvas(self.tab[1],width=100,height=50,bg='#ffffff',cursor='heart')
        self.canvas.pack(fill=BOTH,expand=True,padx=5,pady=5)
        self.canvas.bind('<B1-Motion>',self.bind_paint)
        ttk.Button(self.tab[1], text='clean', command=lambda: self.canvas.delete(ALL)).pack(side=LEFT,anchor=CENTER,padx=90,pady=5,fill=X,expand=1)
        ttk.Button(self.tab[1], text='close', command=quit).pack(side=LEFT, padx=90, fill=X, anchor=CENTER,expand=1)

    #BINDING MOTION THAT SKETCHED THE CIRCLES RED COLORS
    def bind_paint(self,event):
        x1,y1 = (event.x - 5),(event.y - 5)
        x2,y2 = (event.x + 5),(event.y + 5)
        i = self.canvas.create_oval(x1,y1,x2,y2,fill='#ff0000',outline='#000000')
        return i

if __name__ == '__main__':
    Notebook()
Please how can I add one new tab/page (Frame) at a time into my NoteBook Widget each time I clicked on 'New' in my File Menu Widget?
Thanks.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyQt] coloring the widget table row atlass218 5 153 Yesterday, 07:57 PM
Last Post: Axel_Erfurt
  [Tkinter] Inserting numbers into entry box in a widget richard13 1 141 Aug-11-2019, 11:37 AM
Last Post: Yoriz
  [PyQt] Collect entry from textline Widget via UI file mart79 3 139 Aug-05-2019, 01:40 PM
Last Post: Denni
  How to add a widget to a QtWindow after it has been created LavaCreeperKing 4 231 Jul-13-2019, 07:58 PM
Last Post: LavaCreeperKing
  [Tkinter] Adding space between Notebook tabs Columbo 4 264 Jul-10-2019, 10:46 PM
Last Post: Columbo
  Pygobject Menu And Notebook? loss 0 129 Jun-29-2019, 12:49 PM
Last Post: loss
  GUI Tkinter Widget Positions punksnotdead 3 276 Jun-12-2019, 06:06 PM
Last Post: Yoriz
  The PMW Notebook Widget MattSA 6 432 Jun-03-2019, 07:24 PM
Last Post: Larz60+
  [Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? chano 6 395 May-27-2019, 04:24 PM
Last Post: Yoriz
  [WxPython] Adding a Window to a Button wxPython ShashankDS 4 336 Apr-23-2019, 06:53 PM
Last Post: Yoriz

Forum Jump:


Users browsing this thread: 1 Guest(s)