📜  如何在 Tkinter 的 Text 小部件中设置标签大小?

📅  最后修改于: 2022-05-13 01:55:44.244000             🧑  作者: Mango

如何在 Tkinter 的 Text 小部件中设置标签大小?

先决条件: Tkinter

Python为开发 GUI(图形用户界面)提供了多种选择。在所有 GUI 方法中,Tkinter 是最常用的方法。这是一个标准的Python接口,附带的Python Tk的GUI工具包。 Python with Tkinter 是创建 GUI 应用程序的最快、最简单的方法。使用 Tkinter 创建 GUI 是一项简单的任务。

在本文中,我们将学习如何在 Tkinter 中使用Python设置文本小部件中的选项卡大小。这里的标签大小是指按下标签按钮后将打印多少个空格。让我们看看做同样的事情的方法。

让我们一步一步地理解实现:

  • 创建一个普通的 Tkinter 窗口
Python3
# Import Module
from tkinter import *
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("400x400")
  
# Execute Tkinter
root.mainloop()


Python3
# Add Text Box
text = Text(root)
text.pack()


Python3
# Set Font
font = tkfont.Font(font=text['font'])
  
# Set Tab size
tab_size = font.measure('        ')
text.config(tabs=tab_size)


Python3
# Import Module
from tkinter import *
import tkinter.font as tkfont
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("400x400")
  
# Add Text Box
text = Text(root)
text.pack()
  
# Set Font
font = tkfont.Font(font=text['font'])
  
# Set Tab size
tab_size = font.measure('           ')
text.config(tabs=tab_size)
  
# Execute Tkinter
root.mainloop()


输出:

  • 添加文本小部件

句法:

T = Text(root, bg, fg, bd, height, width, font, ..)

蟒蛇3

# Add Text Box
text = Text(root)
text.pack()
  • 设置标签大小

这里将使用字体包中的tkfont()方法

蟒蛇3

# Set Font
font = tkfont.Font(font=text['font'])
  
# Set Tab size
tab_size = font.measure('        ')
text.config(tabs=tab_size)

下面是实现:

蟒蛇3

# Import Module
from tkinter import *
import tkinter.font as tkfont
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("400x400")
  
# Add Text Box
text = Text(root)
text.pack()
  
# Set Font
font = tkfont.Font(font=text['font'])
  
# Set Tab size
tab_size = font.measure('           ')
text.config(tabs=tab_size)
  
# Execute Tkinter
root.mainloop()

输出:

调整标签大小 tkinter