为什么我的网格权重不影响 window 的布局

Why do my grid weights not affect the layout of window

我正在为带有 GUI 的程序编写一些代码,其中一部分是这段代码:

        self.chatbox = Text(self.chatframe)
        self.messagebox = Entry(self.sendframe, textvariable=self.message)
        self.sendbutton = Button(self.sendframe, text="Send", font=self.font(12))
        self.chatframe.grid_rowconfigure(0, weight=1)
        self.sendframe.grid_columnconfigure(0, weight=19)
        self.sendframe.grid_columnconfigure(1, weight=1)
        self.chatbox.grid(row=0, column=0)
        self.messagebox.grid(row=0, column=0)
        self.sendbutton.grid(row=0, column=1)
        self.sendframe.grid(row=1, column=0)
        self.mainframe.grid_columnconfigure(0, weight=1)
        self.mainframe.grid_columnconfigure(1, weight=9)
        self.mainframe.grid_rowconfigure(0, weight=1)
        self.menu.grid(row=0, column=0)
        self.chatframe.grid(row=0, column=1)

然而,当我 运行 它时,它总是只占用一些 space 而没有像我预期的那样填满屏幕。任何帮助表示赞赏。

完整代码:

from tkinter import *
import tkinter.messagebox
import os

class ServerInfo():
    def __init__(self):
        self.network = ""
        self.host = False
        self.name = StringVar()
        self.name.set("")


class App(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master    
        self.master.state("zoomed")
        #self.master.minsize(1200, 600)
        self.master.title("Chatroom App")
        self.serverinfo = ServerInfo()

        self.buffer_length = 2048
        self.message = StringVar()
        self.message.set("")

        self.mainmenu = Frame(self.master)
        self.localframe = Frame(self.master)
        self.publicframe = Frame(self.master)
        self.mainframe = Frame(self.master)
        self.chatframe = Frame(self.mainframe)
        self.sendframe = Frame(self.chatframe)
        self.choiceframe = Frame(self.master)
        self.inputframe = Frame(self.master)

        self.create_widgets()

    def font(self, size):
        return ("Alef", size)

    def create_widgets(self):
        self.title = Label(self.mainmenu, text="The Chatroom App", font=self.font(40))
        self.localbutton = Button(self.mainmenu, text="Local Chatrooms", font=self.font(16), command=self.go_to_local)
        self.publicbutton = Button(self.mainmenu, text="Public Chatrooms", font=self.font(16), command=self.go_to_public)
        self.exitbutton = Button(self.mainmenu, text="Exit", font=self.font(16), command=self.master.destroy)
        self.title.pack(fill=BOTH)
        self.localbutton.pack(pady=20, fill=BOTH)
        self.publicbutton.pack(pady=20, fill=BOTH)
        self.exitbutton.pack(side=BOTTOM, fill=BOTH)

        self.instruction = Label(self.choiceframe, text="Would you like to host a server or search for available servers", font=self.font(26))
        self.hostbutton = Button(self.choiceframe, text="Host server", font=self.font(32), command=self.host_input)
        self.joinbutton = Button(self.choiceframe, text="Join server", font=self.font(32), command=self.join_input)
        self.backbutton = Button(self.choiceframe, text="Back", font=self.font(32), command=self.back_from_choice)
        self.instruction.pack(pady=20, fill=BOTH)
        self.hostbutton.pack(pady=10, fill=BOTH)
        self.joinbutton.pack(pady=10, fill=BOTH)
        self.backbutton.pack(pady=20, fill=BOTH)

        self.instruction2 = Label(self.inputframe, text="", font=self.font(18))
        self.server_input = Entry(self.inputframe, textvariable=self.serverinfo.name)
        self.continuebutton = Button(self.inputframe, text="", font=self.font(28), command=self.take_input)
        self.backbutton2 = Button(self.inputframe, text="Back", font=self.font(28), command=self.back_from_input)
        self.instruction2.pack(pady=10, fill=BOTH)
        self.server_input.pack(pady=40, fill=BOTH)
        self.continuebutton.pack(pady=10, fill=BOTH)
        self.backbutton2.pack(pady=20, fill=BOTH)

        self.menu = Canvas(self.mainframe, bg="Black")
        self.chatbox = Text(self.chatframe)
        self.messagebox = Entry(self.sendframe, textvariable=self.message)
        self.sendbutton = Button(self.sendframe, text="Send", font=self.font(12))
        self.chatframe.grid_rowconfigure(0, weight=1)
        self.sendframe.grid_columnconfigure(0, weight=19)
        self.sendframe.grid_columnconfigure(1, weight=1)
        self.chatbox.grid(row=0, column=0)
        self.messagebox.grid(row=0, column=0)
        self.sendbutton.grid(row=0, column=1)
        self.sendframe.grid(row=1, column=0)
        self.mainframe.grid_columnconfigure(0, weight=1)
        self.mainframe.grid_columnconfigure(1, weight=9)
        self.mainframe.grid_rowconfigure(0, weight=1)
        self.menu.grid(row=0, column=0)
        self.chatframe.grid(row=0, column=1)

        self.mainmenu.pack()

    def back_from_choice(self):
        self.choiceframe.forget()
        self.mainmenu.pack()
        window.update()

    def back_from_input(self):
        self.inputframe.forget()
        self.choiceframe.pack()

    def take_input(self):
        self.inputframe.forget()
        self.mainframe.pack(fill=BOTH)

    def go_to_local(self):
        self.serverinfo.network = "local"
        self.mainmenu.forget()
        self.choiceframe.pack()
        window.update()

    def go_to_public(self):
        self.serverinfo.network = "public"
        tkinter.messagebox.showinfo("Message from the developer", "This feature is still under development")

    def host_input(self):
        self.serverinfo.host = True
        self.instruction2.config(text="Type in the name of your server. When the server is created, a server ID will show in the top left. Share this to people who want to join the server")
        self.continuebutton.config(text="Host Server")
        self.choiceframe.forget()
        self.inputframe.pack()

    def join_input(self):
        self.serverinfo.host = False
        self.instruction2.config(text="Type in the server ID of the server you want to join")
        self.continuebutton.config(text="Join Server")
        self.choiceframe.forget()
        self.inputframe.pack()


    def host_server(self):
        pass

    def join_server(self):
        pass

    def write_message_to_screen(self, data):
        print(data)
  
    def encode_id(self, server_id):
        return server_id

    def decode_id(self, server_id):
        return server_id

权重仅影响 grid 如何分配 额外 space 一旦所有内容都已布局,它不会描述小部件的整体相对大小。

您似乎也没有使用 sticky 属性,因此 space 可能已分配给小部件,但您并未要求它们伸展以填充 space送给他们。