Python Tkinter将button onclick替换为entry,在按下enter键后将返回到带有entry



我想有一个tkinter按钮,当点击时,是取代带有tk.Entry(),在条目中按下enter后,条目被替换为new按钮,包含条目的文本(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事件时的新按钮。
这是代码的两个独立问题,我真的很感谢你对这两个问题的帮助:)

当按钮被单击时,您需要隐藏按钮并创建输入框。

当你按时,输入键,更新隐藏按钮的文本,销毁输入框,然后显示按钮。

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()

最新更新