简体   繁体   English

Python Tkinter - 在窗口中均匀调整窗口小部件的大小

[英]Python Tkinter - resize widgets evenly in a window

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. 我有一个小测试Python应用程序,因为我正在学习Python和Tkinter,我试图弄清楚如何在窗口中均匀调整标签网格。 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. 我想在用户展开窗口时自动调整方块大小,然后让它们调整大小并缩小到与窗口大小减小相同的大小。

Something like this: 像这样的东西:

默认“方形”大小。默认情况下以此大小打开。

That resizes and scales evenly like this: 这样调整大小和均匀缩放:

窗口大小扩大时的大小。

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. 但是我在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. 为所有行和列提供相同的非零权重。

For example: 例如:

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)

Completing the answer provided by Bryan Oakley , the code for solving it in python 3 is the following. 完成Bryan Oakley提供的答案 ,在python 3中解决它的代码如下。

Note that one option to manage the proportion for which the window resizes is setting the weight parameters for functions grid_columnconfigure(1,weight=1) and grid_rowconfigure(1,weight=1) to different values. 请注意,管理窗口调整大小的比例的一个选项是将函数grid_columnconfigure(1,weight=1)grid_rowconfigure(1,weight=1)的权重参数设置为不同的值。

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='NSEW')

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

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

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

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

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


        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)


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

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

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