首页 > 解决方案 > 如何修复“在封闭范围内分配之前引用的自由变量'db_username'”

问题描述

我刚开始学习 python,我很擅长制作没有 GUI 的程序,但我一直在使用它们并且没有 tkinter,我的 pymysql 代码工作得很好,但是当我开始使用 tkinter 时,它不像没有它那样工作,我在分配之前一直被引用无论我尝试了多少不同的格式\组织,它仍然无法正常工作,完整代码“ https://pastebin.com/04uFWB1R

就像我说的那样,没有 gui 就可以正常工作,但是一旦我用 gui 尝试它就行不通

def lClick():
    __uID = uID.get()
    __uName = uName.get()
    __passW = passW.get()
    print(__uName)
    print(__passW)
    auth = Label(lWindow, text="Authorizing...",font=("Areial Bold", 10))
    auth.place(x=-10,y=99)
    time.sleep(0.5)
    time.sleep(1)
    def authorize():
        if __uID == "01":
            auth.configure(text='''
            User Authenticated...
            Accessing Database
            ''')
        ##############################################################
            def dbCon():
                con = pymysql.connect(host='192.***.***.**',
                    user='****',
                    password='******',
                    db='python1',
                    charset='utf8mb4',
                    cursorclass=pymysql.cursors.DictCursor)
                with con:
                    cur = con.cursor()
                    cur.execute("SELECT * FROM users WHERE 'id' = '{0}'".format(__uID))
                    rows = cur.fetchall()
                    for row in rows:
                        db_username = row['username']
                        db_password = row['password']
                def logon():
                    if __uName == db_username:
                        auth.configure(text="DataBase Connected")
                    else:
                        auth.configure(text="Error Has Occured")
                logon()
            dbCon()
        else:
            auth.configure(text="Not Authorized")
    authorize()

我试图弄清楚如何修复“分配前引用的变量”错误

标签: tkinterpython-3.7pymysql

解决方案


推荐阅读