【问题标题】:Trying to store text entered into entry widget to a variable尝试将输入到条目小部件中的文本存储到变量中
【发布时间】: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


【解决方案1】:
  • 在编写任何代码之前,您应该重新设计整个应用程序。
  • 除了您提到的问题之外,还有其他问题。
  • 我将只解决您强调的问题。

解决方案:

  1. 您必须在nBtn 的回调中获取与他的姓名相关的用户输入。
  2. 您必须在使用的每个单独函数中将 name 声明为全局变量。

将解决方案付诸实践(使用您自己需要重新设计和清理的实际代码):

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

【讨论】:

    猜你喜欢
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多