2010-01-10 26 views

Trả lời

6

Tested trong Ubuntu 9.10:

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

Edit: Như đã đề cập trong ý kiến ​​dưới đây, phương pháp này sẽ không làm việc cho hơn một số hệ điều hành. Hai câu trả lời khác cung cấp cách giải quyết.

+0

Là chỉ ở giữa viết một câu trả lời mà tham gia liệt kê các quá trình nhưng điều này là tốt hơn nhiều. – mdm

+0

Trên Mac OS X 10.6.2 'os.environ.get ('DESKTOP_SESSION')' trả về '" Không có "' –

+2

tôi nghĩ vì mac chỉ có một! – aliva

4

Bạn có thể thử này:

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 

Và đọc các cuộc thảo luận ở đây: http://ubuntuforums.org/showthread.php?t=1139057

+0

Làm việc cho máy tính để bàn của tôi để phát hiện KDE (không giống như DESKTOP_SESSION, trả về Không) – JAL

+0

Cập nhật về điều này. Sử dụng 'xprop -root | grep -io 'xfce'' và sau đó ít nhất thay thế giữa' xfce 'và' lxde '. Điều này hoạt động trên Raspbian và Ubuntu Studio. Thay đổi đầu ra thành trường hợp thấp hơn để hiểu rõ hơn hoặc lựa chọn theo một tuyên bố. – DarkXDroid

4

Đôi khi mọi người chạy một kết hợp của môi trường máy tính để bàn. Làm cho ứng dụng của bạn bất khả tri trên máy tính để bàn bằng cách sử dụng xdg-utils; có nghĩa là sử dụng xdg-open để mở tệp hoặc url, sử dụng xdg-user-dir DOCUMENTS để tìm thư mục tài liệu, xdg-email để gửi e-mail, v.v.

10

tôi sử dụng điều này trong một dự án của tôi:

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

Cảm ơn! Đây là hoàn hảo! – nachopro

+2

Bạn nên tạo một mô-đun Python của nó và đặt nó trên PyPI. – Jabba

+2

Tôi nên thêm điều đó vào Ubuntu Studio là 'os.environ.get (" DESKTOP_SESSION ")' ném 'ubuntustudio'. Để có được môi trường Desktop chính xác, tôi sử dụng 'os.environ ['XDG_CURRENT_DESKTOP']. Lower()' để lấy 'xfce'. Đây là giải pháp để mở rộng mã tuyệt vời này. Tải lên nó – DarkXDroid

Các vấn đề liên quan