manipulating entry box data

247 views Asked by At

I am trying to make a quiz game. However when I try to make an entry box and manipulate the data it throws an error. What I need is an explanation to how i can properly structure an entry widget and be able to store the inputted data to a variable. Here is the code:

    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)

Here is the error:

     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'
1

There are 1 answers

0
j_4321 On

Your error is at this line:

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

because Entry expects only keyword arguments apart from the parent window. So you should replace this line by:

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

Remark: Unlike labels or buttons, entry widgets don't have a text keyword to set the initial text. It has to be set after, using the insert method.