ludwig smith
ludwig smith

Reputation: 21

How do I fill empty space using grid system in tkinter?

I have 5 buttons with 3 of them in the first row and 2 in the second row. How do I fill the white space that is left? I tried the following:

Date_play.grid(row=3,column=1,columnspan=2,sticky="w")
File_play.grid(row=3,column=2,columnspan=2,sticky="e")

Thanks in advance.

screenshot

Upvotes: 0

Views: 1803

Answers (3)

FrainBr33z3
FrainBr33z3

Reputation: 1105

There are multiple ways to do this. One of the easier ones is to play around with the Grid Layout.

Try this:

import tkinter as tk


root = tk.Tk()
# create a grid of 2x6
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
for i in range(6):
    root.columnconfigure(i, weight=1)

# by playing around with columnspan, you can get the layout that you need
button1 = tk.Button(root, text='1')
button1.grid(row=0, column=0, columnspan=2, sticky=tk.NSEW)
button2 = tk.Button(root, text='2')
button2.grid(row=0, column=2, columnspan=2, sticky=tk.NSEW)
button3 = tk.Button(root, text='3')
button3.grid(row=0, column=4, columnspan=2, sticky=tk.NSEW)
button4 = tk.Button(root, text='4')
button4.grid(row=1, column=0, columnspan=3, sticky=tk.NSEW)
button5 = tk.Button(root, text='5')
button5.grid(row=1, column=3, columnspan=3, sticky=tk.NSEW)

root.mainloop()

Upvotes: 1

parvat raj
parvat raj

Reputation: 56

try this:

    Date_play.grid(row=3,column=0,columnspan=2,sticky="w")
    File_play.grid(row=3,column=2,columnspan=1,sticky="e")

grid system does not allow to fill half the grid. best to do is make a new frame without a highlighted boarder, place them just below the row 2 in that frame add your two button.

    bottonframe = Frame(root)
    bottoframe.grid(row = 3, columnspan=3, width = <as per your window>)
    Date_play = Button(buttonframe,<your button config>)  
    File_play = Button(buttonframe,<your button config>)
    Date_play.grid(row=1,column=1,sticky="w")
    File_play.grid(row=1,column=2,sticky="e")

and then little bit adjustment.

Upvotes: 0

Eladtopaz
Eladtopaz

Reputation: 1054

You can try and add space to the text in each button. If the text is something like:

Date Module

Just expand it like this: Date Module It will make the button be bigger.

Also, you can use the width attribute of each button:

date_module_button.configure(width=...)

In both of the options, you need to play with it, add more/less space, add more/less width, until you are happy with the size of the button.

Upvotes: 0

Related Questions