-1

I have a GUI program on python 3 and a file.txt which contains some launch options. One of them is :

Console (Yes/No) = ...

The thing is i want to display or not the console if there is a "Yes" or a "No".

I found a way to do so :

In script1.pyw:

import os

def getParam():
    param = open("file.txt","r").read()
    param.split(" = ")[1] # getting the value of "Console = "

    return(param)

param = getParam()

if "script2.py" in listdir():

    if param == "No":
        os.rename("script2.py","script2.pyw")
        os.system("python script2.pyw")

    elif param == "Yes":
        os.system("python script2.py")

if "script2.py" in listdir():

    if param == "Yes":
        os.rename("script2.pyw","script2.py")
        os.system("python script2.py")

    elif param == "No":
        os.system("python script2.pyw")

In script2.py(w):

from tkinter import *

window = tk.Tk()

...

window.mainloop()

Fortunately it works but as I work on Linux and I want to share my program with people on Windows I encountered a problem : when they launch script1.pyw everything goes well but whatever they put in file.txt, the python's shell opens with it (and it is absolutely not what I want...)

Do someone have any idea?

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
Ulquiro
  • 11
  • 1
  • 4
  • FYI you don't need to come up with your own format for configuration: https://docs.python.org/2/library/configparser.html – jonrsharpe Jan 08 '17 at 10:14
  • Possible duplicate of [Run Python script without Windows console appearing](http://stackoverflow.com/questions/1689015/run-python-script-without-windows-console-appearing) – jonrsharpe Jan 08 '17 at 10:16

2 Answers2

0

You will need to change the os.system call to subprocess.call in order to skip the black window, maybe something like this:

subprocess.call(['py', '3.4', '-c', "import time; time.sleep(1)"], shell=True)

The shell=True part is what keeps the console from showing...

Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36
0

You need to call the pythow.exe on Windows:

pythonw script2.pyw

Better don't use os.sytem. It is recommend to use subprocess:

import sys

if sys.platform == 'win32':
    subprocess.call(['pythonw',  'script2.pyw'], shell=True)

The os.sytem docs recommend subprocess:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • 1
    It's best to avoid using `shell=True`. In this case it's completely unnecessary because pythonw.exe is not a console application. However, even if the target is a console app, such as python.exe, using `shell=True` just to get a hidden console is generally a bad idea. The better options are either to set the `startupinfo` yourself to hide the console window or to use a `creationflags` value of either `CREATE_NO_WINDOW` or `DETACHED_PROCESS`. See [this answer](http://stackoverflow.com/a/7006424/205580) for examples. – Eryk Sun Jan 08 '17 at 13:40