Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto resize tkinter window to fit all widgets

Tags:

python

tkinter

I'd like to have a dynamic sizing for the main tkinter window so that when you add a new widget, you don't have to go change the size of the window. Instead, the main window will account for that widget size and automatically increase its height/width to fit that widget in the window.

masterWindow = Tk()

#Main Window min width
self.window_width = screen_width * .01
self.window_height = screen_height * .04

#Window startst in center of screen
self.window_start_x = (screen_width/2)
self.window_start_y = (screen_height/2)
masterWindow.geometry("%dx%d+%d+%d" % (self.window_width, self.window_height, self.window_start_x, self.window_start_y))
self.buttonsFrame.pack(side = TOP)
button_width = 13
button_height = 2

#A simple dict that stores script name strings
for script in SCRIPTS.keys():
    #Remove script extension
    script_name = script.split(".")[0]
    button_width = 13
    button_height = 2
    BUTTONS[script] = Button(self.buttonsFrame, text = script, width = button_width, height = button_height, justify = LEFT, wraplength = 100, command = lambda s = script: self.runScript(s))
    BUTTONS[script].grid(row = self.row, column = self.col)
    self.update()

    #Increment row and col and set new window size
    if self.col == 2:
        self.col = 0
        self.row += 1
        reached_max_width = True
    else:
        self.col += 1
        if not reached_max_width:
            self.window_width += button_width * 13
    self.window_height = self.buttonsFrame.winfo_height() * (self.row*3)
    masterWindow.geometry("%dx%d" % (self.window_width, self.window_height))


def runScript(self, script):
    print(script)
like image 725
E. Oregel Avatar asked Jun 20 '18 19:06

E. Oregel


People also ask

How do I change the default tkinter window size?

To set a specific size to the window when using Python tkinter, use geometry() function on the Tk() class variable. where width and height should be replaced with integers that represent the width and height of the window respectively.

What does resizable do in tkinter?

resizable() method is used to allow Tkinter root window to change it's size according to the users need as well we can prohibit resizing of the Tkinter window. So, basically, if user wants to create a fixed size window, this method can be used.

What is the maximum size of a window in tkinter?

In the same way, maxsize(1200,988) depicts the maximum width, and the height of the Tkinter window must be 1200 and 988, respectively.


2 Answers

You are explicitly setting the window to a specific size with this line of code:

masterWindow.geometry("%dx%d" % (self.window_width, self.window_height))

Because you're doing that, tkinter will keep the window to that size even when you add widgets. If you want it to auto-size you need to remove that line of code.

Also, if the user resizes the window, tkinter will try to honor that size. If you want it to automatically resize when adding new widgets you'll need to remove the window size so that tkinter can compute it. You can do that by calling masterWindow.geometry("").

like image 198
Bryan Oakley Avatar answered Oct 30 '22 17:10

Bryan Oakley


I found the issue. The problem was that I was setting an initial Width/Height for the Tkinter mainWindow in the block of code:

#Main Window min width
self.window_width = screen_width * .01
self.window_height = screen_height * .04
masterWindow.geometry("%dx%d+%d+%d" % (self.window_width, self.window_height, self.window_start_x, self.window_start_y))

I removed that size definition and setting from the geometry so the code looks like this, and it worked:

masterWindow.geometry("+%d+%d" % (self.window_start_x, self.window_start_y))

Now the window resizes automatically to account for all the buttons.

like image 3
E. Oregel Avatar answered Oct 30 '22 17:10

E. Oregel