0

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
Jacob
  • 41
  • 9
  • Please provide a [mcve], and explain the difference between what the code is doing and what you want it to do. – Bryan Oakley Sep 07 '20 at 20:37
  • Please try to reduce this code down. That's way too much code, and there are a lot of widgets that seem unrelated to the problem. We don't want "full" code, we want a _minimal_ but complete example. – Bryan Oakley Sep 07 '20 at 20:50
  • You Mayer warnt to look at this https://stackoverflow.com/a/63536506/13629335 – Thingamabobs Sep 08 '20 at 04:28

1 Answers1

1

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Ok I understand that now but, my issue is that the widgets won't won't increase in size to fill the extra size, which I now understand is wrong but what is the correct way to do this? – Jacob Sep 07 '20 at 20:42
  • @Jacob: it's impossible for us to answer that. You haven't given us code to reproduce the behavior. `grid` certainly will allow the widgets to resize the way you want. If it's not doing so, you've done something wrong. Without being able to reproduce the problem we can only guess. – Bryan Oakley Sep 07 '20 at 20:45
  • @Jacob: we don't want all the code, we want a _minimal_ example. – Bryan Oakley Sep 07 '20 at 20:50
  • The `sticky` attribute is what I was looking for, thanks – Jacob Sep 15 '20 at 19:16