Python Tkinter GUI程序:SyntaxError:语法无效?

时间:2017-04-03 00:30:20

标签: python tkinter

所以我不是一般的python或编程新手,但我还在学习python。我正在为我和我的一些朋友制作一个节目。我正在为登录屏幕制作GUI和代码,当我不断为我的一个函数获取此语法错误时。而且我不知道可能导致它的原因。如果有人能提供一些帮助,我们将不胜感激。附:我只会发布我遇到错误的部分,而且只有我觉得有什么必要来帮助显示正在发生的事情,而且如果有帮助的话我会使用python 3.x. / p>

# Login Setup
loginW = Tk()

# Password Authentication Function
def Authentication():

    valid = 'pythonprogramming'

    if (pwordE = valid):
        # Syntax Error for the above line
        mp = Tk()
        loginW.destroy()
        # Main Program here
        mp.mainloop()

    else:
        errorW = Tk()
        errorL = Label(errorW, text='Invalid Password!')

        errorW.mainloop()

pwordL = Label(loginW, text='Password: ')
pwordE = Entry(loginW, show='*')

logBtn = Button(loginW, text='Login')
logBtn.bind("<Button-1>", Authentication)

pwordL.pack(row=0)
pwordE.pack(row=0, column=1)
logBtn.pack(row=1, columnspan=2)

loginW.mainloop()

1 个答案:

答案 0 :(得分:1)

您的代码有几个错误:

  • pack更改为grid
  • pwordE = valid更改为pwordE.get() == valid
  • 将参数event添加到Authentication函数

完整代码:

# Login Setup
loginW = Tk()

# Password Authentication Function
def Authentication(event):

    valid = 'pythonprogramming'

    if pwordE.get() == valid:
        # Syntax Error for the above line
        mp = Tk()
        loginW.destroy()
        # Main Program here
        mp.mainloop()

    else:
        errorW = Tk()
        errorL = Label(errorW, text='Invalid Password!')
        errorL.grid(row=0, column=0)
        errorW.mainloop()

pwordL = Label(loginW, text='Password: ')
pwordE = Entry(loginW, show='*')

logBtn = Button(loginW, text='Login')
logBtn.bind("<Button-1>", Authentication)

pwordL.grid(row=0, column=0)
pwordE.grid(row=0, column=1)
logBtn.grid(row=1, columnspan=2)

loginW.mainloop()

截图:

enter image description here

更正密码:

enter image description here

密码不正确:

enter image description here