我正在尝试制作一个问答游戏。但是,当我尝试创建一个输入框并操纵数据时,它会抛出一个错误。我需要的是如何正确构建条目小部件并能够将输入的数据存储到变量的解释。这是代码:
while True:
random_question = random.randint(0, 39)
if questions_asked == 20:
end_label = tkinter.Label(self, "Your score for that round was {} . For another look at your scores go to the scores page".format(score))
end_label.pack()
break
question_label = tkinter.Label(self , text="{}".format(questions[random_question]))
user_entry = tkinter.Entry(self, "Type your answer here : ")
user_entry.pack()
stored_entry = user_entry.get()
remove_key(random_question)
if stored_entry == "end":
end_label = tkinter.Label(self, "Your score for that round was {} . For another look at your scores go to the scores page".format(score))
end_label.pack()
break
else:
verify(stored_entry)
continue
home_button = ttk.Button(self, text="Go back to home page", command=lambda: shown_frame.show_frame(OpeningFrame))
home_button.pack(pady=10, padx=10)
这是错误:
File "app.py", line 132, in <module>
app = MyQuiz()
File "app.py", line 21, in __init__
frame = f(main_frame, self)
File "app.py", line 117, in __init__
user_entry = tkinter.Entry(self, "Type your answer here : ")
File "/usr/lib/python3.5/tkinter/__init__.py", line 2519, in __init__
Widget.__init__(self, master, 'entry', cnf, kw)
File "/usr/lib/python3.5/tkinter/__init__.py", line 2138, in __init__
classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)]
AttributeError: 'str' object has no attribute 'items'
答案 0 :(得分:1)
您的错误就在这一行:
user_entry = tkinter.Entry(self, "Type your answer here : ")
因为Entry只需要父窗口之外的关键字参数。所以你应该用以下代码替换这一行:
user_entry = tkinter.Entry(self)
user_entry.insert(0, "Type your answer here : ")
备注:与标签或按钮不同,条目小工具不具有text
关键字来设置初始文本。必须使用insert
方法设置它。