Python Forum
Tkinter: multitab window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter: multitab window
#1
Hi, I'm trying to use multitab frame in my script. My plan is to create the pages I need in different .py then use this as main page to start my program

from tkinter import *
from Page1 import *
from Page2 import *
from Page3 import *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):
        tab1 = Instructions(self.notebook)
        tab2 = Mainframe(self.notebook)
        tab3 = Test(self.notebook)
        self.notebook.add(tab,text="p1")
        self.notebook.add(tab2,text="p2")
        self.notebook.add(tab3,text="p3")
  
  
class Page1(Frame):
   def __init__(self,name,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
        self.name = name
  
class Page2(Frame):
   def __init__(self,name,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
       self.name = name
       
class Page3(Frame):
   def __init__(self,name,*args,**kwargs):
       Frame.__init__(self,*args,**kwargs)
       self.name = name
  
my_app = App()
my_app.mainloop()
but when I try to make it run all pages appear in different windows and only after I close them finally appear the window with my tab (that are empty). How can I fix this?

This is my first page code:
from openpyxl import *
from tkinter import *
from datetime import datetime
wb = load_workbook('C:\\Users\\MyPC\\Desktop\\\\exc.xlsx')

sheet = wb.active
datetimeFormat = '%d-%m-%Y'
class Page1(Frame):

   def __init__(self,master,*args,**kwargs):
       global msg, inv_cnt
       excel()
       inv_cnt=1

       Frame.__init__(self,master,*args,**kwargs)

   #label
   #entry

class App(Tk):
   def __init__(self):
       global value
       Tk.__init__(self)
       self.title("Fake News Survey")
       self.geometry('400x500')
       self.resizable(0,0)
               # create and pack a Mainframe window
       Mainframe(self).place(x=0,y=0,height=880,width=880)
       self.mainloop()
and my second one
from tkinter import *
window=Tk()


S = Scrollbar(window)
T = Text(window, padx = 10, pady = 10, spacing1 = 10, spacing2 = 1, bd = 2)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, expand=True, fill=BOTH)
S.config(command=T.yview)
T.config(yscrollcommand=S.set, background="LIGHT GRAY")
T.tag_configure('big', font=('times', 15, 'bold'), justify='center')
T.tag_configure('normal', font=('times', 12))
T.insert(END,'Instructions\n', 'big')
quote = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin id augue tempor lectus ornare mollis at sed felis."""
T.insert(END, quote, 'normal')
T.config(state=DISABLED)

window.title('PAGE2')
window.geometry("400x500")
window.mainloop()
Basically I don't know how to format my page to be able to fit in the multitab and how to call it in my main page. Also how could I disable a tab and then able it after the use of a button?
Thank to whoever could help me Smile
Reply
#2
You need to write the pages using a common API. This can be as simple an init and a way to get the page and maybe the title.

Notobook module
import tkinter as tk
from tkinter import ttk

import page1
import page2

class Paper(tk.Tk):
    def __init__(self):
       super().__init__()
       self.title("Daily Paper")
       
       self.notebook = ttk.Notebook(self)
       self.notebook.pack(fill='both')
       self.add_page(page1.Page1)
       self.add_page(page2.Page2)
   
    def add_page(self, page_cls):
        page = page_cls(self.notebook)
        self.notebook.add(page.frame, text=page.title)
   
my_app = Paper()
my_app.mainloop()
Page module
import tkinter as tk

headlines = ('Covid Cured', 'Economy Rebounds', 'Global Warming Reversed', 'World Peace')
 
class Page1:
    def __init__(self, parent):
        self.title = 'Headlines'  # Tab title in notebook
        self.frame = tk.Frame(parent) # Tab conents
        for i, text in enumerate(headlines):
            story = tk.Label(self.frame, text=text)
            story.grid(row=i, column=0)
May as well have two pages:
import tkinter as tk

sports = ('Hockey', 'Baseball', 'Soccer', 'Football', 'Basketball')
 
class Page2:
    def __init__(self, parent):
        self.title = 'Sports'
        self.frame = tk.Frame(parent)
        for i, text in enumerate(sports):
            story = tk.Label(self.frame, text=text)
            story.grid(row=i, column=0)
The notebook will automatically resize to fit the largest page, and the main window automatically resizes to fit the notebook.

It should be pretty easy to automate filling the notebook with pages. You could put all the page modules in a folder and the notebook module would open scan the folder, import the page modules, and add the page. The contents of the pages themselves could be automated.
Reply
#3
Thank you for your help! I'm having a little bit of trouble understending how to implementing the code/ using class.
This is how I've changed the code of my page2
class page2:
    def __init__(self, parent):
        self.title = 'Head'
        self.frame = tk.Frame(parent)
        for i, text in enumerate(head):
            story = tk.Label(self.frame, text=text)
            story.grid(row=i, column=0)

        self.scrollbar = Scrollbar(self)
        self.text = Text(self, padx = 10, pady = 10, spacing1 = 10, spacing2 = 1, bd = 2)
        self.scrollbar.pack(side=RIGHT, fill=Y)
        self.text.pack(side=LEFT, expand=True, fill=BOTH)
        self.scrollbar.config(command=self.text.yview)
        self.text.config(yscrollcommand=self.scrollbar.set, background="LIGHT GRAY")
        self.text.tag_configure('big', font=('times', 15, 'bold'), justify='center')
        self.text.tag_configure('normal', font=('times', 12))
        self.text.insert(END,'Instructions\n', 'big')
        quote = """Lorem ipsum dolor sit amet, consectetur adipiscing elit."""
        self.text.config(state=DISABLED)
And it's not working.But my even bigger problem is how should I change page1 class to make it works.
Thank you again for your help
Reply
#4
What do you mean by "not working". Does the code not run or don't you see the page in your notebook? Are you not getting a tab for the page? More information please.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 343 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 786 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,380 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,831 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,864 Apr-16-2022, 04:04 PM
Last Post: DBox
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,546 Feb-17-2022, 07:02 AM
Last Post: pymn
  [Tkinter] Tkinter Window Has no Title Bar gw1500se 4 2,795 Nov-07-2021, 05:14 PM
Last Post: gw1500se
  "tkinter.TclError: NULL main window" Rama02 1 5,782 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  function in new window (tkinter) Dale22 7 4,964 Nov-24-2020, 11:28 PM
Last Post: Dale22
  Scrollable big image in a window (TKinter) Prospekteur 3 4,409 Sep-14-2020, 03:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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