我的脚本(如图所示)未正确登录?

时间:2013-02-13 13:12:26

标签: python sqlite

我的python脚本,使用tkinter和sqlite3:

#! /usr/bin/python

from Tkinter import *

import hashlib
import sqlite3
conn = sqlite3.connect("test.db")
query = conn.cursor()


root = Tk()

root.geometry("800x800+10+10")
root.title('test')

user_session = "None"

def login():
    username_val_ = username_.get()
    password_val_ = password_.get()
    password_en = hashlib.sha512(password_val_).hexdigest()
    query.execute("SELECT `username` FROM `users` WHERE `username` = ? AND `password` = ?", (username_val_, password_en))
    rows = 0
    for user in query:
        rows = rows +1
        final_name_ = user
    if rows == 1:
        final_name = final_name_[0]
        user_session = final_name
        print(user_session)


if user_session == "None":

    username_l = Label(root, text= "Username:")
    username_l.place(x=20, y=0)

    username = Entry(root)
    username.place(x=20, y=20)

    password_l = Label(root, text= "Password:")
    password_l.place(x=20, y=50)

    password = Entry(root, show="*")
    password.place(x=20, y=70)

    def clear_fields():
        username.delete(0, END)
        password.delete(0, END)
    def clear_fields_():
        username_.delete(0, END)
        password_.delete(0, END)    
    def join(username, password):
        new_pass = hashlib.sha512(password).hexdigest()
        print(new_pass)
        query.execute("INSERT INTO users (username, password) VALUES(?, ?)", (username, new_pass))
        conn.commit()

    def run_test():
        username_val = username.get()
        password_val = password.get()
        if len(username_val)>0 and len(password_val)>0:
            error_textv.set("")
            join(username_val, password_val)
            suc_textv.set("Well done!")
        else:
            error_textv.set("All fields are required")

    button = Button(root, text = "Reset", command = clear_fields)
    button.place(x=120, y=100)

    button = Button(root, text = "Register", command = run_test)
    button.place(x=20,y=100)


    error_textv = StringVar()
    error_textv.set("")
    error_text = Label(root, textvariable= error_textv, fg = 'red')
    error_text.place(x=40,y=140)

    suc_textv = StringVar()
    suc_textv.set("")
    suc_text = Label(root, textvariable= suc_textv, fg = 'green')
    suc_text.place(x=40,y=140)


    suc_text = Label(root, text = 'Or')
    suc_text.place(x=400,y=50)


    username_l_ = Label(root, text= "Username:")
    username_l_.place(x=600, y=0)

    username_ = Entry(root)
    username_.place(x=600, y=20)

    password_l_ = Label(root, text= "Password:")
    password_l_.place(x=600, y=50)

    password_ = Entry(root, show="*")
    password_.place(x=600, y=70)


    button_ = Button(root, text = "Reset", command = clear_fields_)
    button_.place(x=700, y=100)


    button_ = Button(root, text = "Login", command = login)
    button_.place(x=600,y=100)

else:
    print "Welocme"

photo = PhotoImage(file="email.gif")
w = Label(root, image=photo)
w.photo = photo
w.place(x=300, y = 600)

root.mainloop()

正如您将看到的,我的代码编写得非常糟糕,而且非常混乱。 在登录功能上,我希望它能设置" user_session"到用户名, 它是什么,但if语句"如果user_session ="无":然后显示整个登录页面,但是当我将变量设置为登录用户名时,登录页面保持不变,而不是打印"欢迎" ......任何想法?

1 个答案:

答案 0 :(得分:0)

我相信(虽然我不确定)您的user_session变量未被def login()更改。相反,def login()正在创建一个名为user_session的新变量,并不会更改当前的user_session。您应该看看全局变量以及如何使用它们。 global variables and how to use them