Jacob
Jacob

Reputation: 41

Why do my grid weights not affect the layout of window

I am writing some code for a program with a GUI, and part of this is this code:

        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)

However when I run it, it always ends up only taking up some space and not filling the screen as I would expect. Any help appreciated.

Full Code:

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

Upvotes: 0

Views: 37

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385800

Weights only affect how grid allocates extra space once everything has been laid out, it doesn't describe the overall relative size of widgets.

You also don't seem to be using the sticky attribute, so space may be allocated to widgets but you aren't requesting that they stretch to fill the space given to them.

Upvotes: 1

Related Questions