Compare tkinter Entry to actual addition answer

Nahuatl_C137

I'm trying to compare the input of an Entry box to the actual answer. This is one of my first python projects and I'm still very confused and to be honest I don't even know how to start asking the question.

The user will click either the addition or subtraction button. A string will appear asking "What does 4 + 5 equal?" The numbers are generated randomly.

I then insert an Entry box using the tkinter library. I don't know how to get() the input and compare it to the sum or difference of the actual numbers.

I was trying to follow this video but I've failed using other methods as well. FYI, I was focusing on the addition method mostly so if you test, that with addition first.

from tkinter import Entry
import tkinter as tk
import random as rand

root = tk.Tk()
root.geometry("450x450+500+300")
root.title("Let's play Math!")

welcomeLabel = tk.Label(text="LET'S PLAY MATH!").pack()
startLabel = tk.Label(text='Select a math operation to start').pack()

def addition():
    a = rand.randrange(1,10,1)
    b = rand.randrange(1,10,1)
    global Answer
    Answer = a + b

    tk.Label(text="What does " + str(a) + " + " + str(b) + " equal? ").place(x=0, y=125)
    global myAnswer
    myAnswer = Entry().place(x=300, y= 125)

def checkAnswer():
    entry = myAnswer.get()

    while int(entry) != Answer:
        if int(entry) != Answer:
            tk.Label(text="Let's try again.").pack()
        elif int(entry) == Answer:
            tk.Label(text="Hooray!").pack()


addBtn = tk.Button(text="Addition", command=addition).place(x=100, y = 60)
subBtn = tk.Button(text="Subtraction", command=subtraction).place(x=200, y=60)
checkBtn = tk.Button(text="Check Answer", command=checkAnswer).place(x=300, y = 150)

tk.mainloop()
jack6e

All you need to do to fix your issue is separate the creation of your Entry() object from the placing of it:

def addition():
    a = rand.randrange(1,10,1)
    b = rand.randrange(1,10,1)
    global Answer
    Answer = int(a + b)

    tk.Label(text="What does " + str(a) + " + " + str(b) + " equal? ").place(x=0, y=125)
    global myAnswer
    myAnswer = Entry()
    myAnswer.place(x=300, y= 125)

Entry() returns the entry object, which has a get() method. However, when you chain .place(), you return its result instead, which is None. Therefore you never actually store the Entry object in your variable.

Also, it is a good idea to ensure that Answer is an int as well.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

tkinter entry bg and state

分類Dev

Compare actual file with file at specific baseline

分類Dev

Tkinter changing entry state based on radiobutton

分類Dev

How to make validation of entry field in tkinter

分類Dev

Errors when using entry widgets Tkinter

分類Dev

Python - Tkinter My Entry isn't displayed

分類Dev

How to put the value of an entry into a variable in tkinter

分類Dev

How to make an unchangeable default text for a Tkinter Entry?

分類Dev

How to get an integer value from Entry in Tkinter?

分類Dev

Problem with Tkinter using StringVar(), trace() and Entry()

分類Dev

Python using tkinter and listbox can't get button to deliver answer

分類Dev

Tkinter: Entry box value doesn't store into StringVar() object

分類Dev

Insert a list whithout spaces between each index in a entry widget tkinter

分類Dev

PYTHON TKINTER> e = Entry()> e.bind( '<ENTER>'、function)

分類Dev

Passing parameters to a dynamically created Button from an Entry widget in Tkinter

分類Dev

How can I get the tkinter entry from a loop

分類Dev

Store User Value in an Entry box to calculate a value in Tkinter

分類Dev

How do I get Tkinter Entry.get() to equal 0 if theres no entry?

分類Dev

tkinterのstringvar.get()とEntry.getの違い

分類Dev

RPI Tkinter Window, I want to use a command like overrideredirect and maintain entry box functionality

分類Dev

Resetting Date Entry box and re-selection all check buttons in tkinter after button is pressed

分類Dev

Entry.get()は何も返しませんtkinter-python 3.4

分類Dev

Python-tkinterのIntVar / entryは0のみを返します

分類Dev

Tkinter Entry でのコンマ区切りの使用

分類Dev

Addition on location

分類Dev

Addition with 'sed'

分類Dev

tkinterのEntry.xview_movetoが失敗するのはなぜですか?

分類Dev

Entry.insert()がtkinterのフレームで機能する方法は?

分類Dev

Tkinterの「entry」オブジェクトがtextvariable変数を更新しない(Python 3.4)

Related 関連記事

  1. 1

    tkinter entry bg and state

  2. 2

    Compare actual file with file at specific baseline

  3. 3

    Tkinter changing entry state based on radiobutton

  4. 4

    How to make validation of entry field in tkinter

  5. 5

    Errors when using entry widgets Tkinter

  6. 6

    Python - Tkinter My Entry isn't displayed

  7. 7

    How to put the value of an entry into a variable in tkinter

  8. 8

    How to make an unchangeable default text for a Tkinter Entry?

  9. 9

    How to get an integer value from Entry in Tkinter?

  10. 10

    Problem with Tkinter using StringVar(), trace() and Entry()

  11. 11

    Python using tkinter and listbox can't get button to deliver answer

  12. 12

    Tkinter: Entry box value doesn't store into StringVar() object

  13. 13

    Insert a list whithout spaces between each index in a entry widget tkinter

  14. 14

    PYTHON TKINTER> e = Entry()> e.bind( '<ENTER>'、function)

  15. 15

    Passing parameters to a dynamically created Button from an Entry widget in Tkinter

  16. 16

    How can I get the tkinter entry from a loop

  17. 17

    Store User Value in an Entry box to calculate a value in Tkinter

  18. 18

    How do I get Tkinter Entry.get() to equal 0 if theres no entry?

  19. 19

    tkinterのstringvar.get()とEntry.getの違い

  20. 20

    RPI Tkinter Window, I want to use a command like overrideredirect and maintain entry box functionality

  21. 21

    Resetting Date Entry box and re-selection all check buttons in tkinter after button is pressed

  22. 22

    Entry.get()は何も返しませんtkinter-python 3.4

  23. 23

    Python-tkinterのIntVar / entryは0のみを返します

  24. 24

    Tkinter Entry でのコンマ区切りの使用

  25. 25

    Addition on location

  26. 26

    Addition with 'sed'

  27. 27

    tkinterのEntry.xview_movetoが失敗するのはなぜですか?

  28. 28

    Entry.insert()がtkinterのフレームで機能する方法は?

  29. 29

    Tkinterの「entry」オブジェクトがtextvariable変数を更新しない(Python 3.4)

ホットタグ

アーカイブ