So I'm playing with python3 on Cloud9, got interested in trying pygame and found a recommendation to install cloud9-vnc for a desktop display. Got both of those things to work, but not in tandem. I'm a rather newbish with Linux and VNC, so I'm stuck at the moment. Any chance I can get pygame output on a VNC desktop?
What I have so far is that I've installed pygame using this and cloud9-vnc using this. Pygame import and commands run smoothly (both in terminal and script) and when I run the script with c9vnc I get the link to a VNC desktop. However, the desktop is clear, apart from Ubuntu logo.
The program doesn't actually seems to be running, considering that it doesn't display the text to be printed
In fact, it seems that it's not even started to run.
However, inside the VNC desktop I have the access to complete cloud9 workspace, including installed pygame, which does work, albeit a bit more clunky, compared to cloud9's interface.
So what I want to ask is is there a way for me to write and run a code on cloud9's default interface that would directly display the output in VNC's desktop, with little to no additional interaction?
Related
I'm primarily using Windows, where I run WSL2. So from a python script running in the subsystem, I would like to screenshot whatever is on windows monitor, as simple as such:
v1
import mss
import os
os.environ['DISPLAY'] = ':0'
with mss.mss() as sct:
sct.shot()
This gives only gives "Segmentation fault" error and no image. So I tried to setup vcxsrv in Windows and I'm able to open stuff from my subsystem in Windows through the server, however I cant get it the other way around..
I just want access to the windows screen so I can screenshot it. Any help about how to access the monitor through wsl would be greatly appreciated, I can't find much on google..
The problem with your attempted solution is that the WSL/Linux Python's mss, as you've found, isn't able to capture the Windows desktop. Being the Linux version of MSS, it will only be able to communicate with Linux processes and protocols like X. Starting up VcXsrv might get you part of the way there, in that you might be able to capture X output, but you may need to be running the Python app from inside a terminal that is running in a X window.
Regardless, you've said that your goal is to capture the entire Windows desktop, not just the X output in VcXsrv. To do that, you'll need to use a Windows process of some sort.
But don't worry; using WSL's interop, you can still do that from inside WSL/Linux Python. We just need to call out to a Windows .exe of some sort.
There are literally dozens of third-party apps that you could use in Windows to create a screenshot. But I prefer to use a solution that doesn't require any additional installation.
So I resorted to PowerShell for this, since you can easily call powershell.exe and pass in a script from WSL. There are a number of examples here on Stack Overflow, but I ended up going slightly "lower tech" to try to simplify a bit. The code here is most similar to this solution, so refer to that if you want to expand on this.
From WSL/Linux Python:
import os
os.system("""
powershell.exe \"
Add-Type -AssemblyName System.Windows.Forms
[Windows.Forms.Sendkeys]::SendWait('+{Prtsc}')
\$img = [Windows.Forms.Clipboard]::GetImage()
\$img.Save(\\\"\$env:USERPROFILE\\Pictures\\Screenshots\\screenshot.jpg\\\", [Drawing.Imaging.ImageFormat]::Jpeg)\"
""")
That essentially sends the ShiftPrintScreen key chord to capture the current desktop to the clipboard, then saves the clipboard. It can get slightly hairy with the quoting, since you are essentially wrapping PowerShell inside a /bin/sh inside a Python script.
Note that, even though you are in Linux Python, since it's the Windows PowerShell that we are calling, it takes the Windows path format (C:\Users\Username\Pictures...) rather than the Linux version (/mnt/c/Users/...).
While I didn't have any timing issues with this, you may need to insert small delays. Again, refer to the existing answer for that. This solution is primarily to explain how to do it through WSL's Python using PowerShell.
if you are on a laptop you could hit the windows button and prtsc to take a screenshot and if you are on PC you could use OBS its recording software that can do pretty much anything.
I have a rather small Pygame based python script and I just bought a Mac so I'm trying to run my script on here. Everything has been installed correctly (Checking by typing 'import Pygame') into python terminal as-well as running a basic hello world program. However, when I try running this script, IDLE gets brought to the front but nothing happens from there. (The script is supposed to pop-up a new window). Is this a problem with my installations or my code? (Code works on windows setup).
You could try to run the script again on a windows computer to see if you forgot to save some changes or if the file got corrupted somehow. You could also use bootcamp to install Windows on your mac since you could have one windows version on your mac for coding purposes and the regular mac OS for other stuff if you like.
you can execute it by left clicking in your script and select open with python launcher
I've created a simple script that executes a "moving mouse and keyboard" sequence. Although currently to get this to work I use a Shell (Idle) to run and that is to slow with boot up time and such.
Is there a way to have this python file on desktop och with a hotkey swiftly run the code? I tried doing it through terminal but it doesn't like my module.
Some info:
This is for both mac and windows.
The module I imported is pyautogui from PyPi and it works in the shell.
Thank you in advance!
Some things to consider when trying to setup a hotkey to successfully execute any kind of command:
Each Operating System has its own ways to setup hotkeys and sometimes this may differ between distributions as well as between desktop managers.
Many of the relevant how-to-descriptions are easily found via regular search machines as Google.
If you would in fact like your script to set-up its own hotkeys you would have to re-write it in such a manner that it can detect the current operating system/distribution/desktop manager by itself and execute the commands relevant to that particular set-up.
I have written a python game using pygame module. It runs fine from terminal.
I have a game directory (let's call it myGame) which contain all the resources(images and sounds) and all the python scripts which use those.
When I run the game through terminal (let's say python3 mainPythonModule.py), the game runs fine.
I tried to create a game.desktop file. But when I click it, a black window appears and crashes almost instantly.
I am using Ubuntu 16.04 LTS. And my game is written in python3.5 and uses pygame module.
My game.desktop file looks like this
I don't want the solution answered for this question because I still have to type in terminal.
I would like to create a desktop applcation, such that by clicking on the icon, the game would start.
Just like when we download any game, after installation, we just click on the game icon and it runs, I want something like that. How can I do that?
Edit :
I don't think this question is a possible duplicate of this question.
I want to know how to develop a desktop application in Ubuntu 16.04, such that by clicking on some icon, the game would start.
Final Edit :
First of all, I would like to thank everyone who responded. I really am sorry for wasting your precious time.
The issue is resolved. The problem was all the resource (images and sounds) were loaded via relative path and not through absolute path. Now the game.desktop file runs fine.
I think what are you trying to do is some kind of launcher.
Here you have my repo, it contains a Tkinter GUI with a PyGame call on a button.
Feel free to study/use it as you want, look at the file desktop_application.py and if you don`t care about the coin part you can just skip that and take what you need.
https://github.com/FilippoLeone/PyGameLauncher
And you can compile everything as an executable with PyInstaller.
Install PyInstaller from PyPI:
pip install pyinstaller
Go to your program’s directory and run:
pyinstaller yourprogram.py
I'm using pygame on a PC (PC-104) with ubuntu server, for this reason it only has terminal, not UI.
On my laptop the code works perfecly, running the code on the terminal "eg: python game.py", but on the PC104 it gives "Video system not initialized", I read the error is shown because it is a way to use events on a system without UI?
Thanks.
Thank for your answer jsbueno. I was able to found a solution a few weeks ago but forget to answer the question.
I wasn't able to run any pygame script with any other library than X11. But I found that one can run the script with no graphics library. Just setting the SDL_VIDEOLIBRARY enviroment variable to "dummy".
This is posible on bash but I prefer to do it on python:
os.environ["SDL_VIDEODRIVER"] = "dummy"
With this I was able to run the pygame script, detect joystick events, etc.
It is possible to run pygame programs in a system without X11 if you set it to use framebuffer or vgalib - the docs even talk about using aalib (which would display graphics using ascii art on the terminal.)
This part of the documentation has it:
Pygame will select from one of several internal display backends when
it is initialized. The display mode will be chosen depending on the
platform and permissions of current user. Before the display module is
initialized the environment variable SDL_VIDEODRIVER can be set to
control which backend is used. The systems with multiple choices are
listed here.
Windows : windib, directx
Unix : x11, dga, fbcon, directfb,
ggi, vgl, svgalib, aalib
So, what you have to do is set the SDL_VIDEODRIVER environment variable before starting your code. And being shure the proper lib. is installed.
For more information:
http://www.pygame.org/docs/ref/display.html