Kerialstraz
Kerialstraz

Reputation: 45

Making a Label in Tkinter fill the entire cell

I want my Widgets to fill the entire space provided by a cell in tkinter. What I am currently trying is:

root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=3)
root.columnconfigure(2, weight=3)
root.columnconfigure(3, weight=3)

root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
root.rowconfigure(2, weight=3)

font_1 = ("Helvetica", 22)

chessboard = Canvas(root, background="#ffdead")
player_1_field = Label(root, text="Player 1", anchor=W, background="grey", font=font_1)
player_2_field = Label(root, text="Player 2", anchor=W, background="grey", font=font_1)
player_1_pieces_captured = Canvas(root, background="#ff0000", height=100)
player_2_pieces_captured = Canvas(root, background="#ff0000", height=100)
timer_field = Label(root, text="Timer", anchor=W, background="grey", font=font_1)
move_history_label = Label(root, text="History", anchor=W, background="grey", font=font_1)
move_history_field = Listbox(root, background="grey", font=font_1)

chessboard.grid(row=2, column=1, columnspan=3)
player_1_field.grid(row=0, column=1, sticky=W)
player_2_field.grid(row=0, column=3, sticky=W)
player_1_pieces_captured.grid(row=1, column=1, sticky=W)
player_2_pieces_captured.grid(row=1, column=3, sticky=W)
timer_field.grid(row=0, column=2, rowspan=2)
move_history_label.grid(row=0, column=0, sticky=W)
move_history_field.grid(row=1, column=0, rowspan=2, sticky=W)

The result is something like https://i.sstatic.net/WidcI.jpg My desired result is something like https://i.sstatic.net/0FI33.jpg (rough sketch)

The problem I face is that I don't know how to get the size of a given column/row and then padx/pady by that amount. Any help, even if it's just a link to resources, would be greatly appreciated!

Upvotes: 1

Views: 2247

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386275

If you want a widget to fill the space it has been allotted, set the sticky parameter to "nsew" (or the constant NSEW) so that it "sticks" to every side of the cell.

chessboard.grid(row=2, column=1, columnspan=3, sticky=NSEW)
player_1_field.grid(row=0, column=1, sticky=NSEW)
player_2_field.grid(row=0, column=3, sticky=NSEW)
player_1_pieces_captured.grid(row=1, column=1, sticky=NSEW)
player_2_pieces_captured.grid(row=1, column=3, sticky=NSEW)
timer_field.grid(row=0, column=2, rowspan=2, sticky=NSEW)
move_history_label.grid(row=0, column=0, sticky=NSEW)
move_history_field.grid(row=1, column=0, rowspan=2, sticky=NSEW)

screenshot

Upvotes: 1

Derek
Derek

Reputation: 2244

I've included two functions that I find very useful when using grid manager.

flexx will make grid objects resizable

grid a convenience function that helps to remove data from the code.

Here is your code using flexx and grid

from tkinter import *

def flexx( o, r = 0, c = 0, rw = 1, cw = 1 ):
    '''flexx( o(TkContainer), r=0, c=0, rw=1, cw=1 )'''
    if r != None:
        o.rowconfigure( r, weight = rw )
    if c != None:
        o.columnconfigure( c, weight = cw )

def grid( r = 0, c = 0, s = 'nsew', rs = 1, cs = 1 ):
    '''grid( r=0, c=0, s=nsew, rs=1, cs=1 )'''
    return dict(
        row = r, column = c, rowspan = rs, columnspan = cs, sticky = s )

root= Tk()

flexx( root )
flexx( root, r = 1, c = 1, cw = 3 )
flexx( root, r = 2, c = 2, rw = 3, cw = 3 )
flexx( root, c = 3, cw = 3 )

font_1 = ("Helvetica", 22)

chessboard = Canvas(root, background = "#ffdead")
player_1_field = Label(root, text = "Player 1", anchor = W, background = "grey", font = font_1)
player_2_field = Label(root, text = "Player 2", anchor = W, background = "grey", font = font_1)
player_1_pieces_captured = Canvas(root, background = "#ff0000", height = 100)
player_2_pieces_captured = Canvas(root, background = "#ff0000", height = 100)
timer_field = Label(root, text = "Timer", anchor = W, background = "grey", font = font_1)
move_history_label = Label(root, text = "History", anchor = W, background = "grey", font = font_1)
move_history_field = Listbox(root, background = "grey", font = font_1)

chessboard.grid(grid(r = 2, c = 1, cs = 3 ))
player_1_field.grid(grid( c = 1 ))
player_2_field.grid(grid( c = 3 ))
player_1_pieces_captured.grid(grid( r = 1, c = 1 ))
player_2_pieces_captured.grid(grid( r = 1, c = 3 ))
timer_field.grid(grid( c = 2, rs = 2 ))
move_history_label.grid(grid( ))
move_history_field.grid(grid( r = 1, rs = 2 ))

mainloop()

Upvotes: 0

Related Questions