2013-02-13 42 views
0

我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

直到所有其他代码('if'语句等)有人准备好运行。无论您的Tk界面如何,Python都不会再次执行该代码。 – 2013-02-13 13:22:57

+0

好的,这是有道理的。我需要一些如何循环它? – 2013-02-13 13:24:03

+0

你需要重构你的代码,是的。 – 2013-02-13 13:30:48

回答

0

我相信(虽然我不确定)你的user_session变量没有被def login()改变,而是def login()正在创建一个新的变量叫做user_session并且不会改变当前的user_session,你应该看看全局变量以及如何使用它们global variables and how to use them