缩放 Tkinter 小部件

Cod*_*Cat 5 python grid tkinter

我正在使用 Tkinter 的“grid()”方法为我的 python 程序设计 GUI。

有没有办法让小部件随主窗口缩放?

这是一个简短的例子:

from Tkinter import *

master = Tk()

Label(master, text="This is a test").grid(row=0, column=0)

mytext1 = Text(master, width=30, height=5)
mytext1.grid(row=1, column=0)

mytext2 = Text(master, width=30, height=5)
mytext2.grid(row=3, column=0)

master.mainloop()
Run Code Online (Sandbox Code Playgroud)

我想做的是当主窗口的大小发生变化时,让小部件调整它们的大小。(对于如此小的 GUI,这没有问题,但是当有很多小部件时,这就变得可取了。)

任何帮助将不胜感激!

tyl*_*rjw 2

这里和上面一样,没有使用类。随着程序规模的增大,使用类会更简洁、更有意义。请注意,我将标签的权重设置为 0,这可以防止其扩展。您不必执行此操作,因为它是默认选项。这只是为了让您了解您可以做什么。

有关网格布局管理器的更多详细信息:http ://www.tkdocs.com/tutorial/grid.html

from Tkinter import *

master = Tk()

Label(master, text="This is a test").grid(row=0, column=0)

mytext1 = Text(master, width=30,height=5)
mytext1.grid(row=1, column=0, sticky="nsew")

mytext2 = Text(master, width=30,height=5)
mytext2.grid(row=2, column=0, sticky="nsew")

master.columnconfigure(0, weight=1)
master.rowconfigure(0, weight=0) # not needed, this is the default behavior
master.rowconfigure(1, weight=1)
master.rowconfigure(2, weight=1)

master.mainloop()
Run Code Online (Sandbox Code Playgroud)