python定时关机小程序

程序:

import os, time

rh = int(time.strftime("%H", time.localtime())) # 系统时间H

rm = int(time.strftime("%M", time.localtime())) # 系统时间M
cmd = "cmd.exe /k shutdown -s -t 0"


c1 = True
while c1:
    try:
        h = int(raw_input("please input the hour:"))
        if h >= 0 and h <= 23:
            c1 = False
        else:
            continue
    except:
        continue


c2 = True
while c2:
    try:
        m = int(raw_input("please input the minute:"))
        if m >= 0 and m <= 59:
            c2 = False
        else:
            continue
    except:
        continue


if h == rh:
    if m <= rm:
        os.system(cmd)
    else:
        time.sleep((m-rm) * 60)
        os.system(cmd)
elif h > rh:
    tem1 = (h - rh - 1) * 3600 + (60 - rm + m) * 60
    time.sleep(tem1)
    os.system(cmd)
else:
    tem2 = (23 - rh + h) * 3600 + (60 - rm + m) * 60
    time.sleep(tem2)

    os.system(cmd)


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过os模块中的system函数来实现定时关机。具体实现方法可以参考以下代码: ```python import os # 设置定时关机时间(单位为秒) shutdown_time = 60 * 60 # 执行定时关机命令 os.system("shutdown -s -t %d" % shutdown_time) ``` 以上代码会在运行后1小时自动关机。其中,`shutdown -s`表示关机命令,`-t`后面跟的数字表示定时关机的时间(单位为秒)。 如果想要实现更加灵活的定时关机,可以结合tkinter模块实现一个简单的GUI界面,用户可以在界面上选择关机、重启、睡眠或取消定时关机,并设置定时时间。具体实现方法可以参考以下代码: ```python from tkinter import * import os # 创建GUI界面 window = Tk() window.geometry('300x200') window.title('定时关机') # 添加控件 l1 = Label(window, text="时间(分钟)") l1.pack() e1 = Entry(window) e1.pack() radio = IntVar() R1 = Radiobutton(window, text="关机", variable=radio, value=1) R1.pack(anchor=W) R2 = Radiobutton(window, text="重启", variable=radio, value=2) R2.pack(anchor=W) R3 = Radiobutton(window, text="睡眠", variable=radio, value=3) R3.pack(anchor=W) R4 = Radiobutton(window, text="取消定时关机", variable=radio, value=4) R4.pack(anchor=W) def input(): # 根据用户选择的操作类型设置命令参数 a = int(radio.get()) global syst if a == 1: syst = '-s' elif a == 2: syst = '-r' elif a == 3: syst = '-l' elif a == 4: os.system('shutdown -a') def func1(): # 获取用户设置的定时时间,并执行定时关机命令 p = int(e1.get()) * 60 time = " -t " + str(p) os.system(str("shutdown ") + syst + time) button = Button(window, text='start', command=func1) button.pack() window.mainloop() ``` 以上代码会创建一个简单的GUI界面,用户可以在界面上选择关机、重启、睡眠或取消定时关机,并设置定时时间。点击“start”按钮后,程序会根据用户的选择执行相应的命令,实现定时关机功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值