[Python] 纯文本查看 复制代码 import ctypes
import os
import tkinter as tk
from tkinter import messagebox
# 获取计算机空闲时间的函数
def get_idle_duration():
# 仅在Windows上有效
class LASTINPUTINFO(ctypes.Structure):
_fields_ = [("cbSize", ctypes.c_uint),
("dwTime", ctypes.c_uint)]
lii = LASTINPUTINFO()
lii.cbSize = ctypes.sizeof(LASTINPUTINFO)
ctypes.windll.user32.GetLastInputInfo(ctypes.byref(lii))
millis = ctypes.windll.kernel32.GetTickCount() - lii.dwTime
return millis / 1000.0 # 将毫秒转换为秒
# 关闭屏幕的函数
def turn_off_screen():
os.system('powershell.exe Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend, $false, $false)')
# 检查是否关闭屏幕的函数
def check_and_turn_off():
idle_time = get_idle_duration()
try:
trigger_time = int(entry.get())
if idle_time >= trigger_time:
turn_off_screen()
else:
root.after(1000, check_and_turn_off) # 1秒后再次检查
except ValueError:
messagebox.showerror("输入无效", "请输入有效的数字")
# 设置和开始检查的函数
def schedule_off():
root.after(1000, check_and_turn_off)
# 创建主窗口
root = tk.Tk()
root.title("自动息屏工具")
label = tk.Label(root, text="输入无操作状态下息屏时间(秒):")
label.pack(pady=10)
entry = tk.Entry(root)
entry.pack(pady=10, padx=10)
button = tk.Button(root, text="设置时间", command=schedule_off)
button.pack(pady=10)
root.geometry("300x150")
root.mainloop()
写了一段,如果需要的话可以帮你编译出来~!
|