我正在尝试将用户的输入存储在" nEnt
"条目小部件作为稍后将打印的变量。
我已经尝试了.get()
但是当我运行该程序时,它没有打印出打印空白行的名称然后它开始游戏,我想它可能正在打印那里有什么在开始时而不是按下输入按钮时的条目小部件。
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()
答案 0 :(得分:1)
解决方案:
nBtn
。将解决方案付诸实践(使用您自己的实际代码,需要重新设计和清理):
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()