Python:关闭,重启和休眠的Tkinter

时间:2014-05-28 11:16:46

标签: python python-2.7 tkinter subprocess

我目前正在开发一个小型但基本的应用程序,使用tkinter在我的Windows启动时运行,所以我可以为我要打开的不同内容提供一个小菜单。例如,我目前有按钮来启动我玩的几个游戏和按钮来启动Skype,Steam等。但我也在菜单中添加按钮来关机,重启并使我的电脑睡眠。到目前为止,我所拥有的代码是相当基本的,但仍然在这里:

from Tkinter import *
import os, sys, subprocess

win=Tk()

b1 = Button(win, text = "SKYPE")
b2 = Button(win, text = "STEAM", command = lambda: os.startfile("C:\Program Files (x86)\Steam\Steam.exe"))
b3 = Button(win, text = "GOOGLE")

b4 = Button(win, text = "CS:GO")
b5 = Button(win, text = "RUST")
b6 = Button(win, text = "PPIRACY")
b7 = Button(win, text = "TERRARIA")

b8 = Button(win, text = "SHUTDOWN", command = lambda: subprocess.call(["shutdown.exe", "-f", "-s", "-t", "0"]))
b9 = Button(win, text = "SLEEP", command = lambda: subprocess.call(["sleep.exe", "-f", "-s", "-t", "0"]))
b10 = Button(win, text = "RESTART", command = lambda: subprocess.call(["restart.exe", "-f", "-s", "-t", "0"]))


l = Label(win, text = "Apps")
k = Label(win, text = "Games")
j = Label(win, text = "Misc")

l.grid(row = 0, column = 0, padx = 10, pady = 10)
k.grid(row = 0, column = 1, padx = 10, pady = 10)
j.grid(row = 0, column = 2, padx = 10, pady = 10)

b1.grid(row = 1, column = 0, padx = 10, pady = 10)
b2.grid(row = 2, column = 0, padx = 10, pady = 10)
b3.grid(row = 3, column = 0, padx = 10, pady = 10)

b4.grid(row = 1, column = 1, padx = 10, pady = 10)
b5.grid(row = 2, column = 1, padx = 10, pady = 10)
b6.grid(row = 3, column = 1, padx = 10, pady = 10)
b7.grid(row = 4, column = 1, padx = 10, pady = 10)

b8.grid(row = 1, column = 2, padx = 10, pady = 10)
b9.grid(row = 2, column = 2, padx = 10, pady = 10)
b10.grid(row = 3, column = 2, padx = 10, pady = 10)

mainloop()

正如你所看到的,我的按钮8,9和10都是做这三件事。关机工作正常所以我想也许让我们尝试相同的命令,但使用sleep.exe或restart.exe(我以为我试试看)但显然我收到了错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "C:\Users\NAME\Desktop\test.py", line 17, in <lambda>
    b10 = Button(win, text = "RESTART", command = lambda: subprocess.call(["restart.exe", "-f", "-s", "-t", "0"]))
  File "C:\Python27\lib\subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 709, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

然后再次睡觉,但显然是为了使系统睡眠。

如果不起作用的话,对我来说关闭,睡眠和重启的方法是什么?我目前在Windows 8.1上,如果这有所作为。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

睡眠计算机(来自docs):

b9 = Button(win, text = "SLEEP", command = lambda: subprocess.call(['rundll32.exe', 'powrprof.dll', 'SetSuspendState','0','1','0']))

休眠:

b9 = Button(win, text = "SLEEP", command = lambda: subprocess.call(['rundll32.exe', 'powrprof.dll', 'SetSuspendState']))

重启系统:

b10 = Button(win, text = "RESTART", command = lambda: subprocess.call(["shutdown.exe", "-f", "-r", "-t", "0"]))

答案 1 :(得分:0)

让系统重新启动:

b10 = Button(win, text = "RESTART", command = lambda: subprocess.call(["shutdown.exe", "-f", "-s", "-t", "0"]))

您必须将-s更改为-r才能重新启动。我现在需要解决的唯一部分是计算机没有睡眠-h(休眠)不确定在Windows 8.1上执行此操作的任何其他方法,因为-hg不起作用。