简体   繁体   English

动态调整 tkinter window 上的所有小部件的大小,因为 window 调整大小

[英]Dynamically resize all widgets on a tkinter window as the window resizes

I'm trying to make a tkinter application(my first real code) but I'm having a little trouble with the interface.我正在尝试制作一个 tkinter 应用程序(我的第一个真实代码),但我的界面有点麻烦。 I want to get my widgets to all dynamically resize as the window resizes.我想让我的小部件随着 window 调整大小而动态调整大小。 I managed to make my background image resize itself to fit the entire window size, as the window is resized by the user, by writing a function that resizes the PIL image.我设法使我的背景图像自行调整大小以适应整个 window 大小,因为 window 由用户调整大小,方法是编写调整 PIL 图像大小的 function。 Here is the function:这是 function:

    def resizeimage(self,event) :
        width, height = self.winfo_width(), self.winfo_height()
        image = self.bg_image.resize((width,height))
        self.image1 = ImageTk.PhotoImage(image)
        self.bg_label.config(image = self.image1)

I've bound this function to the label that displays the background image like this:我已将此 function 绑定到显示如下背景图像的 label :

        self.bg_image = Image.open("project_pics\\start_pg.png")
        bg_image = ImageTk.PhotoImage(self.bg_image)
        self.bg_label = Label(self,image=bg_image)
        self.bg_label.image = bg_image
        self.bg_label.bind('<Configure>',self.resizeimage)
        self.bg_label.grid(sticky="nwse")

here, self.bg_image is the image to be displayed as background and self.bg_label is the label that displays the image.这里, self.bg_image是要显示为背景的图像, self.bg_label是显示图像的 label。 The reason why I wrote this code to resize the background image was because, when I ran my application on a different computer, since the new one had a different screen size, the background image didn't fit the size of the screen as I would have liked it to.我编写此代码来调整背景图像大小的原因是,当我在另一台计算机上运行我的应用程序时,由于新计算机具有不同的屏幕尺寸,所以背景图像与我的屏幕尺寸不匹配喜欢它。 This function solved that problem but then I noticed a new problem.这个 function 解决了这个问题,但后来我注意到了一个新问题。 The other widgets on the screen remained the same size .屏幕上的其他小部件保持相同的大小 So when the window was shrunk, you could see that all the widgets remained the same size and just bunched together on the smaller screen.因此,当 window 缩小时,您可以看到所有小部件都保持相同大小,只是在较小的屏幕上聚集在一起。 I know that I could write some function similar to resizeimage() to resize all the other widgets in proportion to the window and similar to the background image, bind all the widgets on the screen to such a function but it seems very tedious and not very efficient. I know that I could write some function similar to resizeimage() to resize all the other widgets in proportion to the window and similar to the background image, bind all the widgets on the screen to such a function but it seems very tedious and not very高效的。 I'm looking for a more dynamic solution.我正在寻找更动态的解决方案。 This is how The window looks normally:这就是 window 的正常外观:

原始窗口

This is how it looks when The window is shrunk:这是 window 缩小时的样子:

缩小窗口

My question is this.我的问题是这个。 Is there any way I could dynamically resize all the widgets on the window so that they don't bunch up like in image 2 but resize themselves in proportion to the window so that they look just like image 1 when shrunk, just smaller?有什么办法可以动态调整 window 上的所有小部件的大小,这样它们就不会像图 2 中那样聚集,而是按 window 的比例调整自己的大小,这样它们在缩小时看起来就像图 1,只是更小? I think a similar question was asked here but the solution given didn't quite answer the entire question.我认为这里提出了一个类似的问题,但给出的解决方案并没有完全回答整个问题。

I did something similar some time ago to adjust font size according to label size, and this was not tedious and could be adapted for adjusting several labels.前段时间我做了类似的事情来根据 label 大小调整字体大小,这并不乏味,可以适应调整多个标签。

def resize(event):
    # Adjust font according to Label size
    h, w = display.winfo_height(), display.winfo_width()
    if h/w > 183/605:           # Label text h/w ratio is 183/605
        h = int(w*183/605)
    label_font.config(size=h)

display is the name of the Label widget. display是 Label 小部件的名称。 label_font is a tkinter.font object. label_fonttkinter.font object。 Then I simply bind the function to <Configure> .然后我简单地将 function 绑定到<Configure> Also, the text was always the same number of characters.此外,文本始终是相同数量的字符。

Do you think this may be of use?你觉得这可能有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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