以像素为单位指定 Tkinter 文本框的尺寸


你可以使用 place(**option) 几何管理器通过指定文本框的尺寸来设置其位置。在一个框架内实例化一个文本框会使文本框在整个应用程序窗口中保持独立性。然后我们使用 place() 几何管理器来分配文本框在窗口内的宽度和高度。像素决定了文本框在窗口中的定位精确度。因此,place() 几何管理器提供了一个网格系统,我们可以在其中将任何文本框放置在特定位置。

示例

# Import required libraries
from tkinter import *
from tkinter import ttk
from lorem_text import lorem

# Create an instance of tkinter frame
win= Tk()

# Set the window size
win.geometry("700x350")

# Add a Text widget and insert some dummy text
text= Text(win, wrap= WORD, font= ('Courier 15 bold'))
text.insert(END,lorem.sentence())
text.place(x=10, y= 10, width= 400, height= 300)

win.mainloop()

输出

当我们运行上面的代码时,一个文本框会显示在窗口中,其中包含一些虚拟文本。文本框的尺寸可以通过更改 place() 几何管理器中 xywidthheight 的值来更新。

更新于:2021 年 6 月 7 日

3K+ 阅读量

启动您的 职业生涯

完成本课程获得认证

开始学习
广告
© . All rights reserved.