2010-08-30 194 views

回答

4

我假設你是一個GUI工具包。爲什麼你會對屏幕尺寸感興趣?

檢出來自PyGTK的gtk.gdk.screen_width()gtk.gdk.screen_height()。 QT應該有類似的東西可用。

+0

隨着wxPython中,這將是GetDisplaySize方法:http://xoomer.virgilio.it/infinity77/wxPython/wxFunctions.html?highlight=getdisplaysize#GetDisplaySize – Mark 2010-08-30 14:02:16

+0

謝謝,我目前正在重新配置我的一些程序在Ubuntu中運行。我將切換出所有Win特定功能。 – rectangletangle 2010-08-30 18:23:22

4

我可以提出一些可以使用的方法。我沒有使用xlib版本。

1)xlib(用於Python程序的X客戶端庫)(如果系統上可用)。你可以看一下「顯示」方法和屬性:python-xlib.sourceforge

2)在Ubuntu,你可以做到以下幾點,以獲得屏幕分辨率:

xrandr | grep \* | cut -d' ' -f4 

3)您可以使用子Python模塊,運行上述命令並提取信息

import subprocess 
output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4',shell=True, stdout=subprocess.PIPE).communicate()[0] 
print output 

讓我知道,如果這對你有幫助。

0
import subprocess 


def get_screen_resolution(): 
    output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4',shell=True, stdout=subprocess.PIPE).communicate()[0] 
    resolution = output.split()[0].split(b'x') 
    return {'width': resolution[0], 'height': resolution[1]} 

print(get_screen_resolution()) 
0

我在Ubuntu 16.04 LTS上使用python 3.5,並且有同樣的問題。我想要解決它,而不必依賴額外的模塊,因爲它們並未默認安裝。

所以我想象了這個小函數,它使用xrandr來獲取數據。

def monitorsInfo(): 

import subprocess 

def getParenthesis(texte): 
    content = None 
    p1 = texte.find('(') 
    if p1 >= 0: 
     p2 = texte.find(')') 
     if p2 > p1: 
      content = texte[p1:p2+1] 
    return content 

commande = ['xrandr','--listmonitors'] 
res = subprocess.check_output(commande, shell=True).decode().split('\n') 

monitors = {} 

for l in res: 
    if len(l) > 1: 
     if l[0] != ' ': 
      if l.split()[0] == l.split()[0].upper(): 

       options = getParenthesis(l) 
       if options: 
        l = l.replace(options, '') 
       z = l.split() 

       # this is a connector 
       name = z[0] 
       conn = None 
       primary = None 
       geo = None 
       size = None 
       width = height = offsetx = offsety = None 
       if z[1].lower() == 'connected': 
        conn = True 
        # monitor in use :-) 
       else: 
        # screeen connection exists, no screen used 
        conn = False 
       # for connected screens : get extra data 
       if conn: 
        if z[2].lower() == 'primary': 
         primary = True 
         z.pop(2) 
        # other data for connected screeens 
        geo = z[2] # get rid of extra 'primary' 
        size = ''.join(z[3:]) 
        # get width and height 
        z = geo.split('+') 
        offsetx = int(z[1]) 
        offsety = int(z[2]) 
        z = z[0].split('x') 
        width = int(z[0]) 
        height = int(z[1]) 


       # create a dict per monitor 
       d = {} 
       d['name'] = name 
       d['connected'] = conn 
       d['primary'] = primary 
       d['geometry'] = geo 
       d['options'] = options 
       d['size'] = size 
       d['width'] = width 
       d['height'] = height 
       d['offsetx'] = offsetx 
       d['offsety'] = offsety 

       monitors[name] = d 

return monitors 

結果以字典形式返回。