Focus on pygame window - python

I am using
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 6 )
to close the console and leave the pygame window open however I have to click on the window to interact with it. Can I make it so it goes back to the pygame window automatically?

A python script may be run in windowed mode by launching with pythonw, this may be automatically handled by changing the file extension to .pyw.
See this answer for more detailed information.

Related

How to open a Pygame window and a Tkinter window simultaneously?

I'm making a simple game using the Pygame Module. I require a Tkinter window to be open alongside the Pygame window.
Whenever I try to open both the windows, the second window only opens after I kill the first one.
Now, the only solution I can think of, is using multi-threading. But, I'm not able to implement it.
How do I go about this?
I would really appreciate some help here.
Thank You!
There is a fundamental design issue in pygame that makes it unable to open a window if the process already has a window. It will also prevent other windows from opening while it is running. However, you can open as many TK windows as you like and you can embed a pygame window inside a SDL drawing frame inside a TK window.
See this answer on how to use the drawing frame: Embedding a Pygame window into a Tkinter or WxPython frame
See this answer on how to create multiple windows in tkinter: How to open multiple windows in Tkinter

Screenshot with python pyautogui on windows - how to prevent opening shell or cmd

I want to take a screenshot with python using pyautogui. This is my code:
import pyautogui
im1 = pyautogui.screenshot("my_screen.png")
My problem is that this way half of my screen is blocked by the python shell, which pops up when I start the code. So the picture doesn´t really is a screenshot. Prior I have used python.exe to run python files and then the problem was the cmd window poping up. Now I use pythonw.exe, because apparently this prevents the cmd window to show up, but now the python shell window blocks my screen.
So does anyone know how I can take a clean screenshot?
Well, the solution was quite simple. After trying to take a screenshot with pillow I got the same result. So the answear is in the configuration of Spyder, which I was using. Under configuration and "run" you can change where the code will run. And I had chosen that the code shall ran in a external window. So I changed to internal and everything works as it should.
make it sleep for 5 seconds maybe? enough to minimize what is not necessary to capture
import pyautogui
pyautogui.sleep(5)
im1 = pyautogui.screenshot("my_screen.png")

Terminal window appears along with pygame window upon launch of the exe

I made a game using pygame and built it using cx_Freeze, now when I run the executable it launches a console window along with the pygame window, is there any way to remove/disable this window
I solved it
In the cx_Freeze setup file you need to add the line
base = “WIN32GUI”
When declaring your exeacutable

What is this Python Icon?

Sometimes when I run python scripts in terminal a python instance opens up
I've seen matplotlib and ghost cause this. The only way to close it seems to be to close the python terminal.
What exactly is this instance and how do I have it close automatically after my script executes?
On OS X the Dock will display an icon for Python whenever you do something that interacts with the GUI. The most common reason for this is using matplotlib with a GUI backend (e.g. PyQt). When using an interactive backend (e.g. PyQt) you will normally see a rocket icon after you call plt.show() which will remain on the Dock until after the window is closed and process that interacted with the GUI exits.
When you are working interactively in the Python shell, the icon stays there not because the Terminal is still open (or script is still executing) but because the Python process is still active. This process is probably maintaining a GUI event loop in the background.
There are a few solutions:
In matplotlib you can use plt.show(block=False) to detach the event loop, meaning the icon will disappear once the window is closed.
Set a non-interactive backend, e.g. matplotlib.use("Agg") to avoid creating the windows at all. Of course, you can't see anything.
Modify the Python.app .plist to suppress icons.
For the latter, the instructions here (Dr. Drang) are as follows. Note this is for the system Python, for other locations you'll need to find the .plist elsewhere:
cd /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/
# NB the following file is binary, use a compatible editor or convert with
sudo plutil -convert xml1 Info.plist # Convert to XML
sudo chmod 666 Info.plist # Make editable
Then add the following before the final </dict>
<key>LSUIElement</key>
<true/>
Return to binary and fix permissions:
sudo chmod 644 Info.plist
sudo plutil -convert binary1 Info.plist
PyCharm doesn't consider the script to be complete until any popups it spawns are closed. This is evident when you run matplotlib and a plot pops up. If PyCharm closed the window upon completion of the script, the window would immediately close before you could even view your chart.
There are two ways to finish the script. Either close the popups, or click the stop button in PyCharm.

How to make PyQt App give up window focus?

I have a python command line script that starts a PyQt Application showing the input of the video device in a little window. Then it starts a curses screen so that the user can control the camera with the keyboard.
After starting the script, the window focus is automatically on the PyQt window. Is there any way to tell PyQt to give up the window focus or curses / the terminal to reclaim it?
I use Linux (Mint Cinnamon) and the solution does not have to be OS independent.
You can try with setFocusPolicy here is the documentation. http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#setFocusPolicy

Categories