python tkinter替换按钮onclick in CONTRICK将在按下Enter后将转向输入输入的输入

发布于 2025-01-26 04:17:45 字数 751 浏览 1 评论 0 原文

我正在尝试使用一个tkinter按钮,即单击时,用 tk.entry() ,然后按下输入在条目中,该条目被A new 按钮,该按钮具有条目的文字( entry.get.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()

由于某种原因,我无法发布照片,但结果是条目和buttos重叠,
以及正在创建的新按钮 onfocus 而不是返回事件时。
这些代码是两个分开的问题,我真的很感谢两者的帮助:)

I'm trying to have a tkinter button, that when clicked, is replaced with an tk.Entry(), and after pressing enter in the entry, the entry is replaced with a new button that has the text of the entry ( entry.get() )...

This is my code:

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

I can't post a photo for some reason, but the result is the entries and buttoons overlap,
as well as the new button being created OnFocus instead of when there is a Return event.
These are two seperate issues with the code, and I'd really appreciate some help with both :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫南 2025-02-02 04:17:45

单击按钮时,您需要 hide 按钮并创建条目框。

当您按 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()

When the button is clicked, you need to hide the button and create the entry box.

When you press Enter key, update the text of the hidden button, destroy the entry box and then show back the button.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文