Search code examples
pythontkintergrid-layout

Tkinter widgets not resizing as expected


Sorry if it's an easy fix - I'm new to tkinter (and graphical applications in general), and on my way up the learning curve.

I have a simple window in grid layout, with a canvas and a few labels to the right of it. They're positioned and sized correctly - but when I resize the window to the right, they DO expand over, but only about half way (i.e., for every 2 pixels to the right I expand the window, they widgets only expand by 1). I have their respective columns weighted, as well as the parent frame's column weighted. I'm not sure how to remedy this, and haven't found anything similar through Googling. Below I've posted my code, and screenshots of the widgets when the application is launched vs. when I resize.

CODE

from tkinter import *

root = Tk()
root.title("title")

mainframe = Frame(root)
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)


canvas = Canvas(mainframe, width=800, height=800)
canvas.grid(row=1, column=1, rowspan=3)

info_label = Label(mainframe, text='Info Label', bg='white', relief='solid', borderwidth=1)
info_label.grid(row=1, column=2, columnspan=2)

chatbox = Label(mainframe, text='Welcome to the application.', bg='white', relief='solid', borderwidth=1)
chatbox.grid(row=2, column=2, columnspan=2, sticky=(N,S,E,W))

chat_entry = Entry(mainframe)
chat_entry.grid(row=3, column=2)

chat_send = Button(mainframe, text='Send')
chat_send.grid(row=3, column=3)

mainframe.rowconfigure(1, weight=1)
mainframe.rowconfigure(2, weight=1)
mainframe.columnconfigure(2, weight=1)

root.columnconfigure(1, weight=1)

root.mainloop()

SCREENSHOTS

enter image description here enter image description here


Solution

  • The only positioning command that resizes according to the window size is pack(). As known, you have the following functions:

    pack()
    grid()
    place()
    

    By the way, I recommend using the place function and use this:

        window.resizable(width=False, height=False)
    

    This function prevent from the client the ability to resize the tkinter window.