为什么我的Tkinter Python代码不能像我预期的那样工作?
代码如下:为什么我的Tkinter Python代码不能像我预期的那样工作?,python,user-interface,tkinter,Python,User Interface,Tkinter,代码如下: from tkinter import * def main(): pw = '' passwordCorrect = False window = Tk() instructionLabel = Label(window,text='Enter your password:') entryBox = Entry(window) def checkPassword(): if entryBox.get() == '
from tkinter import *
def main():
pw = ''
passwordCorrect = False
window = Tk()
instructionLabel = Label(window,text='Enter your password:')
entryBox = Entry(window)
def checkPassword():
if entryBox.get() == 'password':
global passwordCorrect
passwordCorrect = True
confirmButton = Button(window,text='Confirm',command=checkPassword)
instructionLabel.pack()
entryBox.pack()
confirmButton.pack()
window.mainloop()
if passwordCorrect:
print('Access granted')
else:
print('Access denied')
main()
当我关闭窗口时,即使我在输入框中输入“密码”并按下按钮,我始终会收到消息“访问被拒绝”(期望“访问被授予”)。我错过了什么?非常感谢。您忘记在
主功能中设置密码正确
全局。您的main
函数有自己的本地passwordCorrect
变量,该变量与全局变量不同
如果您使用的是python 3,也可以将global passwordCorrect
更改为non-local passwordCorrect
,以便checkPassword
函数使用main
中定义的变量,因为您忘记在main
函数中设置passwordCorrect
全局。您的main
函数有自己的本地passwordCorrect
变量,该变量与全局变量不同
如果您使用的是python 3,您也可以将global passwordCorrect
更改为non-local passwordCorrect
,以便checkPassword
函数使用main
Removeglobal passwordCorrect
中定义的变量。您期望什么?Removeglobal passwordCorrect
。您期望什么?