Reputation: 81
I am trying to store the user's input in the "nEnt
" entry widget as a variable that will be printed later on.
I've tried .get()
but when I run the program it doesn't print the name it prints a blank line and then it starts the game, I think it might be printing what is there in the beginning rather than what is the entry widget as the enter button is pressed.
from tkinter import *
import random
def game():
while 1:
c1 = input("Would you like to play from the beginning?").lower()
if c1 == "yes":
lvl1()
elif c1 == "no":
print("Your score was:", score)
break
else:
game()
quit()
def lvl1():
print(name)
print("LEVEL 1")
global score
score = 0
operators = ("+","-")
while 1:
No1 = random.randint(1,10)
No2 = random.randint(1,10)
Op = random.choice(operators)
if Op == "+":
A1 = No1 + No2
print("What is", No1, Op, No2,"?")
answer = float(input("Enter your answer"))
elif Op == "-":
A1 = No1 - No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
if answer == A1:
print("Correct!")
score = score + 1
print(score)
else:
print("Incorrect! Try again...")
game()
if score == 5:
break
print("Congratulations on making it to level 2!")
lvl2()
def lvl2():
print("LEVEL 2")
global score
score = 5
operators = ("+","-","x")
while 1:
No1 = random.randint(1,15)
No2 = random.randint(1,15)
Op = random.choice(operators)
if Op == "+":
A1 = No1 + No2
print("What is", No1, Op, No2,"?")
answer = float(input("Enter your answer"))
elif Op == "-":
A1 = No1 - No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
else:
A1 = No1 * No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
if answer == A1:
print("Correct!")
score = score + 1
print(score)
else:
print("Incorrect! Try again...")
game()
if score == 10:
break
print("Congratulations on making it to level 3!")
lvl3()
def lvl3():
print("LEVEL 3")
global score
score = 10
operators = ("+","-","x")
while 1:
No1 = random.randint(10,50)
No2 = random.randint(10,50)
Op = random.choice(operators)
if Op == "+":
A1 = No1 + No2
print("What is", No1, Op, No2,"?")
answer = float(input("Enter your answer"))
elif Op == "-":
A1 = No1 - No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
else:
A1 = No1 * No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
if answer == A1:
print("Correct!")
score = score + 1
print(score)
else:
print("Incorrect! Try again...")
game()
if score == 15:
break
print("Congratulations on making it to level 4!")
lvl4()
def lvl4():
print("LEVEL 4")
global score
score = 15
operators = ("+","-","x")
while 1:
No1 = random.randint(10,100)
No2 = random.randint(10,100)
Op = random.choice(operators)
if Op == "+":
A1 = No1 + No2
print("What is", No1, Op, No2,"?")
answer = float(input("Enter your answer"))
elif Op == "-":
A1 = No1 - No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
else:
A1 = No1 * No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
if answer == A1:
print("Correct!")
score = score + 1
print(score)
else:
print("Incorrect! Try again...")
game()
if score == 20:
break
print("Congratulations! You have completed the game.")
def nextstep():
y.destroy()
nLabel.destroy()
nEnt.destroy()
nBtn.destroy()
w = Label(topFrame, text="To play the game press Start", fg="black")
w.pack(fill=X and Y)
button1 = Button(bottomFrame, text="Start", fg="black", command=lvl1)
button1.pack()
root = Tk()
root.title("Maths Game!")
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
y = Label(topFrame, text="Welcome to the maths game.")
y.pack(fill=X and Y)
nLabel = Label(bottomFrame, text="Enter your name below.")
nLabel.pack(fill=X and Y)
nEnt = Entry(bottomFrame)
nEnt.pack()
name = (nEnt).get()
nBtn = Button(bottomFrame, text="Enter", command=nextstep)
nBtn.pack()
name = nEnt.get()
root.mainloop()
Upvotes: 1
Views: 64
Reputation: 22804
Solution:
nBtn
.Putting the solution in practice (with your own actual code which needs to be redesigned and cleaned):
from tkinter import *
import random
def game():
while 1:
c1 = input("Would you like to play from the beginning?").lower()
if c1 == "yes":
lvl1()
elif c1 == "no":
print("Your score was:", score)
break
else:
game()
quit()
def lvl1():
global name
print(name)
print("LEVEL 1")
global score
score = 0
operators = ("+","-")
while 1:
No1 = random.randint(1,10)
No2 = random.randint(1,10)
Op = random.choice(operators)
if Op == "+":
A1 = No1 + No2
print("What is", No1, Op, No2,"?")
answer = float(input("Enter your answer"))
elif Op == "-":
A1 = No1 - No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
if answer == A1:
print("Correct!")
score = score + 1
print(score)
else:
print("Incorrect! Try again...")
game()
if score == 5:
break
print("Congratulations on making it to level 2!")
lvl2()
def lvl2():
print("LEVEL 2")
global score
score = 5
operators = ("+","-","x")
while 1:
No1 = random.randint(1,15)
No2 = random.randint(1,15)
Op = random.choice(operators)
if Op == "+":
A1 = No1 + No2
print("What is", No1, Op, No2,"?")
answer = float(input("Enter your answer"))
elif Op == "-":
A1 = No1 - No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
else:
A1 = No1 * No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
if answer == A1:
print("Correct!")
score = score + 1
print(score)
else:
print("Incorrect! Try again...")
game()
if score == 10:
break
print("Congratulations on making it to level 3!")
lvl3()
def lvl3():
print("LEVEL 3")
global score
score = 10
operators = ("+","-","x")
while 1:
No1 = random.randint(10,50)
No2 = random.randint(10,50)
Op = random.choice(operators)
if Op == "+":
A1 = No1 + No2
print("What is", No1, Op, No2,"?")
answer = float(input("Enter your answer"))
elif Op == "-":
A1 = No1 - No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
else:
A1 = No1 * No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
if answer == A1:
print("Correct!")
score = score + 1
print(score)
else:
print("Incorrect! Try again...")
game()
if score == 15:
break
print("Congratulations on making it to level 4!")
lvl4()
def lvl4():
print("LEVEL 4")
global score
score = 15
operators = ("+","-","x")
while 1:
No1 = random.randint(10,100)
No2 = random.randint(10,100)
Op = random.choice(operators)
if Op == "+":
A1 = No1 + No2
print("What is", No1, Op, No2,"?")
answer = float(input("Enter your answer"))
elif Op == "-":
A1 = No1 - No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
else:
A1 = No1 * No2
print("What is", No1,Op,No2,"?")
answer = float(input("Enter your answer"))
if answer == A1:
print("Correct!")
score = score + 1
print(score)
else:
print("Incorrect! Try again...")
game()
if score == 20:
break
print("Congratulations! You have completed the game.")
def nextstep():
global name
name = nEnt.get()
y.destroy()
nLabel.destroy()
nEnt.destroy()
nBtn.destroy()
w = Label(topFrame, text="To play the game press Start", fg="black")
w.pack(fill=X and Y)
button1 = Button(bottomFrame, text="Start", fg="black", command=lvl1)
button1.pack()
root = Tk()
root.title("Maths Game!")
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
y = Label(topFrame, text="Welcome to the maths game.")
y.pack(fill=X and Y)
nLabel = Label(bottomFrame, text="Enter your name below.")
nLabel.pack(fill=X and Y)
nEnt = Entry(bottomFrame)
nEnt.pack()
name = nEnt.get()
#name = (nEnt).get()
nBtn = Button(bottomFrame, text="Enter", command=nextstep)
nBtn.pack()
root.mainloop()
Upvotes: 1