如何在Tkinter(PYTHON 3.6.1)中为GUI设置空闲超时
我希望我的GUI在您登录时空闲5分钟后再次请求登录,但勾选复选按钮时根窗口应保持不变 挑战:如何在Tkinter(PYTHON 3.6.1)中为GUI设置空闲超时,python,user-interface,tkinter,Python,User Interface,Tkinter,我希望我的GUI在您登录时空闲5分钟后再次请求登录,但勾选复选按钮时根窗口应保持不变 挑战: 我一运行脚本,就会弹出“session_ends”函数下解析的messagebox 当我在entry小部件中键入名称和密码并勾选checkbutton时,另一个窗口也会弹出 import tkinter as tk import sys from tkinter import messagebox name=("user") password=("python3") def login_in():
我一运行脚本,就会弹出“session_ends”函数下解析的messagebox 当我在entry小部件中键入名称和密码并勾选checkbutton时,另一个窗口也会弹出
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()
编辑:
使用上面的代码,我们使用提供的答案@Bryan Oakley对其进行了编辑,但是空闲超时是否是空闲5分钟后的工作
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()
将tkinter作为tk导入
导入系统
名称=(“用户”)
密码=(“python3”)
在\u id=无之后
def复位定时器(事件=无):
全局后处理id
如果after_id不是None:
root.after\u取消(after\u id)
after\u id=root.after(300000,会话结束)
def login_in():
如果entry1.get()==名称和entry2.get()==密码:
root.deiconify()
log.destroy()
其他:
messagebox.淋浴错误(“错误”,“登录失败”)
def close():
log.destroy()#删除顶层窗口
root.destroy()#删除根窗口
sys.exit()#结束脚本
def会话_end():
messagebox.showinfo(“会话已过期”,“请再次登录”)
def continue_session():
如果after_id不是None:
root.deiconify()
def print_some():
打印(“这是空闲超时”)
root=tk.tk()
log=tk.Toplevel()#
根几何(“350x350”)
对数几何(“200x200”)
entry1=tk.Entry(log)#用户名项
entry2=tk.Entry(log)#密码输入
check=tk.Checkbutton(log,text=“保留我
登录”,命令=继续(会话)#勾选复选按钮时,root应保持不变
button1=tk.按钮(log,text=“Login”,command=Login_in)#登录按钮
button2=tk.按钮(log,text=“Cancel”,command=close)#取消按钮
label1=tk.Label(root,text=“空闲5分钟后此主ui\n
您需要登录才能打开主ui
print\u button=tk.button(root,text=“print”,command=print\u some).pack()
entry1.pack()
entry2.pack()
check.pack()
按钮1.pack()
按钮2.pack()
标签1.pack()
root.draw()
root.bind\u all(“,重置\u计时器)
root.bind\u all(“,重置\u计时器)
root.mainloop()
如何在Tkinter(PYTHON 3.6.1)中为GUI设置空闲超时
一个简单的解决方案是创建一个将结束会话的函数,然后安排该函数在将来运行。您正在尝试此操作,但在之后调用的不正确
使其成为真正的空闲超时的关键是知道用户何时“空闲”。一个简单的解决方案是为
和
第一件事是创建一个全局变量或实例变量,以记住在
之后使用运行的命令的id:
after_id = None
下一步是创建重置计时器的函数。它需要接受一个事件,因为tkinter会自动传递一个事件。但是,我们希望能够直接调用该函数,因此我们将使其成为可选的
def reset_timer(event=None):
global after_id
if after_id is not None:
root.after_cancel(after_id)
after_id = root.after(5000, session_end)
要启动会话,我们可以直接调用函数:
reset_timer()
接下来,我们可以设置绑定来重置用户活动的计时器。在本例中,我将活动定义为任何按钮或按键。我们这样做:
root.bind_all("<Any-KeyPress>", reset_timer)
root.bind_all("<Any-ButtonPress>", reset_timer)
root.bind\u all(“,重置\u计时器)
root.bind\u all(“,重置\u计时器)
如何在Tkinter(PYTHON 3.6.1)中为GUI设置空闲超时
一个简单的解决方案是创建一个将结束会话的函数,然后安排该函数在将来运行。您正在尝试此操作,但在
之后调用的不正确
使其成为真正的空闲超时的关键是知道用户何时“空闲”。一个简单的解决方案是为
和
第一件事是创建一个全局变量或实例变量,以记住在
之后使用运行的命令的id:
after_id = None
下一步是创建重置计时器的函数。它需要接受一个事件,因为tkinter会自动传递一个事件。但是,我们希望能够直接调用该函数,因此我们将使其成为可选的
def reset_timer(event=None):
global after_id
if after_id is not None:
root.after_cancel(after_id)
after_id = root.after(5000, session_end)
要启动会话,我们可以直接调用函数:
reset_timer()
接下来,我们可以设置绑定来重置用户活动的计时器。在本例中,我将活动定义为任何按钮或按键。我们这样做:
root.bind_all("<Any-KeyPress>", reset_timer)
root.bind_all("<Any-ButtonPress>", reset_timer)
root.bind\u all(“,重置\u计时器)
root.bind\u all(“,重置\u计时器)
超时=30000
def复位定时器(事件=无):
全球超时
如果事件不是无:
超时=30000
其他:
通过
def减少超时()
全球超时
超时=超时-1000
打印(超时)
root.after(1000,减少超时)
如果超时=0:
log.destroy()#删除顶层窗口
root.destroy()#删除根窗口
messagebox.showinfo(“会话已过期”,“请再次登录”)
sys.exit()
root.after(1000,减少超时)
root.bind\u all(“,重置\u计时器)
root.bind\u all(“,重置\u计时器)
超时=30000
def复位定时器(事件=无):
全球超时
如果事件不是无:
超时=30000
其他:
通过
def减少超时()
全球超时
超时=超时-1000
打印(超时)
root.after(1000,减少超时)
如果超时=0:
log.destroy()#删除顶层窗口
root.destroy()#删除根窗口
messagebox.showinfo(“会话已过期”,“请再次登录”)
sys.exit()
root.after(1000,减少超时)
root.bind\u all(“,重置\u计时器)
root.bind\u all(“,重置\u计时器)
问题的一部分是,您在之后使用的不正确。您所做的与session_end()相同;root.after(5000,无)
。请参阅,部分问题在于您在
之后使用了不正确的。您所做的与session_end()相同;root.after(5000,无)
。请看@Bryan Oakley,我将尝试您的建议并用结果更新您。非常感谢。我已经按照您的建议完成了,并添加了“打印按钮”