I want my GUI to ask for login again after been left idle for 5 minutes when you login but the root window should stay when tick the checkbutton.
Challenges:
As soon as I run the script, the messagebox parsed under "session_ends" function pops-up.
When I type the the name and password into the entry widget and I tick the checkbutton, another window also pops-up.
import tkinter as tk
import sys
from tkinter import messagebox
name=("user")
password=("python3")
def login_in():
if entry1.get() == name and entry2.get() == password:
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")
entry1.set("") # This clears entry1
entry2.set("") # this clears entry2
def close():
log.destroy() #Removes toplevel window
root.destroy() #Removes root window
sys.exit() #Ends the script
def session_end():
messagebox.showinfo("session expired","kindly login again")
def continue_session():
root.deiconify()
root=tk.Tk()
log = tk.Toplevel() #
root.geometry("350x350")
log.geometry("200x200")
entry1 = tk.Entry(log) #Username entry
entry2 = tk.Entry(log) #Password entry
check = tk.Checkbutton(log,text="keep me
login",command=continue_session)#root should stay when you tick checkbutton
button1 = tk.Button(log, text="Login", command=login_in) #Login button
button2 = tk.Button(log, text="Cancel", command=close) #Cancel button
label1 = tk.Label(root, text="This main ui after being idle for 5 minutes \n
You need to login for the main ui to open")
entry1.pack()
entry2.pack()
check.pack()
button1.pack()
button2.pack()
label1.pack()
root.withdraw()
root.after(5000,session_end())
root.mainloop()
EDIT: with the above code have edited it with the answer @Bryan Oakley provided but is the idle timeout the work after 5 minute being idle
import tkinter as tk
import sys
name=("user")
password=("python3")
after_id=None
def reset_timer(event=None):
global after_id
if after_id is not None:
root.after_cancel(after_id)
after_id = root.after(300000,session_end)
def login_in():
if entry1.get() == name and entry2.get() == password:
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")
def close():
log.destroy() #Removes toplevel window
root.destroy() #Removes root window
sys.exit() #Ends the script
def session_end():
messagebox.showinfo("session expired","kindly login again")
def continue_session():
if after_id is not None:
root.deiconify()
def print_some():
print("This is idle timeout")
root=tk.Tk()
log = tk.Toplevel() #
root.geometry("350x350")
log.geometry("200x200")
entry1 = tk.Entry(log) #Username entry
entry2 = tk.Entry(log) #Password entry
check = tk.Checkbutton(log,text="keep me
login",command=continue_session)#root should stay when you tick checkbutton
button1 = tk.Button(log, text="Login", command=login_in) #Login button
button2 = tk.Button(log, text="Cancel", command=close) #Cancel button
label1 = tk.Label(root, text="This main ui after being idle for 5 minutes \n
You need to login for the main ui\to open")
print_button = tk.Button(root,text="print",command=print_some).pack()
entry1.pack()
entry2.pack()
check.pack()
button1.pack()
button2.pack()
label1.pack()
root.withdraw()
root.bind_all("<Any-KeyPress>",reset_timer)
root.bind_all("<Any-ButtonPress>",reset_timer)
root.mainloop()