0

I've tried for several hours to build a Frame that fills the window, and which contains a Listbox on one side and three buttons evenly spaced: how I want the layout to be

Instead, with the current code I have, the result looks like this

Can anyone point out what I'm doing wrong, and how I can change the code to get it to the way I want it to be?

Current code: `

# Creates window
root = Tk()
root.title("MyMemory")
root.geometry("600x600")
root["bg"] = "sienna1"

# Frames the window
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe.pack(fill=BOTH, expand = True)

# Creates Listbox of existing subjects, from .txt files in directory of 
program
subjects = Listbox(mainframe)
for file in glob.glob("*.txt"):
    subjects.insert(0,file.split('.')[0])
subjects.grid(column = 0, row = 1, rowspan = 3)

# Creates a button for selecting the subject in the Listbox
select_subject = Button(mainframe, text = 'Select subject', command = 
lambda: subject_menu(subjects, root))
select_subject.grid(column = 1, row = 0, sticky = (N))

# Creates a "make new subject" button
make_subject = Button(mainframe, text = 'Make new subject', command = 
lambda: new_subject(root))
make_subject.grid(column = 1, row = 1, sticky = (N))

# Creates an "import subject" button
import_sub = Button(mainframe, text = 'Import a subject', command = 
import_subject)
import_sub.grid(column = 1, row = 2, sticky = (N))

# Creates an "exit program" button
#close = Button(root, text = 'Exit program', command = ))
#close.pack(pady=20, padx = 20)    


# Starts the loop
root.mainloop()`
user7628
  • 7
  • 4

1 Answers1

1

This related question may be useful to you: tkinter gui layout using frames and grid1

Applying this to your code you might get something like this:

import glob
from Tkinter import *


root = Tk()
root.title('Model Definition')
root.geometry('{}x{}'.format(600, 600))

# create all of the main containers
center = Frame(root, bg='gray2', width=590, height=590, padx=3, pady=3)

# layout all of the main containers
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)

center.grid(row=1, sticky="nsew")

# create the center widgets
center.grid_rowconfigure(0, weight=1)
center.grid_columnconfigure(1, weight=1)

ctr_left = Frame(center, bg='blue', width=295, height=590, padx=50, pady=50)
ctr_right = Frame(center, bg='green', width=295, height=590, padx=70, pady=50)

ctr_left.grid(row=0, column=0, sticky="ns")
ctr_right.grid(row=0, column=2, sticky="ns")

# create right sub widgets
ctr_right.grid_rowconfigure(1, weight=1)
ctr_right.grid_columnconfigure(0, weight=1)

right_top = Frame(ctr_right, bg='yellow', width=295, height=194)
right_center = Frame(ctr_right, bg='red', width=295, height=194)
right_bottom = Frame(ctr_right, bg='purple', width=295, height=194)

right_top.grid(row=0, column=0, sticky="ew")
right_center.grid(row=1, column=0, sticky="ew")
right_bottom.grid(row=2, column=0, sticky="ew")

#
# make the Listbox and buttons
#

# Creates Listbox of existing subjects, from .txt files in directory of program
subjects = Listbox(ctr_left,
                   width=32,
                   height=30)
for this_file in glob.glob("*.txt"):
    subjects.insert(0, this_file.split('.')[0])
subjects.grid(column=0, row=0, sticky="e")

# Creates a button for selecting the subject in the Listbox
select_subject = Button(right_top,
                        text='Select subject',
                        command=lambda: subject_menu(subjects, root),
                        width=20,
                        height=3)
select_subject.grid(column=0, row=0, sticky="e")

# Creates a "make new subject" button
make_subject = Button(right_center,
                      text='Make new subject',
                      command=lambda: new_subject(root),
                      width=20,
                      height=3)
make_subject.grid(column=0, row=0, sticky="e")

# Creates an "import subject" button
import_sub = Button(right_bottom,
                    text='Import a subject',
                    command=import_subject,
                    width=20,
                    height=3)
import_sub.grid(column=0, row=0, sticky="e")

root.mainloop()

Note that adding a width or height to the Listbox or Buttons uses a measurement of characters rather than pixels. This code also adds colors to make it easier to see where the frames are going, and the result is as shown here:

enter image description here