2010-01-10 17 views

回答

6

測試在Ubuntu 9.10:

>>> import os 
>>> os.environ.get('DESKTOP_SESSION') 
'gnome' 

編輯:正如在下面的評論中提到,這種方法不適用於多一些操作系統的工作。另外兩個答案提供瞭解決方法。

+0

只是在寫,涉及枚舉進程答案的中間,但這要好得多。 – mdm 2010-01-10 01:12:37

+0

在Mac OS X 10.6.2'os.environ.get('DESKTOP_SESSION')'返回 ''無「' – 2010-01-10 01:16:44

+2

我想因爲mac只有一個! – aliva 2010-01-10 01:20:39

4

你可以試試這個:

def detect_desktop_environment(): 
    desktop_environment = 'generic' 
    if os.environ.get('KDE_FULL_SESSION') == 'true': 
     desktop_environment = 'kde' 
    elif os.environ.get('GNOME_DESKTOP_SESSION_ID'): 
     desktop_environment = 'gnome' 
    else: 
     try: 
      info = getoutput('xprop -root _DT_SAVE_MODE') 
      if ' = "xfce4"' in info: 
       desktop_environment = 'xfce' 
     except (OSError, RuntimeError): 
      pass 
    return desktop_environment 

這裏閱讀討論:http://ubuntuforums.org/showthread.php?t=1139057

+0

這適用於我的桌面檢測KDE(不像DESKTOP_SESSION,它返回無) – JAL 2010-01-10 09:28:43

+0

這是一個更新。使用'xprop -root | grep -io'xfce''然後至少在'xfce'和'lxde'之間交替。這適用於Raspbian和Ubuntu Studio。將輸出更改爲小寫字母,以便在聲明下更好地理解或選擇。 – DarkXDroid 2015-07-06 13:49:48

4

人有時候運行桌面環境的混合。使用xdg-utils使您的應用程序與桌面無關;這意味着使用xdg-open打開文件或網址,使用xdg-user-dir DOCUMENTS查找文檔文件夾,xdg-email發送電子郵件等。

10

我在項目中的一個使用此:

def get_desktop_environment(self): 
     #From http://stackoverflow.com/questions/2035657/what-is-my-current-desktop-environment 
     # and http://ubuntuforums.org/showthread.php?t=652320 
     # and http://ubuntuforums.org/showthread.php?t=652320 
     # and http://ubuntuforums.org/showthread.php?t=1139057 
     if sys.platform in ["win32", "cygwin"]: 
      return "windows" 
     elif sys.platform == "darwin": 
      return "mac" 
     else: #Most likely either a POSIX system or something not much common 
      desktop_session = os.environ.get("DESKTOP_SESSION") 
      if desktop_session is not None: #easier to match if we doesn't have to deal with caracter cases 
       desktop_session = desktop_session.lower() 
       if desktop_session in ["gnome","unity", "cinnamon", "mate", "xfce4", "lxde", "fluxbox", 
             "blackbox", "openbox", "icewm", "jwm", "afterstep","trinity", "kde"]: 
        return desktop_session 
       ## Special cases ## 
       # Canonical sets $DESKTOP_SESSION to Lubuntu rather than LXDE if using LXDE. 
       # There is no guarantee that they will not do the same with the other desktop environments. 
       elif "xfce" in desktop_session or desktop_session.startswith("xubuntu"): 
        return "xfce4" 
       elif desktop_session.startswith("ubuntu"): 
        return "unity"  
       elif desktop_session.startswith("lubuntu"): 
        return "lxde" 
       elif desktop_session.startswith("kubuntu"): 
        return "kde" 
       elif desktop_session.startswith("razor"): # e.g. razorkwin 
        return "razor-qt" 
       elif desktop_session.startswith("wmaker"): # e.g. wmaker-common 
        return "windowmaker" 
      if os.environ.get('KDE_FULL_SESSION') == 'true': 
       return "kde" 
      elif os.environ.get('GNOME_DESKTOP_SESSION_ID'): 
       if not "deprecated" in os.environ.get('GNOME_DESKTOP_SESSION_ID'): 
        return "gnome2" 
      #From http://ubuntuforums.org/showthread.php?t=652320 
      elif self.is_running("xfce-mcs-manage"): 
       return "xfce4" 
      elif self.is_running("ksmserver"): 
       return "kde" 
     return "unknown" 

    def is_running(self, process): 
     #From http://www.bloggerpolis.com/2011/05/how-to-check-if-a-process-is-running-using-python/ 
     # and http://richarddingwall.name/2009/06/18/windows-equivalents-of-ps-and-kill-commands/ 
     try: #Linux/Unix 
      s = subprocess.Popen(["ps", "axw"],stdout=subprocess.PIPE) 
     except: #Windows 
      s = subprocess.Popen(["tasklist", "/v"],stdout=subprocess.PIPE) 
     for x in s.stdout: 
      if re.search(process, x): 
       return True 
     return False 
+0

謝謝!太棒了! – nachopro 2014-06-26 16:46:09

+2

你應該製作一個Python模塊並將其放在PyPI上。 – Jabba 2014-12-08 23:48:04

+2

我應該在Ubuntu Studio上添加'os.environ.get(「DESKTOP_SESSION」)'拋出'ubuntustudio'。爲了獲得正確的桌面環境,我使用'os.environ ['XDG_CURRENT_DESKTOP']。lower()'來獲得'xfce'。這是一個解決方法來擴展這個真棒代碼。上傳它 – DarkXDroid 2015-07-06 13:19:33