定时关机(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)

相关推荐
代码欢乐豆15 分钟前
scikit-learn零基础配置(含python、anaconda)
python·机器学习·scikit-learn
Java水解26 分钟前
Python数据库操作:SQLAlchemy ORM指南
python
悠哉悠哉愿意35 分钟前
【数学建模学习笔记】无监督聚类模型:分层聚类
笔记·python·学习·数学建模
eleqi1 小时前
Python+DRVT 从外部调用 Revit:批量创建门
python·系统集成·bim·revit·drvt·自动生产流水线
先做个垃圾出来………1 小时前
PyTorch 模型文件介绍
人工智能·pytorch·python
浅醉樱花雨1 小时前
vosk语音识别实战
人工智能·python·语音识别·asr·vosk
再努力"亿"点点1 小时前
爬取m3u8视频完整教程
开发语言·python
悟能不能悟2 小时前
if __name__=‘__main__‘的用处
python
Source.Liu2 小时前
【Python基础】 15 Rust 与 Python 基本类型对比笔记
笔记·python·rust
前端世界2 小时前
Python 正则表达式实战:用 Match 对象轻松解析拼接数据流
python·正则表达式·php