简体   繁体   English

如何在一段时间后自动注销用户。 Python Tkinter

[英]How to auto logout user after certain period of time. Python Tkinter

I am trying to create the program for Raspberry Pi to control locker doors.我正在尝试为 Raspberry Pi 创建程序来控制储物柜门。 Once the user successfully logged in I want to make all locker door buttons active in order to open door.用户成功登录后,我想激活所有储物柜门按钮以打开门。 I would like to also log user out after a few minutes of inactivity.我还想在几分钟不活动后注销用户。 How to automatically log out the user after a certain period of time.如何在一段时间后自动注销用户。 Any help will be really appreciated.任何帮助将不胜感激。

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()

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

Use Tk.after() .使用Tk.after() With it, you can wait a specific time, then launch a callback.有了它,您可以等待特定时间,然后启动回调。

root.after(time_in_milliseconds, log_out_fun)

Also you can launch aThread that waits a specific time and log the user out.您还可以启动等待特定时间的Thread并将用户注销。

CoderCharmander - thanks for support. CoderCharmander - 感谢您的支持。 I used threading and it's working fine.我使用了线程,它工作正常。

    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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM