试图制作简单的计时器来关闭python中的窗口

时间:2017-10-25 19:46:54

标签: windows python-3.x shutdown

尝试使用简单的计时器来关闭python中的窗口(只是为了好玩)但是我遇到了一个无法找到答案的小问题,脚本只是询问用户是否想要使用分钟或秒来关闭使用if,使用秒的部分工作正常,问题是几分钟,脚本得到的时间以分钟为单位转换为秒,而不是运行:subprocess.call([“shutdown”“-s”,“ - t” ,ntime])

但它不起作用,如果我双击file.py并尝试这一部分,脚本就会关闭,但如果IDLE中的执行我收到了这个错误:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\shutdown.py", line 17, in <module>
    subprocess.call(["shutdown" "-s", "-t", ntime])
  File "F:\Programs\python\lib\subprocess.py", line 267, in call
    with Popen(*popenargs, **kwargs) as p:
  File "F:\Programs\python\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "F:\Programs\python\lib\subprocess.py", line 997, in  _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>


Code:
import subprocess
print('0:Seconds')
print('1:Minutes')
minorsec = input('Want to use minutes or seconds? ')
if minorsec == '0':
    time = input('Type the time to shutdown in seconds: ')
    subprocess.call(["shutdown", "-s", "-t", time])
elif minorsec == '1':
    time = input('Type the time to shutdown in minutes: ')
    ntime = int(time) * 60
    subprocess.call(["c:\\windows\\system32\\shutdown.exe" "-s", "-t", str(ntime)])
else:
    print('Error, Press just 0 or 1')
input('Press Enter to close: ') 

1 个答案:

答案 0 :(得分:0)

问题#1:这一行

ntime = time * 60

不按照您的想法行事。 time,从前一个input调用返回的值是一个字符串,而不是整数。因此,如果用户输入“15”作为他的输入,那么ntime就变成疯狂的东西:“1515151515151515151515 ..... 15”。这可能是你的核心问题:

将用户输入的字符串转换回整数:

 ntime = int(time) * 60

问题#2,这是修复#1的副作用:参数列表中subprocess.call的所有值必须是字符串:

time = input('Type the time to shutdown in minutes: ')
ntime = int(time) * 60
subprocess.call(["shutdown" "-s", "-t", str(ntime)])

问题#3:不要使用参数列表:

subprocess.call("shutdown.exe -s -t " + str(ntime))