在用户给出他/她的名字后,我试图删除一个条目字段,但是,使用此代码时出现错误:
from Tkinter import *
from random import randrange
class Application(Frame):
def __init__(self):
Frame.__init__(self)
self.grid()
self.nameEntry = Entry(self)
self.nameEntry.grid(row=0, column=0, columnspan=4)
self.setupBtn = Button(self, text = "Generate", command=self.setup)
self.setupBtn.grid(row=4, column=0, columnspan=4)
self.preText = Text(self, width=50, height=3, wrap=WORD)
self.preText.grid(row=6, column=0, columnspan=4)
self.preText.delete(0.0, END)
self.preText.insert(0.0, "If you enter your name, i'll give you a random number each time!")
self.statusLbl = Label(self, text="What's your name?", bg="white")
self.statusLbl.grid(row=7, column=0, columnspan=4, sticky=W)
def setup(self):
name = self.nameEntry.get()
if name != "":
self.nameEntry.destroy()
self.setupBtn = Button(self, text = "Roll Again", command=self.setup)
self.setupBtn.grid(row=4, column=0, columnspan=4)
randomNumber = randrange(10, 100)
if name == "":
message = "Your name please!"
self.preText.delete(0.0, END)
self.preText.insert(0.0, message)
else:
self.preText.delete(0.0, END)
self.preText.insert(0.0, str(randomNumber))
Application().mainloop()
这是错误:
Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "C:/Users/User/Desktop/app.py", line 25, in setup name = self.nameEntry.get() File "C:\Python27\lib\lib-tk\Tkinter.py", line 2391, in get return self.tk.call(self._w, 'get') TclError: invalid command name ".45073096L.45073736L"
我想在用户输入名称并单击滚动按钮后删除名称输入字段。但是我没有滚动,而是得到了那个错误。
有人可以帮助我吗?据我所知,这是因为name = self.nameEntry.get()
用于存储名称,因此,当我删除它时,它会导致问题。你有解决方法吗?我不是很好,因此不确定如何进行。