首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >自动调整tkinter窗口大小以适合所有小部件

自动调整tkinter窗口大小以适合所有小部件
EN

Stack Overflow用户
提问于 2018-06-21 03:47:39
回答 2查看 20.9K关注 0票数 7

我想为tkinter主窗口设置一个动态大小,这样当你添加一个新的小部件时,你就不必去改变窗口的大小了。相反,主窗口将考虑到该小部件的大小,并自动增加其高度/宽度以适合窗口中的小部件。

代码语言:javascript
复制
        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)
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50955987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档