【问题标题】:How to auto logout user after certain period of time. Python Tkinter如何在一段时间后自动注销用户。 Python Tkinter
【发布时间】:2019-10-27 18:23:04
【问题描述】:

我正在尝试为 Raspberry Pi 创建程序来控制储物柜门。 用户成功登录后,我想激活所有储物柜门按钮以打开门。 我还想在几分钟不活动后注销用户。 如何在一段时间后自动注销用户。 任何帮助将不胜感激。

from tkinter import *

#Create list
users = {'Tom': '123', 'Bli':345}


user_logged_in = FALSE

def user_logged_in_fun(user_logged_in):
    if user_logged_in == TRUE:
        root.config(bg = 'orange')
        print('SUCCESSSSSSSS')
    else:
        root.config(bg='green')


def main_window():
    global root
    global Frame1
    root = Tk()
    root.title('Tomasz Blicharz')
    root.configure(bg ='green')
    root.geometry('800x500')
    Frame1 = Frame(root, width=760, height = 190)
    Frame1.configure(bg='red')
    Frame1.place(x=20, y=0)

    Main_Window_Button()
    pockets()
    root.mainloop()

def pockets():
    global Frame2
    Frame2 = Frame(root, width=760, height=290)
    Frame2.configure(bg='yellow')
    Frame2.place(x=20, y=200)

    Btn1 = Button(Frame2, text = '1')
    Btn1.grid(row=0, column=0, padx= 10, pady = 10, ipadx= 50, ipady=50)

    Btn2 = Button(Frame2, text = '2')
    Btn2.grid(row=0, column=1, padx= 10, pady = 10, ipadx= 50, ipady=50)

    Btn3 = Button(Frame2, text = '3')
    Btn3.grid(row=0, column=2, padx= 10, pady = 10, ipadx= 50, ipady=50)

    Btn4 = Button(Frame2, text = '4')
    Btn4.grid(row=0, column=3, padx= 10, pady = 10, ipadx= 50, ipady=50)

    Btn5 = Button(Frame2, text = '5')
    Btn5.grid(row=0, column=4, padx= 10, pady = 10, ipadx= 50, ipady=50)

def Main_Window_Button():
    But1 = Button(Frame1, text='LOG IN', width = 20, height = 10,  command = open_new_window)
    But1.place(x=20, y=20)
    #But1.pack()
    But2 = Button(Frame1, text='LOG OUT', width = 20, height = 10, command = log_out_fun )
    But2.place(x=260, y=20)

    But3 = Button(Frame1, text='EXIT', width=20, height=10, command=exit1)
    But3.place(x=520, y=20)

def exit1():
    exit()
def log_out_fun():
    user_logged_in_fun(0)

def open_new_window():
    global new_window
    new_window = Toplevel()
    new_window.title('SUSSESS')
    new_window.geometry('300x300')
#==================================================
    global username_verify
    global password_verify

    username_verify = StringVar()
    password_verify = StringVar()

    global username_login_entry
    global password_login_entry

    username_login_entry = Entry(new_window, textvariable=username_verify)
    username_login_entry.pack()
    password_login_entry = Entry(new_window, textvariable=password_verify, show='*')
    password_login_entry.pack()
    Button(new_window, text="Login", width=10, height=1, command=login_verify).pack()


def login_verify():
    print(username_login_entry.get())
    print(password_login_entry.get())
    checkpas(username_login_entry.get(), password_login_entry.get())

def checkpas(login_username, login_password):
    lab_text = 'PODAJ HASLO'

    if (login_username in users):
        if users[login_username] == login_password:
            print('username and password match')
            user_logged_in_fun(1)
            new_window.withdraw() #Correct one
        else:
            print('user entered the wrong password')
            lab_text = 'INCORRECT PASSWORD'
    else:
      print('your username is not registered. Would you like to register')
      lab_text = 'USER NOT RECOGNISED'
    Lab2 = Label(new_window, text=lab_text)
    Lab2.pack()

#========================================

main_window()

#========================================

