在 tkinter 中调整窗口大小时动态调整小部件的大小

时间:2021-01-10 13:24:52

标签: python tkinter

在 tkinter 中调整窗口大小时,有没有办法动态调整小部件的大小?

代码如下:

from tkinter import *

root = Tk()
root.geometry("777x575")

text = Text(root , width = 75 , height = 25)
text.grid(row = 0 , column = 0)

scrollbar = Scrollbar(root , command = text.yview)
scrollbar.grid(row = 0 , column = 1 , sticky = N+S+E+W)

text.config(yscrollcommand = scrollbar.set)

button = Button(root , text = "Sample Button")
button.grid(row = 1 , column = 0 , pady = 20)

mainloop()

当我调整窗口大小时,小部件的宽度和高度保持 same

我想要的是根据窗口的大小动态调整窗口内小部件的大小。

有没有办法在 tkinter 中实现这一点?

如果有人能帮助我就好了。

1 个答案:

答案 0 :(得分:0)

试试这个原始源代码示例:

from tkinter import *

root = Tk()
root.geometry("777x575")
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=2)

root.rowconfigure(0, weight=1)

text = Text(root, width = 95 , height = 95)
text.grid(column=0, row=0, ipadx=500, ipady=10, sticky="NSEW")

scrollbar = Scrollbar(text, orient=VERTICAL)
scrollbar.pack(fill=Y, side=RIGHT)

button = Button(root, text = "Sample Button")
button.grid(column=0, row=1, ipadx=500, ipady=10, sticky="NSEW")

mainloop()