[英]how can I return the user input from entry widget in tkinter
我對這段代碼有這個簡單的問題。 目的是使 label 顯示用戶在 Entry 小部件中鍵入的任何內容。
def action():
output = tk.Label(master, text = #this should be the user input in the Entry widget)
output.grid(row=2)
import tkinter as tk
master = tk.Tk()
e = tk.Entry(master, bg = 'orange').grid(row=0)
b = tk.Button(master, text = 'do it', bg = 'white', command = action).grid(row = 1)
master.mainloop()
您必須定義 StringVar 的變量。 然后通過 Entry 小部件初始化傳遞它,然后您可以通過為 StringVar 變量設置 get function 來獲取小部件的文本。 這是代碼。
content = StringVar()
entry = Entry(parent, text=caption, textvariable=content)
# Getting Text from Entry Widget
text = content.get()
# Setting text to Entry widget
content.set(text)
你可以在這里閱讀文檔: https://effbot.org/tkinterbook/entry.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.