【发布时间】:2017-08-01 15:00:53
【问题描述】:
当我尝试使用我创建的检索命令时,我的代码中不断出现此问题,我希望名为“检索”的按钮在输入框中获取信息。这将触发 strGame 命令。
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: retrieve() missing 1 required positional argument: 'entry'
我的代码:
from tkinter import *
verif = False
true=True
items=['pistol','knife','sword']
command=False
root=Tk()
root.iconbitmap('GameLogo.ico')
strvar=StringVar()
root.title('RPG Game')
root.geometry('900x600')
root.resizable(False,False)
frame1 = Frame(root, relief=RAISED)
frame1.pack(side=LEFT, fill=Y, padx=10, pady=10)
entry = Entry(frame1).pack(fill=X)
def retrieve(entry):
result=entry.get()
return result
retreive = Button(frame1, command=retrieve, text="Click to input").pack(fill=X)
def srtGame():
try:
if retrieve(e1)==str('drop'):
print("HELLO")
command = True
print ("Your inventory:", items)
dropItem = input("Which item do you want to drop? ")
for i in range(len(items)):
if items[i] == dropItem:
items.remove(dropItem)
print("Your", dropItem,"has been dropped. ")
print("Your inventory", items)
verif = True
if verif == False and command == True:
print("You don't have that item. Please enter another command.")
except:
IndexError
StartGame = Button(frame1, text="Start game.", relief=RAISED, command=srtGame).pack(fill=X)
GameOutput = Label(frame1, textvariable=strvar, relief=RAISED).pack(fill=X)
root.mainloop()
【问题讨论】:
-
点击一个按钮是一个事件,所以你可以将
event作为参数而不是entry,如果可行的话试试这个 -
不要将
retrieve用作函数和按钮的名称。 -
从函数参数中删除
entry。除非您有理由接受它作为参数,否则在定义它之后的行。 -
请在下面查看我的答案,以获取工作代码。此外,我强烈建议您在开始申请之前深入了解。
标签: python