【问题讨论】:

    标签: python authentication


    【解决方案1】:

    使用Tk.after()。有了它,您可以等待特定时间,然后启动回调。

    root.after(time_in_milliseconds, log_out_fun)
    

    您还可以启动 Thread 等待特定时间并注销用户。

    【讨论】:

    • 感谢 CoderCharmander 的帮助。我使用线程自动注销。但是我收到一个错误:RuntimeError:当我再次尝试登录时,线程只能启动一次。知道可能出了什么问题吗?
    • 您必须在第一个线程完成工作后实例化一个新线程。
    【解决方案2】:

    CoderCharmander - 感谢您的支持。我使用了线程,它工作正常。

        from tkinter import *
    import threading
    from time import time
    import time
    
    
    def logout_users(delay):
        time.sleep(delay)
        print('YYYYYYYYYYYYYYYYYYYYYYYYYYYY')
        log_out_fun()
    
    #Create list
    users = {'Tom': '123', 'Bli':345}
    
    
    user_logged_in = FALSE
    
    def wylacz_ikony():
        for child in Frame2.winfo_children():
            child.configure(state='disable')
    def wlacz_ikony():
        for child in Frame2.winfo_children():
            child.configure(state='active')
    
    
    
    def user_logged_in_fun(user_logged_in):
        if user_logged_in == TRUE:
            root.config(bg = 'orange')
            wlacz_ikony()
            t1.start()
            #==========================================
            # ==========================================
            print('SUCCESSSSSSSS')
        else:
            root.config(bg='green')
            wylacz_ikony()
    
    
    def main_window():
        global root
        global Frame1
        root = Tk()
        root.title('Tomasz Blicharz')
        root.configure(bg ='green')
        root.geometry('800x500')
        Frame1 = Frame(root, width=760, height = 190)
        Frame1.configure(bg='red')
        Frame1.place(x=20, y=0)
    
        Main_Window_Button()
        pockets()
    
      #  root.mainloop()
    
    def pockets():
        global Frame2
        Frame2 = LabelFrame(root, width=760, height=290, text='LOCKER DOOR')
        Frame2.configure(bg='yellow')
        Frame2.place(x=20, y=200)
    
        Btn1 = Button(Frame2, text = '1')
        Btn1.grid(row=0, column=0, padx= 10, pady = 10, ipadx= 50, ipady=50)
    
        Btn2 = Button(Frame2, text = '2')
        Btn2.grid(row=0, column=1, padx= 10, pady = 10, ipadx= 50, ipady=50)
    
        Btn3 = Button(Frame2, text = '3')
        Btn3.grid(row=0, column=2, padx= 10, pady = 10, ipadx= 50, ipady=50)
    
        Btn4 = Button(Frame2, text = '4')
        Btn4.grid(row=0, column=3, padx= 10, pady = 10, ipadx= 50, ipady=50)
    
        Btn5 = Button(Frame2, text = '5')
        Btn5.grid(row=0, column=4, padx= 10, pady = 10, ipadx= 50, ipady=50)
        wylacz_ikony()
    
    
    def Main_Window_Button():
        But1 = Button(Frame1, text='LOG IN', width = 20, height = 10,  command = open_new_window)
        But1.place(x=20, y=20)
        if user_logged_in == TRUE:
            Bu1.configure(state='disable')
        #But1.pack()
        But2 = Button(Frame1, text='LOG OUT', width = 20, height = 10, command = log_out_fun )
        But2.place(x=260, y=20)
    
        But3 = Button(Frame1, text='EXIT', width=20, height=10, command=exit1)
        But3.place(x=520, y=20)
    
        root.resizable(0, 0)
    
    
    def exit1():
        #t1.join()
        root.destroy()
        exit()
    def log_out_fun():
        user_logged_in_fun(0)
    
    
    def open_new_window():
        global new_window
        new_window = Toplevel()
        new_window.title('SUSSESS')
        new_window.geometry('300x300')
    #==================================================
        global username_verify
        global password_verify
    
        username_verify = StringVar()
        password_verify = StringVar()
    
        global username_login_entry
        global password_login_entry
    
        username_login_entry = Entry(new_window, textvariable=username_verify)
        username_login_entry.pack()
        password_login_entry = Entry(new_window, textvariable=password_verify, show='*')
        password_login_entry.pack()
    
        Button(new_window, text="Login", width=10, height=1, command=login_verify).pack()
        global t1
        t1 = threading.Thread(target=logout_users, args=(5,))
    
    
    
    
    def login_verify():
        print(username_login_entry.get())
        print(password_login_entry.get())
        checkpas(username_login_entry.get(), password_login_entry.get())
    
    def checkpas(login_username, login_password):
        lab_text = 'PODAJ HASLO'
    
        if (login_username in users):
            if users[login_username] == login_password:
                print('username and password match')
                user_logged_in_fun(1)
    
                new_window.withdraw()
            else:
                print('user entered the wrong password')
    
                lab_text = 'INCORRECT PASSWORD'
                password_login_entry.delete(0, END)
        else:
          print('your username is not registered. Would you like to register')
          username_login_entry.delete(0,END)
          password_login_entry.delete(0, END)
          lab_text = 'USER NOT RECOGNISED'
        Lab2 = Label(new_window, text=lab_text)
        Lab2.pack()
    
    
    #========================================
    #jeden = TRUE
    #while(jeden):
    #__init__
    
    main_window()
     #   time.sleep(5)
      #  jeden = FALSE
    
    #========================================
    mainloop()
    

    【讨论】:

      猜你喜欢
      • 2013-09-23
      • 2018-09-10
      • 1970-01-01
      • 2018-12-19
      • 2020-04-05
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多