【发布时间】:2017-11-07 13:12:07
【问题描述】:
我正在尝试将用户的输入存储在“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()
【问题讨论】:
-
您正在评估
nEnt的内容以在程序开始时填充name。在程序开始时,没有在输入字段中输入任何文本。用户按下“Enter”按钮后,您可能需要在nextstep函数中获取输入字段的内容。
标签: python python-3.x user-interface variables tkinter