在 Tkinter 中按下按钮后如何清除 Entry 小部件? [英] How to clear the Entry widget after a button is pressed in Tkinter?
本文介绍了在 Tkinter 中按下按钮后如何清除 Entry 小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在用户使用 Tkinter 按下按钮后清除 Entry
小部件.
I'm trying to clear the Entry
widget after the user presses a button using Tkinter.
我尝试使用 ent.delete(0, END)
,但我收到错误消息,指出字符串没有 delete 属性.
I tried using ent.delete(0, END)
, but I got an error saying that strings don't have the attribute delete.
这是我的代码,我在 real.delete(0, END)
上遇到错误:
Here is my code, where I'm getting error on real.delete(0, END)
:
secret = randrange(1,100)
print(secret)
def res(real, secret):
if secret==eval(real):
showinfo(message='that is right!')
real.delete(0, END)
def guess():
ge = Tk()
ge.title('guessing game')
Label(ge, text="what is your guess:").pack(side=TOP)
ent = Entry(ge)
ent.pack(side=TOP)
btn=Button(ge, text="Enter", command=lambda: res(ent.get(),secret))
btn.pack(side=LEFT)
ge.mainloop()
推荐答案
经过Tkinter 简介,我想出了下面的代码,除了显示文本字段并在按下 清除文本"
按钮时清除它外,它什么也不做:
After poking around a bit through the Introduction to Tkinter, I came up with the code below, which doesn't do anything except display a text field and clear it when the "Clear text"
button is pushed:
import tkinter as tk
class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master, height=42, width=42)
self.entry = tk.Entry(self)
self.entry.focus()
self.entry.pack()
self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text)
self.clear_button.pack()
def clear_text(self):
self.entry.delete(0, 'end')
def main():
root = tk.Tk()
App(root).pack(expand=True, fill='both')
root.mainloop()
if __name__ == "__main__":
main()
这篇关于在 Tkinter 中按下按钮后如何清除 Entry 小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文