NexMat
NexMat

Reputation: 93

Tkinter grid layout won't expand

I'm trying to design sidebar which should looke like most of Google's sidebar menu (like in Inbox or Play Music, browser version, for example). I'm using the grid layout but it won't expand. I've looked it up a little bit and despite adding sticky = "nesw" and rowconfigure(0, weigth = 1), the frame on the right won't expand and fill the root.

from tkinter import *
from tkinter import ttk

buttons = ["Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button"]  # List of contacts

root = Tk()
root.title('MyApp v0.1')
root.geometry('800x600')
#root['bg'] = "white"

# Side menu
frame = Frame(root, background = "white")
for i in range(len(buttons)):
    Button(frame, background = "white", text = buttons[i], command = None).grid(row = i, column = 0)
frame.rowconfigure(0, weight = 1)
frame.grid(row = 0, column = 0, sticky = "nesw")

sep = ttk.Separator(root, orient = "vertical")
sep.rowconfigure(0, weight = 1)
sep.columnconfigure(1, weight = 1)
sep.grid(row = 0, column = 1, padx = 5, sticky = "nesw")

root.mainloop()

Here's what I get. My buttons on the left are supposed to be on a white background Frame. The Frame and the separator should go down to the bottom of the application window.

Result display

Thanks in advance.

Upvotes: 4

Views: 9119

Answers (1)

Kevin
Kevin

Reputation: 76194

If you want the Buttons' container to stretch all the way from the top of the window to the bottom, try calling rowconfigure on the root. And if you want the buttons to be evenly distributed, rather than calling frame.rowconfigure(0, weight = 1), call frame.rowconfigure for each row of the frame from 0 to len(buttons).

from tkinter import *
from tkinter import ttk

buttons = ["Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button"]  # List of contacts

root = Tk()
root.title('MyApp v0.1')
root.geometry('800x600')
#root['bg'] = "white"
root.rowconfigure(0, weight=1)

# Side menu
frame = Frame(root, background = "white")
for i in range(len(buttons)):
    Button(frame, background = "white", text = buttons[i], command = None).grid(row = i, column = 0)
    frame.rowconfigure(i, weight = 1)
frame.grid(row = 0, column = 0, sticky = "nesw")

sep = ttk.Separator(root, orient = "vertical")
sep.rowconfigure(0, weight = 1)
sep.columnconfigure(1, weight = 1)
sep.grid(row = 0, column = 1, padx = 5, sticky = "nesw")

Result:

enter image description here

Upvotes: 8

Related Questions