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

相关推荐
旦莫13 分钟前
Python 教程:我们可以给 Python 文件起中文名吗?
开发语言·python
豌豆花下猫30 分钟前
Python 潮流周刊#99:如何在生产环境中运行 Python?(摘要)
后端·python·ai
小杨40433 分钟前
python入门系列二十(peewee)
人工智能·python·pycharm
弧襪34 分钟前
FlaskRestfulAPI接口的初步认识
python·flaskrestfulapi
船长@Quant36 分钟前
文档构建:Sphinx全面使用指南 — 进阶篇
python·markdown·sphinx·文档构建
cloudy49138 分钟前
强化学习:历史基金净产值,学习最大化长期收益
python·强化学习
剁椒排骨39 分钟前
win11什么都不动之后一段时间黑屏桌面无法显示,但鼠标仍可移动,得要熄屏之后才能进入的四种解决方法
运维·windows·经验分享·计算机外设·win11·win10
Bruce_Liuxiaowei1 小时前
使用Python脚本在Mac上彻底清除Chrome浏览历史:开发实战与隐私保护指南
chrome·python·macos
李菠菜1 小时前
Windows Terminal 集成 Git Bash 的简洁配置指南
windows·git
ruyingcai6666661 小时前
用python进行OCR识别
开发语言·python·ocr