Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的Tkinter Python代码不能像我预期的那样工作?_Python_User Interface_Tkinter - Fatal编程技术网

为什么我的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

Remove
global passwordCorrect
中定义的变量。您期望什么?Remove
global passwordCorrect
。您期望什么?