简体   繁体   English

Python3 Tkinter 网格未扩展到整个框架

[英]Python3 Tkinter Grid not expanding to entire frame

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:我得到的结果如下:当前代码的结果

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.我尝试在根( self.app )处使用网格权重,但没有任何区别。

Your main issue was configuring row/column weights on the wrong frame.您的主要问题是在错误的框架上配置行/列权重。 In this case the correct frame was letterFrame .在这种情况下,正确的框架是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.我已更新您的代码以包含所有 label 对象的列表,因此您还可以参考如何将所有标签加载到列表中以供以后使用。

SO is really for asking a specific question about a specific problem. SO实际上是针对特定问题提出特定问题。 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.请记住,MAC 与 PC 中的某些行为可能有所不同。 I am testing this code on PC.我正在 PC 上测试此代码。 Since Tkinter takes a lot of its display from the OS environment your window may look slightly different from mine.由于 Tkinter 从操作系统环境中获取了大量显示,因此您的 window 可能看起来与我的略有不同。

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:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM