使用 Python 3 與 tkinter 在 Text 小元件中選擇所有文字
Tkinter 文字小元件用於建立包含多行使用者輸入的多行文字欄位。它擁有許多內建函式和方法,可以呼叫這些函式和方法在文字小元件上執行某些操作。相反,假設我們在文字小元件中編寫了一段上下文,並且如果我們想要選擇所有文字,那麼我們可以使用 tag_add(tag, range) 選擇文字並新增標籤以及 tag_configure(tag, options) 設定標籤屬性的樣式。
示例
#Import tkinter library
from tkinter import *
#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("750x200")
def select_text():
text.tag_add("start", "1.0","end")
text.tag_configure("start",background="black", foreground= "white")
#Create a Text Widget
text= Text(win)
text.insert(INSERT, "Python is an interpreted, high-level and generalpurpose
programming language. Python's design philosophy emphasizes
code readability with its notable use of significant indentation")
text.pack()
#Create a button to select all the text in the text widget
button= Button(win, text= "Select", background= "gray71", command=select_text)
button.pack(pady=20, side= TOP)
win.mainloop()輸出
執行以上程式碼會顯示一個包含一個“選擇”按鈕的視窗,該按鈕可用於選擇文字小元件中編寫的所有內容。

現在,單擊“選擇”按鈕選擇小元件中的所有文字。

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP