Python3 Tkinter Grid not expanding to entire frame

Question:

I have this code:

import tkinter as tk
from tkinter import ttk

app_title = "Wordle-Klon" # Appens titel.
app_font = ("Arial", 20) # Applikation-vid typsnitt.
app_background_color = "yellow" # Bakgrundsfärg för appen.

window_width = 1000
window_height = 800

class App(tk.Tk): 
    def __init__(self): 
        super().__init__() 
        self.geometry('500x500') 
        self.initUI()

    def initUI(self):
        self.title = tk.Label(text=app_title, anchor="c", pady=20, font=app_font, bg='yellow', fg="black")
        self.title.pack()

        self.letterFrame = tk.Frame(self, bg="Blue")
        self.letterFrame.pack(fill="both", expand=True, padx=20, pady=20)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.createLetterSquares()

    def createLetterSquares(self):
        self.letters, self.rows = 1, 1

        for i in range(5*5):
            if self.letters == 6: 
                self.rows += 1
                self.letters = 1

            self.frame = tk.Frame()
            self.label = tk.Label(text="("+str(self.rows)+", "+str(self.letters)+")", bg="red", padx=10, pady=10)
            self.label.pack(in_=self.frame, anchor="c")

            self.frame.grid(in_=self.letterFrame, row=self.rows, column=self.letters, sticky=tk.NSEW)
            self.frame.grid_columnconfigure(self.letters, weight=1, uniform="True")
            self.frame.grid_rowconfigure(self.rows, weight=1, uniform="True")
            self.letters += 1 

if __name__ == "__main__": 
    app = App() # Skapa ett app objekt. 
    app.mainloop() # Loopa appen. 

I get a result that looks like:
Result of current code

How can I make the grid that is inside the frame, expand to fill the entire thing, rather than only using the minimum space necessary? I tried playing around with grid weights at the root (self.app) but it did not make any difference.

Asked By: NewDeveloper404

||

Answers:

Your main issue was configuring row/column weights on the wrong frame. In this case the correct frame was letterFrame.

I have updated your code to include a list of all label objects so you can also have a reference point for how you can load all your labels into a list for later use.

SO is really for asking a specific question about a specific problem. If you have multiple questions its best to ask one at a time in a new post after solving the main issue.

Keep in mind that some behavior may differ in MAC vs PC. I am testing this code on PC. Since Tkinter takes a lot of its display from the OS environment your window may look slightly different from mine.

Let me know if you have any questions.

import tkinter as tk


app_title = "Wordle-Klon"  # Appens titel.
app_font = ("Arial", 20)  # Applikation-vid typsnitt.
app_background_color = "yellow"  # Bakgrundsfärg för appen.


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('500x500')
        tk.Label(text=app_title, anchor="c", pady=20, font=app_font, bg='yellow', fg="black").pack()
        self.letterFrame = tk.Frame(self, bg="Blue")
        self.letterFrame.pack(fill="both", expand=True, padx=20, pady=20)
        self.label_list = []
        self.number_of_columns = 5
        self.number_of_labels = 25
        self.createLetterSquares()

    def createLetterSquares(self):
        row = 0
        col = 0
        for i in range(self.number_of_labels):
            self.letterFrame.grid_columnconfigure(col, weight=1, uniform="True")
            self.letterFrame.grid_rowconfigure(row, weight=1, uniform="True")
            lbl = tk.Label(self.letterFrame, text=f"({row+1}, {col+1})", bg="red", padx=10, pady=10)
            self.label_list.append([lbl, row, col])
            self.label_list[-1][0].grid(row=row, column=col, sticky=tk.NSEW)
            col += 1
            if col == self.number_of_columns:
                col = 0
                row += 1
        print(self.label_list)


if __name__ == "__main__":
    app = App()  # Skapa ett app objekt.
    app.mainloop()  # Loopa appen.

Results:

enter image description here

Answered By: Mike – SMT