0
votes

I plan a mathematical game. I want the randint from label 1 + randint from label 2 to be calculated and then check if my entry is the same as the calculated number.

I want to add z1 to z2 and check if the Entry is the same but I don't know how I can add one random number to the other.

I can't add z1 to z2 so what is the way to do that? Should this happen in the if...else?

from tkinter import * from random import *

fenster = Tk()
fenster.title("Mathe-Spiel")
fenster.geometry("300x300")

def anfang():
    z1 =label = Label(fenster, text=(randint(1,100)))
    label.pack()

    zp=label2 = Label(fenster, text="+")
    label2.pack()

    z2= label1 = Label(fenster, text=(randint(1,100)))
    label1.pack()

    a =label3 = Label(fenster,)
    label3.pack()


    e1=eingabe = Entry(fenster)
    eingabe.pack()

    e2=z1+z2

    def ausgabe():
        if (e1==e2):
            a.configure(text=(eingabe.get()))
        else:
            a.configure(text="Falsch")

    ergebnis = Button(fenster, text="ergebnis", command= ausgabe)
    ergebnis.pack()







anfangsknopf = Button(fenster, text="Fange mit dem Spielen an", command=anfang)
anfangsknopf.pack()

mainloop()
1
It's unclear what your actual question is. Maybe some background on what an Entry is, and what criteria would be used to determine equality. - user1531971
Hmm, so i plan a mathematic game. I want that randint from label 1 + randint from label 2 get calculated and i want that it got checked if my entry is same with the calculated number. Clear ? If not i try to explain it on an other way. Thanks - silleman
These details should be in the text of the question. Edit the question and tell us what you want to do, what you have tried, and what results you get. - user1531971

1 Answers

0
votes

Error: You are trying adding 2 labels together (e2=z1+z2) but you expect that you add the values in your z1 and z2 text option!

You can get the right values while get text from z1 and z2 but I would do it in a different way.

like so:

from tkinter import Tk, IntVar, Button, Label, Entry
from random import randint

class Gui:
    def __init__(self, master):
        self.master = master
        self.summand1 = IntVar()
        self.summand2 = IntVar()
        anfangsknopf = Button(self.master, text="Fange mit dem Spielen an", command=self.create_widgets)
        anfangsknopf.pack()

    def create_widgets(self):
        """ create widgets on the fly """
        try:
            self.label1.destroy()
            self.label2.destroy()
            self.label3.destroy()
            self.eingabe.destroy()
            self.ergebnis.destroy()
            self.answer.destroy()
        except:
            print("No widgets destroyed")           
        self.fill_summands()
        self.label1 = Label(self.master, textvariable=self.summand1)
        self.label2 = Label(self.master, text="+")
        self.label3 = Label(self.master, textvariable=self.summand2)
        self.eingabe = Entry(self.master)
        self.ergebnis = Button(self.master, text="ergebnis", command= self.ausgabe)
        self.answer = Label(self.master, text="...")  
        self.label1.pack()
        self.label2.pack()
        self.label3.pack()
        self.eingabe.pack()
        self.ergebnis.pack()
        self.answer.pack()
        self.eingabe.focus_set()

    def get_random_nr(self):
        """ get random number """
        return randint(1,100)

    def fill_summands(self):
        """ set IntVar variables """
        r_number1 = self.get_random_nr()
        r_number2 = self.get_random_nr()
        self.summand1.set(r_number1)
        self.summand2.set(r_number2)

    def ausgabe(self):
        """ calculate addition """
        try:
            if self.summand1.get()+self.summand2.get() == int(self.eingabe.get()):
                print("Correct")
                self.answer.configure(text="Correct", fg="Green")
            else:
                print("Wrong")
                self.answer.configure(text="Wrong", fg="Red")
        except ValueError:
            print("Please set a number in Entry widget")
            self.answer.configure(text="Bitte gültige Nummer eingeben", fg="Red")

if __name__ == "__main__":
    fenster = Tk()
    fenster.title("Mathe-Spiel")
    fenster.geometry("300x300")
    app = Gui(fenster)
    fenster.mainloop()