尝试将输入到条目窗口小部件中的文本存储到变量中

时间:2017-11-07 13:12:07

标签: python python-3.x user-interface variables tkinter

我正在尝试将用户的输入存储在" 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()

1 个答案:

答案 0 :(得分:1)

  • 在编码任何内容之前,您应该重新设计整个应用程序。
  • 除了您提到的问题之外,还有其他问题。
  • 我将仅解决您突出显示的问题。

解决方案:

  1. 您必须在nBtn
  2. 的回调中获取与其姓名相关的用户输入
  3. 您必须将name声明为使用的每个单独函数中的全局变量。
  4. 将解决方案付诸实践(使用您自己的实际代码,需要重新设计和清理):

    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()