Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

user interface - 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. 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:

Default "square" size. Opens up at this size by default.

That resizes and scales evenly like this:

Size when the window is expanded in size.

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...