如何在 Tkinter 中使用 Entry 元件?
Entry 元件是 Tcl/Tk 工具包中定義的單行文字元件。我們可以使用 Entry 元件來接收和顯示單行的使用者輸入。
為了使用 Entry 元件,你必須首先使用建構函式 Entry(parent, width, **options) 建立一個 Entry 元件。一旦我們定義了 Entry 元件,我們就可以使用 configure() 方法來配置它的屬性,如字型屬性、顏色、寬度等。
示例
讓我們建立一個 Entry 元件來接收使用者名稱並將其顯示在視窗中。
# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win = Tk() # Set the size of the tkinter window win.geometry("700x350") def show_name(): # Create a Label widget label = Label(win, text="Hello " + str(entry.get()) + "👋", font=('Calibri 25')).pack(pady=20) entry.delete(0, END) # Create a Label Label(win, text="Enter Your Name").pack() # Create an Entry widget entry = Entry(win, width=25) entry.pack(pady=20) Button(win, text="Submit", command=show_name).pack() win.mainloop()
輸出
當你執行上面的程式碼時,它會顯示一個帶有 Entry 元件和一個按鈕的視窗。在給定的 Entry 元件中輸入你的姓名,然後單擊按鈕以在視窗上顯示該訊息。
廣告