如何在 Tkinter 的文本框中插入文本?
Tkinter 支持两种类型的文本编辑器——Entry 小部件和 Text 小部件。Tkinter 文本小部件用于创建文本编辑器,可以在其中编辑、添加或删除文本。我们可以使用 Text(parent) 构造函数创建文本小部件。为了为文本小部件插入默认文本,我们可以在文本小部件声明后使用 insert(INSERT, "text_to_insert") 或 insert("1.0", "text_to_insert") 方法。
示例
在此示例中,我们将文本插入文本框的开头。
#Import the required Libraries from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter frame win.geometry("750x250") #Create a Text Box text= Text(win, width= 50, height= 30, background= "gray71",foreground="#fff",font= ('Sans Serif', 13, 'italic bold')) #Insert the text at the begining text.insert(INSERT, "Write Something About Yourself") text.pack(expand= 1, fill= BOTH) win.mainloop()
输出
运行以上代码,在窗口中显示文本框。
在给定的输出中,默认文本最初设置在文本框的开头。
广告