2017-01-09 95 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

非常感謝你。 –