Get screen layout of multiple screens on linux/Gnome - python

I am trying to create an application for multiple screens however I so far cannot find a way to locate the secondary screens position (relative to the primary screen by x and y coordinates).
I prefer to use python or bash (via libraries/frameworks are fine). I also checked with xorg.conf and it doesn't reflect my current screen setup.
I am using Ubuntu 11.10 (default Gnome 2 I believe), using compiz as the window manager. So to repeat, my question is how to get the screen layout (coordinates relative to primary screen) of all the monitors preferably by python or bash.

Nevermind, I used Pyqt instead. Here is some code...
from PyQt4.QtGui import QApplication, QPixmap
desktop = QApplication.desktop()
screenRect = desktop.screenGeometry(1) #2nd monitor
print screenRect.x(), screenRect.y() #returns the x and y of that screen

Python binding solution
So, from here you can download the xrandr bindings for python: https://launchpad.net/python-xrandr
# Import the module
from xrandr import xrandr
# Get a screen object to work with
screen = xrandr.get_current_screen()
# Get the active output objects as a list
active_outputs = [o for o in screen.get_outputs() if o.is_active()]
This was as far as I got playing around a little. I hope it will get you started :-) I only have one screen connected right now...
Parsing data solution
The other solution, as I mentioned in my comment above is to parse the output of the command xrandr it looks like it should be pretty simple from just taking a glance at it...

Related

How to take screenshot of background app in python 2.7 for mac

I have been trying to make a program that notifies me when a number changes on an app. I have been using ImageGrab and then pytesseract which works but I can only figure out how to take the screenshot when I can visually see the number. It would be very nice if there was a way to take an image of the app if it was minimized (not visible on the screen) so I could work on other things as it ran. I also need the picture of a certain part of that app I need to do a bounding box within the app of where the picture is taken.
This is what I am currently using which takes a certain part of the whole screen:
img = ImageGrab.grab(bbox=(1400,875,1445,905))
I think there might be a way to do it with Quartz but I could not find out how to do a region of a background app.
It is possible to take screenshots of windows that are running in the background with the screencapture api in MacOS. You can see the documentation by typing man screencapture in the terminal.
For your use case it would look something like this:
screencapture -l <windowId> -R <x,y,w,h>
As you mentioned, you can use Quartz to find the desired windowId:
from Quartz import CGWindowListCopyWindowInfo, kCGNullWindowID, kCGWindowListOptionAll
windowName = 'Desktop' # change this to the window you are looking for
def findWindowId():
windowList = CGWindowListCopyWindowInfo(
kCGWindowListOptionAll, kCGNullWindowID)
for window in windowList:
if(windowName.lower() in window.get('kCGWindowName', '').lower()):
return window['kCGWindowNumber']
return None
You can find a fully working example here
Note: From my testings, if you minimise(cmd + m) or hide(cmd + h) a window, taking a screenshot of it will only capture the moment before it was hidden. You would need to keep the window opened for it to work--but it is ok to keep it behind other windows. Tested on MacOS v10.15.7.

Python, How to make the pygame window pop up above everything else [duplicate]

Is there any way to control where pygame creates the game screen? It seems to always create it in the same general area but not in a consistent location.
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = str(position[0]) + "," + str(position[1])
as per http://pygame.org/wiki/FrequentlyAskedQuestions
You can also just center the screen with
import pygame, os
os.environ['SDL_VIDEO_CENTERED'] = '1'
Note that these should be done before you initialize pygame in the main loop. I do it right after I import os for example. And since they are not actually part of pygame, you can probably use it elsewhere, though things like gtk and wxpython provide their own mechanisms.
Positioning of windows is not handled by the client application. It's handled by the Window manager (metacity etc.).
The SDL library on which PyGame is based does have a few environment variables which can be used to give hints to the Window manager. These are hints which the WM may ignore but it's the best you can do.
The comments over here have an example.

How do I find which monitor the mouse pointer is in with python?

I have two monitors attached to my Desktop session in linux. I'd like to have a gtk window pop-up on the screen where the mouse cursor is. Sometimes it would be monitor 1, sometimes monitor 2. The pop-up is easy enough, but how can I determine which monitor contains the mouse pointer? I've run across various examples shelling out of python to call xrandr but I was hoping for a more integrative approach in Python.
Its pretty dependent on your setup which is why an "integrative" approach is fairly difficult. Multiple monitors on X is quite a mess.
Assuming you are using twinview, the X screen is essentially just 1 screen anyway, so how does the monitor matter?
from Xlib import display
data = display.Display().screen().root.query_pointer()._data
locationtuple = (data["root_x"], data["root_y"])
That should work if you are using Twinview.

Python & WNCK: Get data from a window

I have a program executing through wine in Ubuntu 13, this program has its own GUI and there is some data on this program like in this picture:
My intention is (using Python) get this window, and try to obtain this data from it.
Right now I'm trying using wnsck:
from gi.repository import Gtk, Wnck
Gtk.init([])
screen = Wnck.Screen.get_default()
screen.force_update()
all_windows = screen.get_windows() # First I get all windows
# Then I search my window
my_window = all_windows[-1] # Let's supose it's the last window
# I'm trying to do things like
my_window.get_data()
my_window.steal_data()
# ... and many others methods I saw could be what I'm looking for
When I try to get some data as I said above I allways get:
RuntimeError: Data access methods are unsupported. Use normal Python attributes instead
Does anybody if it's possible to get process data with wnck ?
Does anybody knows another way to achieve what I'm trying ?
I'm trying to get some values displayed on the window
I know there are some libraries for windows, but I'm interested doing this in Ubuntu
Thanks in advance

does someone know how to show content on screen (covering up any window) using Ruby or Python?

using Ruby or Python, does someone know how to draw on the screen, covering up any other window? Kind of like, press a key, and the program will show current weather or stock quote on the screen (using the whole screen as the canvas), and then press the key again, and everything restores to the same as before? (like Mac OS X's dash board).
You could use the systems dashboard (desktop widgets, or whatever it's called) API. In order to do that you need bindings to it for Python or Ruby.
Alternatively you could use some generic gui toolkit or application framework and just create a frameless window with transparent background. Then you need to be sure that the chosen toolkit supports 'always-on-top' options on your desired platform(s).
If you are on windows you can directly draw to desktop dc(device context) using win32api
e.g. just for fun try this :)
>>> import win32ui
>>> import win32gui
>>> hdc = win32ui.CreateDCFromHandle( win32gui.GetDC( 0 ) )
>>> hdc.DrawText("Wow it works", (100, 100, 200, 200))
>>> hdc.LineTo(500,500)
but that won't be very useful ,as not erasable
best bet would be to use a transparent window or window with a cutout region (atleast on windows that is possible)
or even if you can't draw transparent on some system you can grab the current screen and display it as background of you window that would give a transparent effect
I would recommend PyGame.

Categories

Resources