Grasume
Grasume

Reputation: 106

Frame not expanding

Maybe this is just how Tkinter works but Im unsure. Currently I have the Main window with three frames laid out next to each other. Frame ContainerFrame is a master, then characterFrame and planetFrame are placed inside the ContainerFrame. The issue is or what I would like to happen is that the frames would fill up a set area of the window regardless of whether or not their is data/ widgets in them. Here is what I envision it to look like https://i.sstatic.net/RrWWz.jpg

from tkinter import *
from tkinter import ttk

MainWindow = Tk()
def mainWindow():
  MainWindow.option_add('*tearOff', False)
  MainWindow.title("Interface")
  MainWindow.geometry('800x600')
  menubar = Menu(MainWindow)
  MainWindow.config(menu = menubar)
  File = Menu(menubar)
  About = Menu(menubar)
  menubar.add_cascade(menu = File, label = "File")
  menubar.add_cascade(menu = About, label = "About")

def frameContainer():
  containerFrame = Frame(MainWindow)
  containerFrame.pack(anchor = "nw", side = "left", fill = "both", expand = False)
  scroller = Scrollbar(orient = "vertical")

  characterFrame = Frame(containerFrame, borderwidth="2", relief="sunken")
  characterFrame.pack(anchor = "nw", side = "left", expand = True)

  planetFrame =  Frame(containerFrame ,borderwidth="2", relief="sunken")
  planetFrame.pack(anchor = "nw", side = "right", expand = True)

  scroller = Scrollbar(orient = "vertical")
  scroller.pack(anchor = "e", side = "right", fill = "y", expand = False)

  characterLable = Button(characterFrame, text ="Characters")
  characterLable.pack()
  Label(characterFrame, text ="Test1").pack()
  Label(characterFrame, text ="Test2").pack()

  Label(planetFrame, text ="Test1").pack()
  Label(planetFrame, text ="Test2").pack()


mainWindow()
frameContainer()
MainWindow.mainloop()

Upvotes: 0

Views: 354

Answers (1)

figbeam
figbeam

Reputation: 7176

Normally you would use a frame to organize a widget with a scrollbar, but a frame is not scrollable. If you want to scroll an area containing other widgets the usual thing to do is to use a canvas.

Study this guide: Tkinter Scrollbar Patterns

Pack can be difficult to use and the only way I have found to overcome this is to keep trying. It's usually easier to see what you are doing if you let the different frames have different bg colors. Also I've taken the liberty to change some of your variable names as they do not give a hint as to what they are or are too similar to other names, eg. mainWindow and MainWindow.

I have added some padding to some widgets to make it look better.

from tkinter import *

root = Tk()

def create_main_window():
    root.option_add('*tearOff', False)
    root.title("Interface")
    root.geometry('400x300+800+50')

    menubar = Menu(root)
    root.config(menu = menubar)
    File = Menu(menubar)
    About = Menu(menubar)
    menubar.add_cascade(menu = File, label = "File")
    menubar.add_cascade(menu = About, label = "About")

def create_container_frame():
    container = Frame(root, bg='tan')
    container.pack(fill="both", expand=True)

    scroller = Scrollbar(container, orient="vertical")
    scroller.pack(side="right", fill="y")

    characterFrame = Frame(container, bd=2, relief="sunken", bg='thistle')
    characterFrame.pack(side="left", fill='y', padx=(10,0), pady=10)

    character_button = Button(characterFrame, text ="Characters")
    character_button.pack(padx=10, pady=(10,0))
    Label(characterFrame, text ="Test1").pack()
    Label(characterFrame, text ="Test2").pack()

    planetFrame = Frame(container ,bd=2, relief="sunken", bg='khaki')
    planetFrame.pack(side="left", fill='both', expand=True, padx=10, pady=10)

    Label(planetFrame, text="Test1").pack(pady=(10,0))
    Label(planetFrame, text="Test2").pack()

create_main_window()
create_container_frame()

root.mainloop()

Is this the layout you are aiming for?

Upvotes: 1

Related Questions