如何在 tkinter Entry 元件中插入臨時文字?
要在 tkinter 的 Entry 元件中插入臨時文字,我們將 <FocusIn> 事件與 Entry 元件繫結,並呼叫使用者自定義函式來刪除 Entry 元件內的文字。
步驟:
匯入 tkinter 庫並建立 tkinter 框架的例項。
使用 geometry 方法設定框架的大小。
建立一個使用者自定義方法 "temp_text()" 來捕獲 <FocusIn> 事件並刪除 Entry 元件內的臨時文字。
在根視窗內建立一個 Entry 元件,並設定其屬性,例如背景顏色、寬度和邊框寬度。
使用 Entry 元件的 insert() 方法從起始位置“0”插入字串。這是臨時文字,當單擊 Entry 元件時,它將消失。
將 <FocusIn> 事件與 Entry 元件繫結,並呼叫 temp_text() 方法。
最後,執行應用程式視窗的 mainloop。
示例
# Import the required library
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Define geometry of the window
win.geometry("700x250")
def temp_text(e):
textbox.delete(0,"end")
textbox = Entry(win, bg="white", width=50, borderwidth=2)
textbox.insert(0, "This is Temporary Text...")
textbox.pack(pady=20)
textbox.bind("<FocusIn>", temp_text)
win.mainloop()輸出
執行後,將顯示以下視窗:
當用戶單擊 Entry 元件內部時,臨時文字將自動消失。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP