我目前正在开发一个小型但基本的应用程序,使用 tkinter 在我的 Windows 启动上运行,这样我就可以为我想要打开的不同内容提供一个小菜单。例如,我目前有启动我玩的一些游戏的按钮以及启动 Skype、Steam 等的按钮。但我还在菜单中添加了关闭、重新启动和让我的计算机 sleep 的按钮。到目前为止,我的代码相当基本,但仍然在这里:
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
然后 sleep 再次相同,但显然是为了让系统 sleep 。
如果这不起作用,我该如何关闭、 sleep 和重新启动?我目前使用的是 Windows 8.1(如果这有什么影响的话)。感谢您的帮助。
最佳答案
要让系统重新启动就行:
b10 = Button(win, text = "RESTART", command = lambda: subprocess.call(["shutdown.exe", "-f", "-s", "-t", "0"]))
您必须将 -s 更改为 -r 才能重新启动。我现在唯一需要修复的部分是计算机没有使用 -h (休眠) sleep ,不确定是否有其他方法可以在 Windows 8.1 上执行此操作,因为 -hg 不起作用。
关于Python:Tkinter 关机、重启和 sleep ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23910150/