问题描述
我有一个小测试Python应用程序,因为我正在学习Python和Tkinter,我正试图弄清楚如何在窗口中均匀调整标签网格的大小。我想制作一个不同颜色的正方形的大网格,因此我使用的背景颜色设置为正方形。我想在用户扩大窗口的大小时自动调整方块大小,然后让它们调整大小并缩小为与缩小窗口大小相同的大小。
像这样:
调整大小并像这样均匀缩放:
换句话说:每个标签都应该随着窗口的缩放而一致地缩放。我甚至不确定我是否用窗口和小部件来使用正确的术语。但是我在gui上放置了标签。
我正在使用的测试代码试图使其发挥作用:
import Tkinter
class simpleapp_tk(Tkinter.Tk):
def __init __(self,parent):
Tkinter.Tk .__ init __(self,parent)
self.parent = parent
self.initialize()
def初始化(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()
赋予所有行和列相同的非零权重。
例如:
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)
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:
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.
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)
这篇关于Python Tkinter - 在窗口中均匀调整窗口小部件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!