简体   繁体   English

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

[英]Dynamically resize widget's when resizing a window in tkinter

Is there any way to dynamically resize a widget's size while resizing a window in tkinter?有没有办法在调整 tkinter 中的 window 的大小时动态调整小部件的大小?

Here's the code:这是代码:

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()

when I resize my window, the width and height of the widgets stay the same .当我调整 window 的大小时,小部件的宽度和高度保持不变

what I want is to dynamically resize the widget's inside my window according to the window's size.我想要的是根据窗口的大小动态调整我的 window 内的小部件的大小。

Is there any way to achieve this in tkinter?有没有办法在 tkinter 中实现这一点?

It would be great if anyone could help me out.如果有人可以帮助我,那就太好了。

Try this raw source code example:试试这个原始源代码示例:

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()

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

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