返回 Tkinter 中 Entry 微件的輸入


Tkinter 中的 Entry 微件只不過是一個輸入微件,它接受文字欄位中的單行使用者輸入。要返回 Entry 微件中輸入的資料,我們必須使用 get() 方法。它返回 entry 微件的資料,然後可以在控制檯中列印該資料。

示例

以下示例將返回輸入資料,可以用它來在視窗中顯示,方法是使用一個 Label 微件

#Import the required libraries
from tkinter import *
from tkinter import ttk

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry
win.geometry("700x250")

# Define a function to return the Input data
def get_data():
   label.config(text= entry.get(), font= ('Helvetica 13'))

#Create an Entry Widget
entry = Entry(win, width= 42)
entry.place(relx= .5, rely= .5, anchor= CENTER)

#Inititalize a Label widget
label= Label(win, text="", font=('Helvetica 13'))
label.pack()

#Create a Button to get the input data
ttk.Button(win, text= "Click to Show", command= get_data).place(relx= .7, rely= .5, anchor= CENTER)

win.mainloop()

輸出

如果我們執行上述程式碼,它將顯示一個帶有 Entry 微件的視窗,以及一個按鈕,用於在螢幕上顯示輸入。

現在,單擊“單擊顯示”按鈕,它將在畫布上顯示使用者輸入。

更新於:2023 年 9 月 13 日

36K+ 檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告