Python Forum
[Tkinter] How to create multilple tabs in tkinter from different classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to create multilple tabs in tkinter from different classes
#1
Why label of volume class is overwriting the label of area class in both tabs what's the solution to that help please

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()
Reply
#2
(Jul-14-2017, 03:32 PM)Rishav Wrote: Why label of volume class is overwriting the label of area class in both tabs what's the solution to that help please

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

I think it has something to do with where you are placing each label as it seems you are telling them to be in the same place on screen and Area is in the same place as volume. Two things to try are to alter the row of one of the labels and the other is to use Area.lift after assigning tab2
I think one of these might help to see what is happening.
As I have said in another thread I haven't been using classes so I could be entirely wrong.
Reply
#3
your frames are not within the notebook.
Was this your intent?
Also, you have three separate frames, in classes that are never instantiated,
only defined.
Somewhere you have to create an instance of each class.

If you haven't yet committed yourself to tkinter, you may want to take a look at wxpython.
The new phoenix version for python 3 is extremely easy to use. I have done a lot of work in tkinter
and it's not the easiest to use, especially the geometry.

At least take a look at some of the samples, and if you download the tarball, you can cd to the demo directory
and run the entire demo with:
python demo.py
each demo comes complete with code, which can be displayed at the click of a button from the demo app.
see: https://wxpython.org/Phoenix/docs/html/gallery.html
for a sample of what you can do with it.
In addition, I have a tutorial here: https://python-forum.io/Thread-wxPython-...ep-by-step
if you need help with windows install.
I haven't installed on Linux, but it should be pretty straight forward.
Reply
#4
Hey, i've registered just to answer, fixed your code:

#!/usr/local/bin/python3
from tkinter import *
from tkinter import ttk
 
class App(Tk):
    def __init__(self,*args,**kwargs):
       Tk.__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(self, 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(self, text="Hi This is Tab2")
       self.label.grid(row=1,column=0,padx=10,pady=10)
       self.name = name
 
my_app = App()
my_app.mainloop()
Not sure if it was really missing, or you've just forgot to copy it, but the mainloop() was missing from the end. Also, you only forgot to add 'self' from Label. This way you've put them under the main frame, instead of under their respective tab-frames. This way it works like you've designed it (i've tested it, works).
Reply
#5
Answering this post may help others, so thanks for that. However I wonder if you looked at the post date of the original post, which was one year ago.
Reply
#6
(Jul-10-2018, 01:34 PM)Larz60+ Wrote: Answering this post may help others, so thanks for that. However I wonder if you looked at the post date of the original post, which was one year ago.

Most people (including myself) stumble upon these while browsing the internet for some answer to a problem they've encountered (to me it was about how to define the individual frames of a notepad as separate classes, and then a function definition that could replace tabs within the notepad by pressing a button, etc - this one did not help with that, but could still help others). So as long as there is no Tcl/tk 9 or Python 4 introduced that could render this code obsolete, it remains recent. Besides, 1 year is a relatively short time anyway.

And if anyone's interested, i've figured out how to write multiple layers of frames, and define a controller function within the "root frames" to replace them (or the notebook tabs) via commands.

#!/usr/local/bin/python3
import tkinter as tk
from tkinter import *
from tkinter import ttk

# Root class to create the interface and define the controller function to switch frames
class RootApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(NoteBook)

# controller function
    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

# sub-root to contain the Notebook frame and a controller function to switch the tabs within the notebook
class NoteBook(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.notebook = ttk.Notebook()
        self.tab1 = Tab1(self.notebook)
        self.tab2 = Tab2(self.notebook)
        self.tab3 = Tab3(self.notebook)
        self.notebook.add(self.tab1, text="Tab1")
        self.notebook.add(self.tab2, text="Tab2")
        self.notebook.add(self.tab3, text="Tab3")
        self.notebook.pack()

# controller function
    def switch_tab1(self, frame_class):
        new_frame = frame_class(self.notebook)
        self.tab1.destroy()
        self.tab1 = new_frame
        
# Notebook - Tab 1
class Tab1(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self._frame = None
        self.switch_frame(Tab1_Frame1)

    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

# first frame for Tab1
class Tab1_Frame1(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, text="this is a test - one")
        # button object with command to replace the frame
        self.button = Button(self, text="Change it!", command=lambda: master.switch_frame(Tab1_Frame2))
        self.label.pack()
        self.button.pack()

# second frame for Tab1
class Tab1_Frame2(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, text="it has been changed!")
        # and another button to change it back to the previous frame
        self.button = Button(self, text="Change it back!", command=lambda: master.switch_frame(Tab1_Frame1))
        self.label.pack()
        self.button.pack()

# Notebook - Tab 2
class Tab2(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, text="this is a test - two")
        self.label.pack()

# Notebook - Tab 3
class Tab3(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, text="this is a test - three")
        self.label.pack()

if __name__ == "__main__":
    Root = RootApp()
    Root.geometry("640x480")
    Root.title("Frame test")
    Root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Help create scrollbar in chatbot with tkinter on python fenec10 4 1,432 Aug-07-2023, 02:59 PM
Last Post: deanhystad
  Found buttonstate in tabs MacTommu 4 1,947 Sep-22-2021, 05:56 PM
Last Post: deanhystad
  TabError: inconsistent use of tabs and spaces in indentation hobbyist 3 4,844 Jun-15-2021, 10:38 PM
Last Post: davide73_italy
  Tkinter & classes Heyjoe 6 2,912 Feb-13-2021, 09:17 PM
Last Post: Heyjoe
  [Tkinter] Vertical Tabs Alignment in Tkinter muhammadasim 2 5,912 Oct-05-2020, 08:40 AM
Last Post: Larz60+
  Tkinter: Create an homepage look like PeroPuri 8 5,798 Jun-26-2020, 12:57 AM
Last Post: menator01
  Create image on a Toplevel using tkinter ViktorWong 3 7,745 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Need help please properly putting tabs within a PanedWindow JackMack118 2 3,308 Dec-08-2019, 03:02 PM
Last Post: balenaucigasa
  [PyQt] Drag items across tabs Alfalfa 5 4,687 Sep-01-2019, 11:58 PM
Last Post: Alfalfa
  [Tkinter] Adding space between Notebook tabs Columbo 4 4,425 Jul-10-2019, 10:46 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020