Python: Tkinter to Shutdown, Restart and Sleep
Question: I am currently developing a small but basic applictaion using tkinter to run
on my windows startup so I can have a little menu for the different things I
want to open. For example, I currently have buttons to launch a few games I
play and buttons to launch Skype, Steam etc. But I am also adding buttons to
the menu to Shutdown, Restart and make my computer Sleep. SO far the code I
have is fairly basic but still here it is:

    
    
    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()
    

As you can see my buttons 8, 9 and 10 are all to do these three things. The
shutdown works fine so I thought maybe lets try the same command but with
sleep.exe or restart.exe (Thought I'd give it a try) But obviously I am
getting the error

    
    
    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
    

And then for sleep the same again but obviously for making the system sleep.

What would be the way for me to go about shutdown, sleep and restart for me
then if that does not work? I am currently on Windows 8.1 if that makes a
difference. Thanks for any help.


Answer: To get the system to restart on the line of:

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

You must change the -s to a -r to restart. The only part I need to fix now is
the computer not sleeping with -h (hibernate) Not sure of any other ways to do
this on windows 8.1 as -hg does not work.

