0

Im a beginner in python and my sister is learning her multiplication so I wanted to make a program that gives her random problems from 2 to 5. whenever i click the check button, nothing happens(its suppose to change the lblProblem stringvar to say right for 1 sec then switch to another problem). any help or beginner tips are useful. Thank you!

from tkinter import *
import random
import time
root = Tk()
title = Label(root, text="xX Multiplcation Xx", font=("Helvetica"))
global Num1
Num1 = random.randint(2, 5)
global Num2
Num2 = random.randint(2, 10)

def reset():
    LblProblem.set(f"{Num1} x {Num2}")



LblProblem = StringVar()
LblProblem.set(f"{Num1} x {Num2}")



def AnswerCheck(Num1, Num2):
    userGuess = int(Answer.get())

    if userGuess == (Num1 * Num2):
        LblProblem = "right!"
        time.sleep(1)
        Num1 = random.randint(2, 5)
        Num2 = random.randint(2, 10)
        reset()


    elif userGuess > (Num1 * Num2):
        LblProblem = "Lower"
        time.sleep(1)
        Num1 = random.randint(2, 5)
        Num2 = random.randint(2, 10)
        LblProblem.set(f"{Num1} x {Num2}")
    elif userGuess < (Num1 * Num2):
        LblProblem = "Higher"
        time.sleep(1)
        Num1 = random.randint(2, 5)
        Num2 = random.randint(2, 10)
        LblProblem.set(f"{Num1} x {Num2}")
    else:
        print("error")

#Main part

Problem = Label(root, textvariable=LblProblem, font=("Helvetica"))

Answer = Entry(root)
check = Button(root, text="Check", fg="green", command=lambda: AnswerCheck(Num1, Num2))

title.pack()
Problem.pack()
Answer.pack()
check.pack()
root.mainloop()

1 Answers1

0

A few things:

Declaring a variable as global has no effect in the global scope. You should declare it in the function. That way the functon knows to use the global variable.

You can't use globals as parameters to the function. I changed to no parameters and also changed the button callback.

When you try to display hints in the function:

LblProblem = "Lower"

the name LblProblem will then refer to the string "Lower" and the set() method will no longer work. Use set even here.

After you have set the hint you suspend the program for one second with time.sleep(1), but that also suspends queued updates to the GUI. To update GUI before sleep you can do:

root.update_idletasks()

which performs all waiting tasks.

I also fiddled a bit with the code structure in a way that I find easier to read.

from tkinter import *
import random
import time

def reset():
    LblProblem.set(f"{Num1} x {Num2}")

def AnswerCheck():      # No parameters as you use globals
    global Num1, Num2   # Declare globals inside function
    userGuess = int(Answer.get())

    if userGuess == (Num1 * Num2):
        LblProblem.set("right!")    # Set Problem label variable
        root.update_idletasks()     # Update GUI changes
        time.sleep(1)
        Num1 = random.randint(2, 5)
        Num2 = random.randint(2, 10)
        reset()
    elif userGuess > (Num1 * Num2):
        LblProblem.set("Lower")
        root.update_idletasks()
        time.sleep(1)
        Num1 = random.randint(2, 5)
        Num2 = random.randint(2, 10)
        LblProblem.set(f"{Num1} x {Num2}")
    elif userGuess < (Num1 * Num2):
        LblProblem.set("Higher")
        root.update_idletasks()
        time.sleep(1)
        Num1 = random.randint(2, 5)
        Num2 = random.randint(2, 10)
        LblProblem.set(f"{Num1} x {Num2}")
    else:
        print("error")

root = Tk()
Num1 = random.randint(2, 5)
Num2 = random.randint(2, 10)
LblProblem = StringVar()
LblProblem.set(f"{Num1} x {Num2}")

# Main part
title = Label(root, text="xX Multiplcation Xx", font=("Helvetica"))
title.pack()
Problem = Label(root, textvariable=LblProblem, font=("Helvetica"))
Problem.pack()
Answer = Entry(root)
Answer.pack()
check = Button(root, text="Check", fg="green", command=AnswerCheck)
check.pack()

root.mainloop()
figbeam
  • 7,001
  • 2
  • 12
  • 18
  • _"You can't use globals as parameters to the function. "_ is a false statement. – Bryan Oakley May 19 '20 at 22:23
  • thank you so so much, i never got this much support. the tips were great! – Mr.remark And more May 20 '20 at 14:52
  • @Bryan Oakley: Well I'm getting `SyntaxError: name 'arg' is parameter and global`. Tried to find any clues in a [couple](https://stackoverflow.com/questions/20834370/python-modify-global-var-from-a-function-argument) [of](https://stackoverflow.com/questions/10588317/python-function-global-variables) [answers](https://stackoverflow.com/questions/39842332/python-function-to-alter-a-global-variable-that-is-also-parameter) but none that adressed having a var as both parameter and declared global. Can you provide any pointers? – figbeam May 20 '20 at 23:06
  • @figbeam: well, yes, you can't declare a parameter as global. If it's passed as a parameter, you don't need to declare it as global. – Bryan Oakley May 20 '20 at 23:31