簡體   English   中英

Python Tkinter 將按鈕 onclick 替換為輸入,按下輸入后將返回到輸入輸入的按鈕

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM