Python Forum
Looking for platform test help
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looking for platform test help
#1
Please, if willing, try this snippet on Linux and OS-X
It works fine on windows (7)

import screeninfo
import re
import sys


"""
GetScreenInfo()

cross platform creates a dictionary with an entry for each monitor
tied to users computer. the key is constructed as follows:
   monitor{n} n is sequentiallty assigned beginning with 1

Usage: GetScreenInfo(win_scale=.6, offset_scale=.1)
   where: scale is % of full screen, so default .6 = 60 % and
          offset is % of scaled width & height

each entry contains a nested dictionary with four values:
   swidth = scaled_width
   sheight = scaled height
   hoffset = horizontal offset
   voffset = vertical offset4

example: 
>>> from GetScreenInfo import GetScreenInfo
>>> gsi = GetScreenInfo(win_scale=.8, offset_scale=.2)
>>> print(gsi.monitor_info)
>>> {'monitor1': {'swidth': 1920, 'sheight': 1080, 'hoffset': 384, 'voffset': 216}}
   {'monitor1': {'swidth': 1920, 'sheight': 1080, 'hoffset': 384, 'voffset': 216}}

Tested on Windows 7 with python 3.6
Needs testing on Linux and Apple
"""
class GetScreenInfo():
   def __init__(self, win_scale=.6, offset_scale=.1):
       # use scale of 0 to return unmodified dimensions
       if win_scale == 0:
           newscale = 1
           newoffset = 0
       else:
           newscale = win_scale
           newoffset = offset_scale

       platform = sys.platform
       platform = platform.rstrip('1234567890')
       self.monitor_info = {}
       ostypes = {
           'linux': 'x11',
           'win': 'windows',
           'cygwin': 'cygwin',
           'darwin': 'osx'
       }

       # Following hack is for return proper linux value from sys.platform
       #  prior to python 3.3 which always starts with 'linux' but may
       #  be linux1, linux2 etc.
       if platform.startswith('linux'):
           montype = ostypes['linux']
       mon = screeninfo.get_monitors(ostypes[platform])
       for n, item in enumerate(mon):
           m = re.split(r'[()x+]', str(item))
           mkey = 'monitor{}'.format(n+1)
           self.monitor_info[mkey] = {}
           self.monitor_info[mkey]['swidth'] = int(float(m[1]) * newscale)
           self.monitor_info[mkey]['sheight'] = int(float(m[2]) * newscale)
           self.monitor_info[mkey]['hoffset'] = int(self.monitor_info[mkey]['swidth'] * newoffset)
           self.monitor_info[mkey]['voffset'] = int(self.monitor_info[mkey]['sheight'] * newoffset)


def main(scale, offset):
   gsi = GetScreenInfo(win_scale=scale, offset_scale=offset)
   print(gsi.monitor_info)

if __name__ == '__main__':
   main(scale=.8, offset=.2)
It should return a dictionary with an entry for each monitor attached on the target computer
here's a single monitor output example:
Output:
{'monitor1': {'swidth': 1536, 'sheight': 864, 'hoffset': 307, 'voffset': 172}}
the values are scaled by scale and offset with the fp number representating % of full scale
Reply
#2
Linux Mint 18.2 on my laptop and a TV connected via HDMI
Output:
{'monitor1': {'hoffset': 307, 'voffset': 172, 'sheight': 864, 'swidth': 1536}, 'monitor2': {'hoffset': 307, 'voffset': 172, 'sheight': 864, 'swidth': 1536}}
Reply
#3
same as buran. Ubuntu 16.04 with my monitor and TV hooked up via HDMI
Output:
{'monitor2': {'voffset': 172, 'sheight': 864, 'hoffset': 307, 'swidth': 1536}, 'monitor1': {'voffset': 172, 'sheight': 864, 'hoffset': 307, 'swidth': 1536}}
windows 10 with TV hooked up via HDMI to the same TV as linux
Output:
{'monitor1': {'swidth': 1536, 'sheight': 864, 'hoffset': 307, 'voffset': 172}}
windows 10 hooked up with TV via HDMI set as extended displays
Output:
{'monitor1': {'swidth': 1536, 'sheight': 864, 'hoffset': 307, 'voffset': 172}, 'monitor2': {'swidth': 1536, 'sheight': 864, 'hoffset': 307, 'voffset': 172}}
Recommended Tutorials:
Reply
#4
Thanks all, anyone have OS-X ?

Even if I don't get a test with OS-X, I will use this in my app,
but would be nice to know.
Reply
#5
Turns out I already posted this before (not sure if there have been any changes since then)
I didn't intend on doing this, it's a consequence of my brain doing what brains do after the
age of 70.

I'll merge together.
Reply
#6
Arch linux - Kernel~4.11.7-1

Output:
{'monitor1': {'swidth': 1536, 'sheight': 864, 'hoffset': 307, 'voffset': 172}}
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Is this correct? I would have expected a different output from each person/monitor. Are you all sharing the same monitor?

btw, I haven't been able to try it yet.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
Hmm ... I see your point ... Monitors do tend to be a set of standard sizes.
Can add a print statement after assignment of mon which
will show the raw monitor size.
Reply
#9
Display Server: X.Org 1.19.3 driver: intel Resolution: [email protected]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
for wavic:
1920x1080
{'monitor1': {'swidth': 1536, 'sheight': 864, 'hoffset': 307, 'voffset': 172}}
the width and height look ok,
values for offset don't correspond to 20%
but do correspond to 20 % of scaled sizes - this is not correct! the calculation fro offset should be on total size
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020