定时关机(python)

内容

为了更简单地实现定时关机,不必去记繁琐的命令行,于是搞了一个gui出来

基于python和Windows命令行实现,核心为:

bash 复制代码
shutdown -s -t 60

代码

python 复制代码
# -*- coding: utf-8 -*-
# Environment    PyCharm
# File_name   autoShutdownPro |User    Pfolg
# 2024/10/09 12:44
import os
import tkinter as tk


# print("程序开始运行时会取消已设置的定时关机")
#
# try:
#     time = int(input('计划在多少分钟后关机(整数) 或者 回车取消定时关机:'))
#     basic_time = time * 60
#     text = f'shutdown -s -t {basic_time}'
#     os.system(text)
#     input(f"已设置,您的系统将于{basic_time}秒后关闭\n回车开始:")
# except ValueError:
#     input("回车退出:")
def autoShutdown(time):
    if time:
        os.system(f"shutdown -s -t {time}")


if __name__ == '__main__':
    os.system("shutdown -a")
    # 窗口设置
    window = tk.Tk()
    w, h = window.winfo_screenwidth(), window.winfo_screenheight()
    window.geometry(f"{int(w / 4)}x{int(h / 4)}+{int(w / 4)}+{int(h / 4)}")
    window.title("Auto_Shutdown_Pro")
    # 标签
    tk.Label(window, text="Time Setting", font=("微软雅黑 bold", 16)).place(relx=.02, rely=.02)

    # 控件设置
    day, hour, minute, second = tk.IntVar(), tk.IntVar(), tk.IntVar(), tk.IntVar()
    # day
    tk.Entry(window, width=8, textvariable=day).place(relx=.25, rely=.25)
    tk.Label(window, text="Day:").place(relx=.1, rely=.25)
    # hour
    tk.Entry(window, width=8, textvariable=hour).place(relx=.25, rely=.4)
    tk.Label(window, text="Hour:").place(relx=.1, rely=.4)
    # minute
    tk.Entry(window, width=8, textvariable=minute).place(relx=.25, rely=.55)
    tk.Label(window, text="Min:").place(relx=.1, rely=.55)
    # second
    tk.Entry(window, width=8, textvariable=second).place(relx=.25, rely=.7)
    tk.Label(window, text="Second:").place(relx=.1, rely=.7)

    # 标签及按钮
    tk.Label(window, text="shutdown -a\nshutdown -s -t 60\nIt will run \ncommand one at first.").place(relx=.6, rely=.3)
    tk.Button(
        window, text="Set", width=8, command=lambda: autoShutdown(
            (
                    (
                            int(day.get()) * 24 + int(hour.get())
                    ) * 60 + int(minute.get())
            ) * 60 + int(second.get())
        )
    ).place(relx=.55, rely=.7)
    tk.Button(window, text="Cancel", width=8, command=lambda: os.system("shutdown -a")).place(relx=.75,rely=.7)

    window.mainloop()

实现效果

其实我主要是为了学习剪视频,虽然两个都比较烂......

定时关机程序

如何获取

单一程序及源码:

Release AutoShutdownPro · Pfolg/Pfolg_Source (github.com)

集成工具箱:

Pfolg/PGBox: 平时写的小程序的大杂烩------整合后的工具箱,主要功能基于windows系统实现 (github.com)

相关推荐
费弗里3 分钟前
Python全栈应用开发利器Dash 3.x新版本介绍(4)
python·dash
辣辣y11 分钟前
python基础day08
开发语言·python
花海如潮淹25 分钟前
硬件产品研发管理工具实战指南
前端·python
张彦峰ZYF35 分钟前
快速掌握Python编程基础
python
Json____1 小时前
使用python的 FastApi框架开发图书管理系统-前后端分离项目分享
开发语言·python·fastapi·图书管理系统·图书·项目练习
安思派Anspire1 小时前
LangGraph + MCP + Ollama:构建强大代理 AI 的关键(二)
人工智能·后端·python
站大爷IP1 小时前
Python文件与目录比较全攻略:从基础操作到性能优化
python
摆烂工程师1 小时前
(5千字总结)国内如何安装和使用 Claude Code 的保姆级教程 - 支持Mac和Windows用户
windows·macos·claude
ahead~2 小时前
【大模型入门】访问GPT_API实战案例
人工智能·python·gpt·大语言模型llm
大模型真好玩3 小时前
准确率飙升!GraphRAG如何利用知识图谱提升RAG答案质量(额外篇)——大规模文本数据下GraphRAG实战
人工智能·python·mcp