简体   繁体   中英

Auto resize tkinter window to fit all widgets

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)

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("") .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM