Python Tkinter - 在窗口中均匀调整小部件的大小 [英] Python Tkinter - resize widgets evenly in a window

查看:52
本文介绍了Python Tkinter - 在窗口中均匀调整小部件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型的 Python 测试应用程序,因为我正在学习 Python 和 Tkinter,我正在尝试弄清楚如何均匀地调整窗口中标签网格的大小.我想制作一个由不同颜色方块组成的大网格,所以我使用带有背景颜色的标签来制作方块.我想在用户展开窗口时自动调整正方形的大小,然后让它们调整大小并缩小到与窗口减小的大小相同的大小.

I have a small test Python app as I am learning both Python and Tkinter and I am trying to figure out how to evenly resize a grid of labels in a window. I would like to make a large grid of different colored squares so I'm using labels with a background color set to make the squares. I would like to resize the squares automatically when the user expands the window out and then have them resize and scale down to all be the same size as the window is decreased in size.

像这样:

像这样均匀地调整大小和缩放:

That resizes and scales evenly like this:

换句话说:每个标签都应该随着窗口的缩放而统一缩放.我什至不确定我是否使用了正确的术语窗口"和小部件".但我在 gui 上贴了标签.

In other words: each label should all scale uniformly as the window scales. I'm not even sure if I'm using the correct terms with "window" and "widget". But I'm placing labels on a gui.

我用来尝试让它工作的测试代码:

Test code I'm using to try to get this to work:

    import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        label = Tkinter.Label(self,anchor="center",bg="green")
        label.grid(column=0,row=0,sticky='EW')

        label2 = Tkinter.Label(self,anchor="center",bg="black")
        label2.grid(column=1,row=0,sticky='EW')

        label3 = Tkinter.Label(self,anchor="center",bg="red")
        label3.grid(column=2,row=0,sticky='EW')

        label4 = Tkinter.Label(self,anchor="center",bg="purple")
        label4.grid(column=0,row=1,sticky='EW')

        label5 = Tkinter.Label(self,anchor="center",bg="blue")
        label5.grid(column=1,row=1,sticky='EW')

        label6 = Tkinter.Label(self,anchor="center",bg="yellow")
        label6.grid(column=2,row=1,sticky='EW')


        self.grid_columnconfigure(0,weight=0)

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title("Test App")
    app.mainloop()

推荐答案

赋予所有行和列相同的非零权重.

Give all rows and columns the same non-zero weight.

例如:

self.grid_columnconfigure(0,weight=1)
self.grid_columnconfigure(1,weight=1)
self.grid_columnconfigure(2,weight=1)
self.grid_rowconfigure(0,weight=1)
self.grid_rowconfigure(1,weight=1)

这篇关于Python Tkinter - 在窗口中均匀调整小部件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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