Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter - resize widgets evenly in a window

I have a small test Python app as I am learning both Python and Tkinter and I am trying to figure out how to evenly resize a grid of labels in a window. I would like to make a large grid of different colored squares so I'm using labels with a background color set to make the squares. I would like to resize the squares automatically when the user expands the window out and then have them resize and scale down to all be the same size as the window is decreased in size.

Something like this:

Default "square" size. Opens up at this size by default.

That resizes and scales evenly like this:

Size when the window is expanded in size.

In other words: each label should all scale uniformly as the window scales. I'm not even sure if I'm using the correct terms with "window" and "widget". But I'm placing labels on a gui.

Test code I'm using to try to get this to work:

    import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        label = Tkinter.Label(self,anchor="center",bg="green")
        label.grid(column=0,row=0,sticky='EW')

        label2 = Tkinter.Label(self,anchor="center",bg="black")
        label2.grid(column=1,row=0,sticky='EW')

        label3 = Tkinter.Label(self,anchor="center",bg="red")
        label3.grid(column=2,row=0,sticky='EW')

        label4 = Tkinter.Label(self,anchor="center",bg="purple")
        label4.grid(column=0,row=1,sticky='EW')

        label5 = Tkinter.Label(self,anchor="center",bg="blue")
        label5.grid(column=1,row=1,sticky='EW')

        label6 = Tkinter.Label(self,anchor="center",bg="yellow")
        label6.grid(column=2,row=1,sticky='EW')


        self.grid_columnconfigure(0,weight=0)

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title("Test App")
    app.mainloop()
like image 863
user1340081 Avatar asked Jul 09 '14 02:07

user1340081


People also ask

What is resizable in Tkinter?

Tkinter initially creates a resizable window for every application. Let us suppose that we want to make a non-resizable window in an application. In this case, we can use resizable(height, width) and pass the value of height=None and width=None.

How do you make a window resizable in Python?

resizable() method in Tkinter | Python So, basically, if user wants to create a fixed size window, this method can be used. Arguments to be passed: -> In resizable() method user can pass either positive integer or True, to make the window resizable. -> To make window non-resizable user can pass 0 or False.

Which function is used to set the size of the Tkinter window?

In Tkinter, minsize() method is used to set the minimum size of the Tkinter window. Using this method a user can set window's initialized size to its minimum size, and still be able to maximize and scale the window larger.


2 Answers

Give all rows and columns the same non-zero weight.

For example:

self.grid_columnconfigure(0,weight=1)
self.grid_columnconfigure(1,weight=1)
self.grid_columnconfigure(2,weight=1)
self.grid_rowconfigure(0,weight=1)
self.grid_rowconfigure(1,weight=1)
like image 108
Bryan Oakley Avatar answered Oct 05 '22 02:10

Bryan Oakley


Completing the answer provided by Bryan Oakley, the code for solving it in python 3 is the following.

Note that one option to manage the proportion for which the window resizes is setting the weight parameters for functions grid_columnconfigure(1,weight=1) and grid_rowconfigure(1,weight=1) to different values.

import tkinter

class simpleapp_tk(tkinter.Tk):
    def __init__(self,parent):
        tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        label = tkinter.Label(self,anchor="center",bg="green")
        label.grid(column=0,row=0,sticky='NSEW')

        label2 = tkinter.Label(self,anchor="center",bg="black")
        label2.grid(column=1,row=0,sticky='NSEW')

        label3 = tkinter.Label(self,anchor="center",bg="red")
        label3.grid(column=2,row=0,sticky='NSEW')

        label4 = tkinter.Label(self,anchor="center",bg="purple")
        label4.grid(column=0,row=1,sticky='NSEW')

        label5 = tkinter.Label(self,anchor="center",bg="blue")
        label5.grid(column=1,row=1,sticky='NSEW')

        label6 = tkinter.Label(self,anchor="center",bg="yellow")
        label6.grid(column=2,row=1,sticky='NSEW')


        self.grid_columnconfigure(0,weight=1)
        self.grid_columnconfigure(1,weight=1)
        self.grid_columnconfigure(2,weight=1)
        self.grid_rowconfigure(0,weight=1)
        self.grid_rowconfigure(1,weight=1)


if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title("Test App")
    app.mainloop()
like image 20
aturegano Avatar answered Oct 05 '22 02:10

aturegano