Tkinter - NameError:名称未定义

时间:2018-03-01 21:39:10

标签: python tkinter

我在Tkinter创建一个简单的加法和减法游戏。

{
    "foo":{
        "value1": "fooval1",
        "value2": "fooval2"
    },
    "bar":{
        "value1": "barval1",
        "value2": "barval2"
    }
}

我在运行程序时遇到问题,如果按jq 'map(.name: {.})',我会被告知有import random import Tkinter as tkinter window = tkinter.Tk() window.geometry("250x150") window.title("Maths Game Login") window.iconbitmap("mathsgame_1_16x16.icns") window.configure(background="firebrick") username = "" password = "" def login(): if entusername.get() == username and entpassword.get() == password: game_window = tkinter.Toplevel(window) game_window.title("Maths Game") game_window.configure(background="firebrick") game_window.geometry("500x250") number1 = random.randint(0, 9) number2 = random.randint(0, 9) addorsubtract = random.randint(1,2) lblno1 = tkinter.Label(game_window, text="{0}".format(number1), bg="firebrick", fg="yellow", font=("Courier", 100)) lblno1.place(x=50, y=60) lblno2 = tkinter.Label(game_window, text="{0}".format(number2), bg="firebrick", fg="yellow", font=("Courier", 100)) lblno2.place(x=200, y=60) if addorsubtract == 1: additionsign = tkinter.Label(game_window, text="+", bg="firebrick", fg="yellow", font=("Courier", 100)) additionsign.place(x=125, y=60) elif addorsubtract == 2: subtractsign = tkinter.Label(game_window, text="-", bg="firebrick", fg="yellow", font=("Courier", 100)) subtractsign.place(x=125, y=60) equalssign = tkinter.Label(game_window, text="=", bg="firebrick", fg="yellow", font=("Courier", 100)) equalssign.place(x=275, y=60) entanswer = tkinter.Entry(game_window, bg="yellow", fg="firebrick", width=2, font=("Courier", 100)) entanswer.place(x=350, y=60) if addorsubtract == 1: global sum1 sum1 = int(number1) + int(number2) print("{0}".format(sum1)) elif addorsubtract == 2: global sum2 sum2 = int(number1) - int(number2) print("{0}".format(sum2)) def submitanswer(): if int(entanswer.get()) == int(sum2) or int(entanswer.get()) == int(sum1): print("Correct") else print("Try Again") submitbtn = tkinter.Button(game_window, text="Check Answer", highlightbackground="firebrick", cursor="gumby", command=submitanswer) submitbtn.place(x=360, y=200) else: exit() lbl = tkinter.Label(window, text="Hatim's Maths Game", bg="firebrick", fg="yellow", font=('Courier', 18)) lbl.pack() lbl = tkinter.Label(window, text="Username:", bg="firebrick", fg="yellow") lbl.pack() entusername = tkinter.Entry(window) entusername.pack() lbl = tkinter.Label(window, text="Password:", bg="firebrick", fg="yellow" ) lbl.pack() entpassword = tkinter.Entry(window, show="*") entpassword.pack() btn = tkinter.Button(window, text="Login", highlightbackground="firebrick", command=login, cursor="gumby") btn.pack() window.mainloop() 。我不确定是否认为问题存在于变量submitbtnNameError: global name 'sum2' is not defined中,但我试图做出的任何改变都是无效的 任何帮助将不胜感激。

0 个答案:

没有答案