get属性不能用于条目



我正在尝试制作一个Jenga Scorer,我只是在制作一个球员名单。当我这样做的时候,我偶然发现我不能从条目中获得属性。

qadd是执行此操作的函数。代码:

from tkinter import *
players = []
def questionw():
def addplayer():
player = qentry.get()
players.append(player)
question = Tk()
question.geometry("200x150")
qentry = Entry(question,).place(y=60, x=3, width=195, height=20)
qlabel = Label(question, text="What is the namenof the player?", justify=CENTER, font=("Amasis MT Pro",12)).pack()
qdone = Button(question, text="Done").place(y=90, x=10, width=80)
qadd = Button(question, text=f"Add ({len(players)})", command=addplayer).place(y=90, x=100, width=80)
qdone = Button(question, text="Cancel").place(y=120, x=55, width=80)
question.mainloop()
question()

当我在添加按钮中输入一些东西时,它在控制台上返回如下内容:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersuserAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 1921, in __call__
return self.func(*args)
File "C:UsersuserDesktopjenga.py", line 5, in addplayer
player=qentry.get()
AttributeError: 'NoneType' object has no attribute 'get'

回溯告诉您qentry当前不是Entry,而是None。此外,您正在将place()方法的返回值分配给qentry,而不是Entry的初始化器。

试试这个:

qentry = Entry(question,)
qentry.place(y=60, x=3, width=195, height=20)

相关内容

  • 没有找到相关文章

最新更新