自动调整 tkinter 窗口大小以适合所有小部件 [英] Auto resize tkinter window to fit all widgets

查看:73
本文介绍了自动调整 tkinter 窗口大小以适合所有小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 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)

推荐答案

您使用以下代码行明确地将窗口设置为特定大小:

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))

因为您这样做,即使您添加小部件,tkinter 也会将窗口保持在该大小.如果您希望它自动调整大小,则需要删除该行代码.

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.

此外,如果用户调整窗口大小,tkinter 将尝试遵守该大小.如果您希望它在添加新小部件时自动调整大小,您需要删除窗口大小,以便 tkinter 可以计算它.您可以通过调用 masterWindow.geometry("") 来实现.

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

这篇关于自动调整 tkinter 窗口大小以适合所有小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