试图将变量从一个程序导入到另一个程序,出现错误ImportError:无法导入名称username_value

时间:2018-07-13 08:55:17

标签: tkinter

我已经创建了2个文件,login_tkinter和主页。登录完成后,我需要从login_tkinter将变量username_value变量导入home,这样我才能显示“ hello” + username_value。但是显示错误 ImportError:无法导入名称username_value 请帮助

这些是代码

login_tkinter.py

    from Tkinter import *#to import the library
    import MySQLdb #to import Mysql

    #to create login window template
     root=Tk() 
     root.title("Login")
     root.configure(background="black")
     root.geometry("400x200")
     label_1=Label(root,text = "Login", bg = "black").grid(row =1, column = 2, sticky = W)
     label_2=Label(root,text = "User-name", bg="black", fg = "white").grid(row=2, column=0,sticky = W)
     label_2=Label(root,text = "Password", bg="black", fg = "white").grid(row=4, column=0,sticky = W)
     username_entry = Entry(root, width = 20, bg="white")
     username_entry.grid(row = 2, column = 4,sticky = W)
     password_entry = Entry(root, show="*", width = 20, bg="white")
     password_entry.grid(row = 4, column = 4,sticky = W)


    def part2():
        flag=0
        username_value= username_entry.get()#to accquire input username
        password_value= password_entry.get() #to accquire input password
        print(username_value)
        print(password_value)
        db = MySQLdb.connect(host="localhost",   #to create connection to SQL
                 user="root",        
                 passwd="passwd",  
                 db="stb_report")        
        cur = db.cursor() #cursor created to execute commands
        cur.execute ("select username,password from stb_details")
        user= cur.fetchall()
        for row in user:
            if (row[0]==username_value): #loop to check username
                if(row[1]==password_value): #loop to check password
                    print("login validated")
                    flag=1
                    root1= Tk()
                    root1.title("New Window")
                    root1.configure(background="black")
                    label_1=Label(root1,text = "New page", bg = "black" ,fg="white").grid(row =2, column = 2, sticky = W)

                    break
                else:
                    print("username recgonized,wrong password")
                    flag=2
            else:          
                print("invalid username")

        if (flag==0): #to pr int comment line in window
            label_3=Label(root,text="INVALID USERNAME",bg="black",fg="red").grid(row=10,column=4,sticky=W)

        elif(flag==1):
            label_3=Label(root,text="Login Validated",bg="black",fg="red").grid(row=10,column=4,sticky=W)
            import home
        elif(flag==2):
            label_3=Label(root,text="Username Recgonized,Wrong Password",bg="black",fg="red").grid(row=10,column=4,sticky=W)




db.close()


    submit= Button(root, text="SUBMIT",width=6,command =part2).grid(row=8, column =4) 

    if __name__ == '__main__':
         root.mainloop()

home.py

    from Tkinter import *
    from login_tkinter import username_value

    root=Tk() 
    root.title("Welcome to MCQ")
    root.configure(background="black")
    root.geometry("400x200")
    welcome='welcome',username_value,'start quiz?'
    label1=Label(root,text = welcome, bg="black", fg = "white").grid(row=2, column=0,sticky = W)

    if __name__ == '__main__':
         root.mainloop()

0 个答案:

没有答案
相关问题