📜  如何使用 Tkinter 创建多行条目?

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

如何使用 Tkinter 创建多行条目?

Tkinter是Python用于开发 GUI 的库。它提供了各种用于开发 GUI(图形用户界面)的小部件。 tkinter 中的 Entry 小部件有助于获取用户输入,但它收集的输入仅限于一行文本。因此,要在 tkinter 中创建多行条目文本,有多种方法。

使用 tkinter 创建多行条目的方法:

  1. 使用文本小部件
  2. 使用 ScrolledText 小部件

方法 1:使用 Tkinter 文本小部件

文本小部件为用户提供多行文本区域。文本小部件实例是在文本类的帮助下创建的。它还用于显示文本行并允许编辑文本。

句法:



Text( root, optional parameters, ... )

示例 1:

Python3
import tkinter as tk
from tkinter import ttk
  
  
window = tk.Tk()
window.title("Text Widget Example")
window.geometry('400x200')
  
ttk.Label(window, text="Enter your comment :",
          font=("Times New Roman", 15)).grid(
  column=0, row=15, padx=10, pady=25)
  
# Text Widget
t = tk.Text(window, width=20, height=3)
  
t.grid(column=1, row=15)
  
window.mainloop()


Python3
import tkinter as tk
  
  
window = tk.Tk()
window.title("Text Widget with Scrollbar")
  
text = tk.Text(window, height=8, width=40)
scroll = tk.Scrollbar(window)
text.configure(yscrollcommand=scroll.set)
text.pack(side=tk.LEFT)
  
scroll.config(command=text.yview)
scroll.pack(side=tk.RIGHT, fill=tk.Y)
  
insert_text = """GEEKSFORGEEKS :
A Computer Science portal for geeks.
It contains well written, well thought
and well explained computer science and
programming articles, quizzes and
many more.
GeeksforGeeks realises the importance of programming practice in the field of
Computer Science.
That is why, it also provides an option of practicing problems.
This huge database of problems is created by programming experts.
The active team of GeeksforGeeks makes the learning process
interesting and fun.
"""
  
text.insert(tk.END, insert_text)
tk.mainloop()


Python3
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
  
  
root = tk.Tk()
  
root.title("ScrolledText Widget Example")
  
ttk.Label(root, text="ScrolledText Widget Example",
          font=("Times New Roman", 15)).grid(column=0, row=0)
ttk.Label(root, text="Enter your comments :", font=("Bold", 12)).grid
(column=0, row=1)
  
text_area = scrolledtext.ScrolledText(root, wrap=tk.WORD,
                                      width=40, height=8,
                                      font=("Times New Roman", 15))
  
text_area.grid(column=0, row=2, pady=10, padx=10)
  
# placing cursor in text area
text_area.focus()
root.mainloop()


输出:

多行文本小部件 tkinter

该示例的上述输出允许用户在多行中输入文本。但它不会显示用户输入的超出文本小部件高度的所有文本,即高度=3。由于它仅显示 3 行文本,因此对此类多行文本使用滚动条将解决问题。

示例 2:将 Scrollbar 添加到文本小部件

蟒蛇3

import tkinter as tk
  
  
window = tk.Tk()
window.title("Text Widget with Scrollbar")
  
text = tk.Text(window, height=8, width=40)
scroll = tk.Scrollbar(window)
text.configure(yscrollcommand=scroll.set)
text.pack(side=tk.LEFT)
  
scroll.config(command=text.yview)
scroll.pack(side=tk.RIGHT, fill=tk.Y)
  
insert_text = """GEEKSFORGEEKS :
A Computer Science portal for geeks.
It contains well written, well thought
and well explained computer science and
programming articles, quizzes and
many more.
GeeksforGeeks realises the importance of programming practice in the field of
Computer Science.
That is why, it also provides an option of practicing problems.
This huge database of problems is created by programming experts.
The active team of GeeksforGeeks makes the learning process
interesting and fun.
"""
  
text.insert(tk.END, insert_text)
tk.mainloop()

输出:



多行 tkinter 1

方法 2:使用 ScrolledText tkinter 小部件

我们可以直接使用 ScrolledText tkinter 小部件来让用户进行多行输入,而不是像示例 2 中看到的那样向文本小部件添加滚动条。当文本增加到超过 scrolledText 小部件的高度时,此小部件会自动添加滚动条。

例子:

蟒蛇3

import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
  
  
root = tk.Tk()
  
root.title("ScrolledText Widget Example")
  
ttk.Label(root, text="ScrolledText Widget Example",
          font=("Times New Roman", 15)).grid(column=0, row=0)
ttk.Label(root, text="Enter your comments :", font=("Bold", 12)).grid
(column=0, row=1)
  
text_area = scrolledtext.ScrolledText(root, wrap=tk.WORD,
                                      width=40, height=8,
                                      font=("Times New Roman", 15))
  
text_area.grid(column=0, row=2, pady=10, padx=10)
  
# placing cursor in text area
text_area.focus()
root.mainloop()

输出:

滚动文本 tkinter