如何在 tkinter 中為輸入框新增佔位符?
Tkinter 提供了新增諸如按鈕、文字、輸入框、對話方塊和其他屬性等視窗小部件的功能,這些功能有助於開發應用程式。但是,tkinter 並未在輸入框小部件中包含佔位符。佔位符是出現在輸入框小部件中的佔位文字,用於告知使用者相關資訊。
在本文中,我們將使用 insert(default value, text) 函式在輸入框小部件中新增一個佔位符,該函式採用一個預設值(如 0)以及佔位符文字。
示例
#Import tkinter library from tkinter import* #Create an instance of frame win= Tk() #Set geometry win.geometry("700x400") #Create a text Label Label(win, text="Notepad", font=('Poppins bold', 25)).pack(pady=20) text= StringVar() #Create an entry widget test= Entry(win, textvariable=text) test.pack(fill='x', expand=True, padx= 45, pady=45) test.focus() #Add a placeholder in the entry Widget test.insert(0, "Enter any Text") win.mainloop()
輸出
執行上述程式碼將在其中建立一個帶有佔位符的輸入框小部件。
廣告