-1

I am trying to place a label on top of a Frame, which is inside a 'Notebook' tab.

But when I run this code, the label always ends up in the center of the frame.

from tkinter import *
from tkinter import ttk

class Window:
    def __init__(self,master):
        self.master = master
        master.title("Title")
        master.resizable(1,1)
        master.geometry('500x400')

        self.load_UI()

    def load_UI(self):

        self.tabOptions = ttk.Notebook(self.master )
        self.tab1 = Frame(self.tabOptions, padx=130, pady=80, bg='white')

        self.tabOptions.add(self.tab1, text="Add Files")    
        self.tabOptions_AddFile()

        self.tabOptions.pack()

    def tabOptions_AddFile(self):

        self.label = Label(self.tab1, text="Why is this in the center of the frame?")
        self.label.grid(row=0, column=0)

root = Tk()
app = Window(root)
root.mainloop()

I tried to place the label using: pack(), grid(), place(). I also tried to place the label before adding the frame to the Notebook but it still looks the same :(

I am using python 3 btw.

Sergio Loza
  • 16
  • 1
  • 2
  • As you did not make the notebook to fill the available space of root window, it will just resize itself to fit all its children, ie the frame `self.tab1` and the tabs on top. Try changing `self.tabOptions.pack()` to `self.tabOptions.pack(fill='both', expand=1)`. – acw1668 Jun 15 '20 at 06:04

1 Answers1

1

This is because your Frame is padded in the line self.tab1 = Frame(self.tabOptions, padx=130, pady=80, bg='white'). Your Frame is here:

Just remove padx=130, pady=80 and all works. But to keep the size of tabOptions, replace

self.tabOptions.pack()

by

self.tabOptions.pack(fill=BOTH, expand=True)
D_00
  • 1,440
  • 2
  • 13
  • 32