[英]Python Tkinter replace button onclick with entry that after pressed enter will turn back to button with entry input
我正在嘗試創建一個 tkinter 按鈕,單擊該按鈕時,它會被替換為tk.Entry()
,並且在條目中按下enter
后,該條目將被替換為具有條目文本的新按鈕( entry.get()
)...
這是我的代碼:
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
def func(neww):
b = tk.Button(root, text=neww, command=onclick)
b.grid(row=0, column=0)
def onclick():
e = tk.Entry(root)
e.grid(row=0, column=0)
e.bind('<Return>', func(e.get()))
b = tk.Button(root, text='clickme', command=onclick)
b.grid(row=0, column=0)
root.mainloop()
由於某種原因我無法發布照片,但結果是條目和按鈕重疊,
以及正在創建OnFocus
的新按鈕,而不是在有Return
事件時創建。
這是代碼的兩個獨立問題,我真的很感激對這兩個問題的幫助:)
單擊按鈕時,需要隱藏按鈕並創建輸入框。
當您按下Enter鍵時,更新隱藏按鈕的文本,銷毀輸入框,然后顯示按鈕。
def func(evt):
# update button text
b.config(text=evt.widget.get())
# remove the entry box
evt.widget.destroy()
# show back the button
b.grid(row=0, column=0)
def onclick():
# hide the button
b.grid_forget()
# show the entry box
e = tk.Entry(root)
e.grid(row=0, column=0)
e.bind('<Return>', func)
e.focus_set()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.