Turns out your problem is a simple matter of size. The LabelFrame
is basically sitting at a zero size because you have not added anything to the frame. So to correct this add width and height to resize the LabelFrame
and boom problem solved.
Once you start filling those LabelFrame
's with widgets you will no longer need the size formatting.
import tkinter as tk
from tkinter import ttk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
notebook = ttk.Notebook(parent)
notebook.add(Typ1(notebook), text='TAB1')
notebook.add(Typ2(notebook), text='TAB2')
notebook.pack()
class Typ1(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
shell_frame=tk.LabelFrame(self, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
shell_frame.grid(row=0,column=0,padx=5,pady=5)
class Typ2(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
shell_frame=tk.LabelFrame(self, text="Sample Label Frame", padx=5,pady=5, width=200, height=200)
shell_frame.grid(row=0,column=0,padx=5,pady=5)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Results:

You could also simply inherit from LabelFrame
instead of Frame
in your class's to get the same results with less.
Example:
import tkinter as tk
from tkinter import ttk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
notebook = ttk.Notebook(parent)
notebook.add(Typ1(notebook), text='TAB1')
notebook.add(Typ2(notebook), text='TAB2')
notebook.pack()
class Typ1(tk.LabelFrame):
def __init__(self, parent, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
self.grid(row=0,column=0,padx=5,pady=5)
class Typ2(tk.LabelFrame):
def __init__(self, parent, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
self.grid(row=0,column=0,padx=5,pady=5)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
To answer your comments below here is how you would integrate the frame swapping with a login page.
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(LoginPage)
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.grid(row=0, column=0)
class LoginPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
tk.Label(self, text="Username: ").grid(row=0, column=0)
tk.Label(self, text="Password: ").grid(row=1, column=0)
self.un_entry = tk.Entry(self)
self.un_entry.grid(row=0, column=1)
self.pw_entry = tk.Entry(self)
self.pw_entry.grid(row=1, column=1)
self.pw_entry.bind("<Return>", self.check_login)
tk.Button(self, text="Login", command=self.check_login).grid(row=2, column=0)
def check_login(self, event=None):
if self.un_entry.get() == "Mike" and self.pw_entry.get() == "pass":
self.master.switch_frame(MainApplication)
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
notebook = ttk.Notebook(parent)
notebook.add(Typ1(notebook), text='TAB1')
notebook.add(Typ2(notebook), text='TAB2')
notebook.grid(row=0, column=0)
class Typ1(tk.LabelFrame):
def __init__(self, parent, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
self.grid(row=0,column=0,padx=5,pady=5)
class Typ2(tk.LabelFrame):
def __init__(self, parent, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
self.grid(row=0,column=0,padx=5,pady=5)
if __name__ == "__main__":
App().mainloop()