使 tkinter 文本小部件适合窗口
问题描述:
我正在制作一个文本编辑器,它的主要小部件是一个文本小部件,供用户实际输入文本.当用户调整窗格大小时,我需要使文本小部件适合窗口.我通过使小部件变大来作弊,但这只是一个临时解决方案,让我在寻找解决方案的同时处理其他部分.如何让 Text 小部件自动调整大小以适应窗口?
I'm making a text editor whose main widget is a Text widget for the user to actually enter text. I need to make the text widget fit to the window when the user resizes the pane. I kind of cheated by making the widget huge, but that's just a makeshift solution to let me work on other parts while I look for a solution. How can I make the Text widget automatically resize to fit the window?
答
使用pack来放置widget,expand设置为True
,fill设置为BOTH
.例如:
Use pack to place the widget, with expand set to True
and fill set to BOTH
. Ex:
from tkinter import *
root=Tk()
text=Text(root)
text.pack(expand=True, fill=BOTH)
root.mainloop