2017-01-09 97 views
0

我正在尝试进行问答游戏。但是,当我尝试创建一个输入框并操作数据时,它会引发错误。我需要的是如何正确地构造入口小部件并能够将输入数据存储到变量的解释。下面是代码:操纵输入框数据

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

BTW:而不是'text =“{}”。格式(questions [random_question]))''你可以做'text = questions [random_question]' – furas

+0

哦,对,我认为它不会允许。谢谢你furas。 –

回答

1

你的错误是在这一行:

user_entry = tkinter.Entry(self, "Type your answer here : ") 

因为进入预计只有关键字参数除了父窗口。所以,你应该更换这行:

user_entry = tkinter.Entry(self) 
user_entry.insert(0, "Type your answer here : ") 

备注:与标签或按钮,输入小工具没有text关键字设置的初始文本。它必须在使用insert方法后设置。

+0

非常感谢你。 –