Home > database >  Resizing ttk.frame of a ttk notebook, frame defined in a other class than the notebook
Resizing ttk.frame of a ttk notebook, frame defined in a other class than the notebook

Time:03-24

I have a main class for my gui and I added a ttk.NoteBook after a label:

class MainApplication(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('1000x500')
        self.configure(background='#F0F8FF')

        #TOP LABEL

        load = Image.open("my_image")
        load = load.resize((200, 67), Image.ANTIALIAS)
        self.render = ImageTk.PhotoImage(load)

        self.Label_top = tk.Label(self, image=self.render, compound=tk.LEFT, text="TOOL")
        self.Label_top.pack()


        #--Notebook---------

        self.notebook = ttk.Notebook(self)

        self.Page1 = Page1(self.notebook)
        self.Page2 = Page2(self.notebook)
        self.Page3 = Page3(self.notebook)
        self.Page4 = Page4(self.notebook)

        self.notebook.add(self.Page1, text='PAGE1')
        self.notebook.add(self.Page2, text='PAGE2')
        self.notebook.add(self.Page3, text='PAGE3')
        self.notebook.add(self.Page4, text='PAGE4')



        self.notebook.pack(fill='x', side=TOP)
        #expand=True create empty space between my top label          and my notebook, even with side=TOP

And I defined each frame in a class like this :

class Page1(ttk.Frame):
    def __init__(self, container):
        super().__init__()

        self.(width=400, height=280) #Error message

        self.pack(expand=True) #Doesn't work

Do you know how can I expand my frame for that it fills my page and pack the notebook just after my top label

CodePudding user response:

I think this will do what you want. I've incorporated most of the things @Bryan Oakley mentioned in his answer except I also added a BasePage class and derived all the other Page classes from it. This was done to provide a place to put code that would otherwise need to be repeated each of the subclasses.

I also changed some of your variable names to conform to PEP 8 Naming Conventions.

import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *


class BasePage(ttk.Frame):
    def __init__(self, container):
        super().__init__(container, width=400, height=280)
        classname = type(self).__name__
        tk.Label(self, text=f'Welcome to {classname}').place(relx=0.5, rely=0.25, 
                                                             anchor=CENTER)

class Page1(BasePage):
    def __init__(self, container):
        super().__init__(container)

class Page2(BasePage):
    def __init__(self, container):
        super().__init__(container)

class Page3(BasePage):
    def __init__(self, container):
        super().__init__(container)

class Page4(BasePage):
    def __init__(self, container):
        super().__init__(container)


class MainApplication(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('1000x500')
        self.configure(background='#F0F8FF')

        #--Notebook---------

        self.notebook = ttk.Notebook(self)

        self.page1 = Page1(self.notebook)
        self.page2 = Page2(self.notebook)
        self.page3 = Page3(self.notebook)
        self.page4 = Page4(self.notebook)

        self.notebook.add(self.page1, text='Page1')
        self.notebook.add(self.page2, text='Page2')
        self.notebook.add(self.page3, text='Page3')
        self.notebook.add(self.page4, text='Page4')

        self.notebook.pack(expand=True, fill=BOTH)


app = MainApplication()
app.mainloop()

CodePudding user response:

I see three problems.

First, each "page" needs to be a child of the notebook. You do that by making sure the notebook is passed to the __init__ of the frame:

class Page1(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)

Second, you need to not call pack on the page. self.notebook.add is already adding the frame to the notebook. So, remove the line self.pack(expand=True) from each page.

Third, self.(width=400, height=280) needs to be self.configure(width=400, height=280)

  • Related