為 tkinter Text 視窗小部件新增高階功能


Tkinter 文字視窗小部件不僅僅是一個多行文字編輯器。它適用於需要使用者互動的許多應用程式。

為了配置文字視窗小部件,tkinter 提供了許多函式和方法。我們可以為文字視窗小部件新增的一些樣式特性包括:更改字體系列、背景色、前景顏色、新增和選擇特定文字、更改字型大小等。

示例

在此示例中,我們將瞭解如何為特定文字小部件新增不同的功能,

#Import tkinter library
from tkinter import *

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

#Set the geometry of tkinter window
win.geometry("750x250")

#Create a text widget
text= Text(win, width= 50, height=14)

#Set default text for text widget
text.insert(INSERT, "Tkinter is a Python Library which is used to create various types of applications.")
text.insert(END, "Learning Tkinter is Awesome!!")

#Select Text by adding tags
text.tag_add("start", "1.0","1.7")
text.tag_configure("start", background= "OliveDrab1", foreground= "black")

#Configure the Text Style
text.configure(font=('Times New Roman',15))
text.pack()
win.mainloop()

輸出

執行以上程式碼將顯示一個包含文字視窗小部件的視窗,該視窗具有一些功能和屬性。

更新於: 15-Apr-2021

585 瀏覽量

開啟您的職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.