【发布时间】:2010-01-10 01:06:41
【问题描述】:
我怎样才能知道我的桌面环境使用 Python 是什么?我喜欢结果是 gnome 或 KDE 或其他。
【问题讨论】:
标签: python linux environment
我怎样才能知道我的桌面环境使用 Python 是什么?我喜欢结果是 gnome 或 KDE 或其他。
【问题讨论】:
标签: python linux environment
我在我的一个项目中使用它:
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
【讨论】:
os.environ.get("DESKTOP_SESSION") 抛出“ubuntustudio”。为了获得正确的桌面环境,我使用os.environ['XDG_CURRENT_DESKTOP'].lower() 来获得“xfce”。这是扩展这个很棒的代码的一种解决方法。上传它
在 Ubuntu 9.10 中测试:
>>> import os
>>> os.environ.get('DESKTOP_SESSION')
'gnome'
编辑 2:正如 cmets 所说,使用较新的 GNOME 版本开始变得更加不可靠。我现在也在运行 Ubuntu 18.04,它返回 'ubuntu' 而不是之前的 'gnome'。
编辑 1:如下面的 cmets 所述,这种方法不适用于更多的某些操作系统。其他两个答案提供了解决方法。
【讨论】:
os.environ.get('DESKTOP_SESSION') 返回 "None"
你可以试试这个:
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
【讨论】:
xprop -root | grep -io 'xfce',然后至少在“xfce”和“lxde”之间交替。这适用于 Raspbian 和 Ubuntu Studio。将输出更改为小写,以便更好地理解或选择语句。
有时人们会运行多种桌面环境。使用 xdg-utils 使您的应用与桌面无关;这意味着使用xdg-open 打开文件或url,使用xdg-user-dir DOCUMENTS 查找文档文件夹,使用xdg-email 发送电子邮件等等。
【讨论】: