Rishabh
Rishabh

Reputation: 53

How to create multiple tabs in tkinter from different classes in python

from tkinter import *
from tkinter import ttk

class App(Frame):
    def __init__(self,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
       self.notebook = ttk.Notebook()
       self.add_tab()
       self.notebook.grid(row=0)

    def add_tab(self):
        tab = Area(self.notebook)
        tab2 = Volume(self.notebook) 
        self.notebook.add(tab,text="Tag")
        self.notebook.add(tab2,text="Tag2")


class Area(Frame):
   def __init__(self,name,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
       self.label = Label(text="Hi This is Tab1")
       self.label.grid(row=1,column=0,padx=10,pady=10)
       self.name = name

class Volume(Frame):
   def __init__(self,name,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
       self.label = Label(text="Hi This is Tab2")
       self.label.grid(row=1,column=0,padx=10,pady=10)
       self.name = name

my_app = App()

The volume class label in overwriting the label of Area class in both the tabs how i can solve this problem and how i can add different stuff of classes in different tabs.

Upvotes: 2

Views: 2418

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386382

You need to make the widgets in each tab be a child of the tab frame. You aren't specifying the parent or master for the labels, so they are going into the root window.

Take notice of the use of self in the last line of this code:

class Area(Frame):
   def __init__(self,name,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
       self.label = Label(self, text="Hi This is Tab1")

Upvotes: 1

Related Questions