2

After writing the script below(which works perfectly), I open the cmd.exe windows prompt and type the following

pyinstaller -F --windowed myscript.py

which gives me a file called "myscript.exe".

The problem is when I open the executable and press the button, nothing happens. I think there is a problem with this line:

check_output("shutdown -s -t 60", shell=True)  

Even though the script works "as a script", it doesn't work as an executable.
I've tried other syntax like

os.system("shutdown -s -t 60") 

but they don't seem to work.

from tkinter import *
from subprocess import check_output,CalledProcessError

class main_gui:
    def __init__(self,master):
        self.master=master
        master.geometry("250x100")
        self.button1=Button(self.master,
                            text="Press me",
                            font="Times 10 bold",
                            command=self.shutdown)
        self.button1.pack()

    def shutdown(self):
        try:
            check_output("shutdown -s -t 60", shell=True)
            print("Computer will shutdown in 60 seconds")
        except CalledProcessError:
            print("Already pressed")

root = Tk()
my_gui = main_gui(root)
root.mainloop()

What can I do?

0

1 Answer 1

2

What you can do:

using:

import subprocess
subprocess.call(["shutdown", "-f", "-s", "-t", "60"])

this will work. with --windowed

It seems thats there is a problem with check_output with --windowed flag :/

Edit1:

based on eryksun comments. Also was my research results was but now it seems proof.

Use check_call and a creation flag to avoid creating a console window. For example: CREATE_NO_WINDOW = 0x08000000; check_call('shutdown -s -t 60', creationflags=CREATE_NO_WINDOW).

Regarding check_output, since it overrides stdout, Popen has to also duplicate inheritable copies of the existing stdin and stderr handles. This will fail if they're invalid. When run from a console in Windows 7, a GUI can inherit invalid standard handles. A workaround is to override all 3 handles. For example: output = check_output('shutdown -s -t 60', stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=CREATE_NO_WINDOW

Edit2:

You can also add a icon directly with pyinstaller...
Reference: Windows and Mac OS X specific options

Sign up to request clarification or add additional context in comments.

7 Comments

Use check_call and a creation flag to avoid creating a console window. For example: CREATE_NO_WINDOW = 0x08000000; check_call('shutdown -s -t 60', creationflags=CREATE_NO_WINDOW).
Regarding check_output, since it overrides stdout, Popen has to also duplicate inheritable copies of the existing stdin and stderr handles. This will fail if they're invalid. When run from a console in Windows 7, a GUI can inherit invalid standard handles. A workaround is to override all 3 handles. For example: output = check_output('shutdown -s -t 60', stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=CREATE_NO_WINDOW).
Using the above method worked on the mentioned script but when I tried it with a longer one, the button somehow opened another instance of the application.
EDIT: Sometimes it works (the longer script), sometimes it doesn't. Sometimes the button opens another instance of the program without executing the command. I created two executables and then put them in the same directory, none of them works, when I deleted one of them, the other magically started working.
@SinaUser, we don't have enough information. Use logging to write errors to a file, instead of depending on printing information to stdout and exceptions to stderr. They aren't available by default in a GUI process.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.