Вот код проходит через реестр, чтобы найти подразделы «DisplayName» и «DisplayIcon»:
Код: Выделить всё
def list_installed_apps_windows():
uninstall_keys = [
r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
]
installed_apps = {}
for key in uninstall_keys:
reg_key = None # Initialize reg_key to None
try:
reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key)
for i in range(winreg.QueryInfoKey(reg_key)[0]):
subkey_name = winreg.EnumKey(reg_key, i)
subkey = winreg.OpenKey(reg_key, subkey_name)
try:
app_name = winreg.QueryValueEx(subkey, "DisplayName")[0]
# Attempt to get the DisplayIcon, if it exists
icon_path = None
try:
icon_path = winreg.QueryValueEx(subkey, "DisplayIcon")[0]
except FileNotFoundError:
icon_path = None
installed_apps[app_name] = icon_path
except OSError:
# Handle the case where DisplayName does not exist
continue
finally:
winreg.CloseKey(subkey) # Ensure we close the subkey
except OSError as e:
# Log or print the error (optional)
print(f"Error accessing registry key {key}: {e}")
continue
finally:
if reg_key: # Only close reg_key if it was successfully opened
winreg.CloseKey(reg_key) # Ensure we close the main key
# Sort the installed_apps dictionary alphabetically by app_name
sorted_installed_apps = dict(sorted(installed_apps.items()))
return sorted_installed_apps
def list_installed_apps():
os_name = platform.system()
if os_name == "Windows":
return list_installed_apps_windows()
else:
return {}
Код: Выделить всё
installed_apps = list_installed_apps()
for app_name, icon_path in installed_apps.items():
if icon_path and os.path.exists(icon_path):
icon = QIcon(icon_path)
else:
icon = QIcon() # Default blank icon
self.installed_apps_combobox1.addItem(icon, app_name)
self.installed_apps_combobox2.addItem(icon, app_name)
self.installed_apps_combobox3.addItem(icon, app_name)
self.installed_apps_combobox4.addItem(icon, app_name)
[img]https://i.sstatic.net /bmDZPOPU.png[/img]
Проблема в том, что большинство приложений не имеют «DisplayIcon», я бы сказал, только около 5-10% из тех, что установлены на моем компьютере. В базовых приложениях, таких как Google Chrome или Steam, отсутствуют значки.
Можно ли что-то улучшить с помощью предоставленного кода или можно использовать какой-то альтернативный подход?
Подробнее здесь: https://stackoverflow.com/questions/790 ... ur-compute