Hide Python console when running Tkinter script from shell command - python

I have extended the file context menu in the Windows registry and added a shortcut to a Python script (which is a Tkinter window). It runs fine, but obviously with the Python console in the background. I tried to change the shell command from python to pythonw (and renaming the file to .pyw) but then Windows always simply opens the "open with..." dialog and not the script. If I copy-paste the shell command and run it from cmd, it works. Running the .pyw from the explorer also works fine, without console.
Why is the behavior different when running it in the shell command?
This is in my .reg setup file under the command key:
#="pythonw \"C:\\test_script.pyw\" \"%1\""

Related

Why does PowerShell run in the background when I'm running a Python program?

I have created a Python program with a user interface and converted it to (exe) format.
And when I run the program, PowerShell opens in the background behind the program's interface.
How can I run the program without PowerShell appearing? Can it run in the background?
You should use pythonw.exe to run in the background.
If you already have the executable, try changing the extensions from .py to .pyw
See this similar question:
pythonw.exe or python.exe?

How can I determine the method used to launch my python script in Windows?

Is there a way to differentiate between a .py file launched by double-clicking its icon in Windows versus typing myscript.py in an existing command prompt window? My code ends with os.system("pause") so that I can see the results when double-clicking to launch, but that line should be ignored when running it in a persistent window.
I would first recommend you to go to advanced settings of your Windows Explorer and activate the "show extensions" options if that can help.
For more information about your question, you can also check this link :
https://automatetheboringstuff.com/appendixb/

Run terminal inside VS Code for python script?

I'm on Linux Mint running VSCode and I was somehow able to run a terminal not as a separate window but right below an open Python file. Seems to be easy on Win/OSX (Ctrl/Cmd+J and select Terminal tab) but not specifically a feature that I can choose when I'm on a Linux machine. Any special keys to bring it back?
In general, Ctrl-` opens the integrated terminal in VS Code. Otherwise look under the View menu for the Integrated Terminal option. If you're looking for Python-specific options, there are Run Python File in Terminal and Run Python Selection/Line in Terminal commands from the command palette.

Execute python script directly, shorcut and execute from startup

I did a application with Python 3 using Tkinter gui, on Raspbian with the RPi. I have three questions:
1) I've turned the python script into executable using:
chmod -x path/myfile.py
I've declared on the first line of script
#!/usr/bin/env python3
When I double click onthe script it pops up a windows asking if I want to execute normally or on the terminal. If i chose Execute, the script runs normally.
Is there any way to execute normally directly, without asking?
2) How do i create a shortcut on my Desktop to execute my python script?
I've created a file on the desktop and I edited the following:
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Name of the shortcut
Comment=Comment
Icon=path/pic.gif
Exec=python3 path/myfile.py
Terminal=false
The picture if the shortcut works, but the script it's not launched. It appears the hourglass and nothing happens.
3) How do I make a python script launch when at the startup of Raspbian?
I've already edited the
/etc/xdg/lxsession/LXDE/autostart
and also the
/etc/xdg/lxsession/LXDE-pi/autostart
with:
#python3 path/myfile.py
on the last line of the file. Rebooting the system, nothing happens.
Thank you for your help.

Failed with running subprocess from Python launcher without terminal window on Mac

My ultimate goal is to run a Python script (the code need to call some subprocess) without terminal.
I tried with python launcher on Mac. The code works correctly when the "Run in a Terminal window" is checked.
(Notice that the shell=False is default here.) However, it won't run the subprocess correctly when I uncheck that option.
I also tried to run in with Mac OS automator (Run Shell Script) — negative too.
Here is a small sample code.
import subprocess
with open("record","w") as f:
subprocess.call(["which","ipython"], stdout = f) # this line fails
subprocess.call(["open","."]) # this line always works
If I run from terminal, it creates a record file, which shows the ipython path. However when I run it from automator or python launcher without terminal window, it creates an empty file.

Categories