当前位置: 首页 > news >正文

【Linux】Ubuntu 创建应用图标的方式汇总,deb/appimage/通用方法

Ubuntu 创建应用图标的方式汇总,deb/appimage/通用方法

对于标准的 Ubuntu(使用 GNOME 桌面),desktop 后缀的桌面图标文件主要保存在以下三个路径:

  1. 当前用户的桌面目录(这是最常见的位置)。所有放在这个目录中的.desktop文件、文件夹或快捷方式会显示在桌面上(如果桌面图标功能启用)。
~/Desktop # 英文系统
~/桌面 # 中文系统
  1. 用户本地的应用程序快捷方式目录。更改文件不需要 sudo 权限。
~/.local/share/applications
  1. 系统范围的桌面图标目录。更改文件需要 sudo 权限。
/usr/share/applications

总结一下,如果想为普通用户创建应用图标,可以优先将 .desktop 文件放在前两个路径下,不需要 sudo 权限。另外路径1是将图标放在系统的桌面,也就是开机后看到的界面上,路径2则是放在菜单中,点击 All 展开后可以看见图标。

.desktop文件的基本格式如下:

[Desktop Entry]
Version=1.0
Type=Application
Name=My App
Comment=This is my application
Exec=/usr/bin/my-app
Icon=my-app-icon
Terminal=false
Categories=Utility;
字段含义
Type类型,如Application(应用程序)、Link(超链接)、Directory(目录)等
Name应用程序显示名称
GenericName泛称,如“文本编辑器”
Comment简短描述,鼠标悬停提示用
Exec启动命令,可以包含参数(如%f,%u,见下方)
Icon图标文件名(可为绝对路径或主题中的图标名)
Terminal是否在终端中运行 (truefalse)
Categories所属菜单类别,如Utility,Development,Game
MimeType支持的 MIME 类型(用于与文件类型关联)
StartupNotify是否启用启动通知(通常为true
Path启动程序前切换到的工作目录(可选)
Hidden若为true,则不会出现在菜单中

Exec 指定应用的路径,通常是启动可执行文件/ sh 脚本路径/appimage路径。当然也可以是某个终端命令,可以给自己常用的 shell 命令创建一个图标。

接下来讨论给 deb/appimage 快速创建图标的方法,顺便提供一个可以给一般的可执行文件/sh脚本/shell命令创建图标的通用方法。

一、deb 应用

一般命令行安装 deb 都会自动创建应用图标。

命令是否自动处理依赖是否联网下载依赖
sudo dpkg -i xxx.deb
sudo apt install ./xxx.deb

如果没有,则需要找到启动文件,参考第三种方法创建图标。

二、appimage 应用

一种方法是把 appimage 看成是可执行文件,手动编写 desktop 文件。更加高效的方法是安装 appimaged,自动维护特定目录下所有 appimage 的桌面图标。

appiamged 项目说明 https://github.com/probonopd/go-appimage/tree/master/src/appimaged#appimaged

安装

# Remove pre-existing conflicting tools (if any)
systemctl --user stop appimaged.service || true
sudo apt-get -y purge appimagelauncher || true
[ -f ~/.config/systemd/user/default.target.wants/appimagelauncherd.service ] && rm ~/.config/systemd/user/default.target.wants/appimagelauncherd.service# Clear cache
rm "$HOME"/.local/share/applications/appimage*# Optionally, install Firejail (if you want sandboxing functionality)# Download
mkdir -p ~/Applications
wget -c https://github.com/$(wget -q https://github.com/probonopd/go-appimage/releases/expanded_assets/continuous -O - | grep "appimaged-.*-x86_64.AppImage" | head -n 1 | cut -d '"' -f 2) -P ~/Applications/
chmod +x ~/Applications/appimaged-*.AppImage# Launch
~/Applications/appimaged-*.AppImage

卸载方法

systemctl --user disable --now appimaged.service || true
rm ~/.config/systemd/user/appimaged.service
rm ~/.local/share/applications/appimagekit*.desktop
rm ~/Applications/appimaged-*-x86_64.AppImage

注意,安装后不要删除 ~/Applications/appimaged*.AppImage,之后会自动检测以下路径中的 appimage 应用,并自动创建图标。

安装后,以下路径中的 appimage 应用都能被识别,自动创建图表

  • /usr/local/bin
  • /opt
  • ~/Applications(建议把所有 appimage 应用,如微信、QQ都放在这个路径下进行统一管理)
  • ~/.local/bin
  • ~/Downloads
  • $PATH, which frequently includes /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin, and other locations

例如,将文件保存在 ~/Applications

请添加图片描述

图标会自动出现,可以右键图标进一步固定到 Dock。如果文件被删除,图标也会自动消失,完全不需要手动管理。

请添加图片描述

三、通用方法

适用于一般的脚本、命令。

  1. 使用这个 desktop_creator.py 脚本
#!/usr/bin/env python3import sys
import os
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QLineEdit, QPushButton,QFileDialog, QVBoxLayout, QHBoxLayout, QMessageBox, QCheckBox
)
from pathlib import Pathclass ShortcutCreator(QWidget):def __init__(self):super().__init__()self.setWindowTitle("快捷方式生成器")layout = QVBoxLayout()# 应用名输入self.name_input = QLineEdit()self.name_input.setPlaceholderText("请输入应用名称")layout.addWidget(QLabel("应用名称:"))layout.addWidget(self.name_input)# 脚本路径输入script_layout = QHBoxLayout()self.script_input = QLineEdit()self.script_input.setPlaceholderText("选择启动脚本路径")script_button = QPushButton("选择脚本")script_button.clicked.connect(self.select_script)script_layout.addWidget(self.script_input)script_layout.addWidget(script_button)layout.addLayout(script_layout)# 图标路径输入icon_layout = QHBoxLayout()self.icon_input = QLineEdit()self.icon_input.setPlaceholderText("选择图标路径")icon_button = QPushButton("选择图标")icon_button.clicked.connect(self.select_icon)icon_layout.addWidget(self.icon_input)icon_layout.addWidget(icon_button)layout.addLayout(icon_layout)# 终端复选框self.terminal_checkbox = QCheckBox("启动时打开终端")self.terminal_checkbox.setChecked(True)layout.addWidget(self.terminal_checkbox)# 快捷方式按钮button_layout = QHBoxLayout()desktop_btn = QPushButton("创建桌面快捷方式")global_btn = QPushButton("创建应用快捷方式")desktop_btn.clicked.connect(self.create_desktop_shortcut)global_btn.clicked.connect(self.create_user_launcher_shortcut)button_layout.addWidget(desktop_btn)button_layout.addWidget(global_btn)layout.addLayout(button_layout)self.setLayout(layout)def select_script(self):path, _ = QFileDialog.getOpenFileName(self, "选择启动脚本", "", "脚本文件 (*.py *.sh *.AppImage);;所有文件 (*)")if path:self.script_input.setText(path)def select_icon(self):path, _ = QFileDialog.getOpenFileName(self, "选择图标", "", "图标文件 (*.png *.ico *.svg *.xpm);;所有文件 (*)")if path:self.icon_input.setText(path)def get_desktop_path(self):# 支持 Desktop 和 桌面possible_names = ["Desktop", "桌面"]for name in possible_names:path = Path.home() / nameif path.exists():return path# fallback: 尝试 xdg-user-dirxdg_path = os.popen("xdg-user-dir DESKTOP").read().strip()return Path(xdg_path) if xdg_path else Path.home()def create_desktop_file(self, target_path):name = self.name_input.text().strip()script_path = self.script_input.text().strip()icon_path = self.icon_input.text().strip()open_terminal = self.terminal_checkbox.isChecked()if not (name and script_path):QMessageBox.warning(self, "错误", "请填写应用名称并选择启动脚本路径")returndesktop_entry = f"""[Desktop Entry]
Name={name}
Exec={script_path}
Icon={icon_path if icon_path else 'utilities-terminal'}
Terminal={'true' if open_terminal else 'false'}
Type=Application
Categories=Utility;
"""try:with open(target_path, 'w') as f:f.write(desktop_entry)os.chmod(target_path, 0o755)QMessageBox.information(self, "成功", f"已创建快捷方式:{target_path}")print(f"✅ 快捷方式已生成: {target_path}")print(f"🗑️ 若需删除:运行命令:rm '{target_path}'")except Exception as e:QMessageBox.critical(self, "错误", f"创建失败:{e}")def create_desktop_shortcut(self):desktop_path = self.get_desktop_path()target = desktop_path / f"{self.name_input.text().strip()}.desktop"self.create_desktop_file(str(target))def create_user_launcher_shortcut(self):app_path = Path.home() / ".local/share/applications"app_path.mkdir(parents=True, exist_ok=True)target = app_path / f"{self.name_input.text().strip()}.desktop"self.create_desktop_file(str(target))if __name__ == "__main__":app = QApplication(sys.argv)window = ShortcutCreator()window.show()sys.exit(app.exec_())
  1. 安装依赖
pip3 install PyQt5
  1. 运行脚本
cd desktop-creator && 
./desktop_creator.py
  1. 输入应用名,启动脚本路径,应用图标路径还有选择是否打开终端。两个路径可以直接输入,也可以点击右侧按钮进行选择。

请添加图片描述

输入完成后,点击创建桌面快捷方式,生成文件到 ~/Desktop。

请添加图片描述

右键选择 Allow Launching 之后可以双击运行应用

可以在终端中查看生成 desktop 文件的路径和删除方式。

同样可以点击创建应用快捷方式,生成文件到 ~/.local/share/applications。

相关文章:

  • Java高级 | 【实验六】Springboot文件上传和下载
  • 《递推》题集
  • 【C++进阶篇】C++11新特性(下篇)
  • OpenLayers 从后端服务加载 GeoJSON 数据
  • 基于Spring Boot的云音乐平台设计与实现
  • day26-计算机网络-4
  • 新时代AI发展,更好的做自己
  • 8.库制作与原理
  • DDPM优化目标公式推导
  • 【Java开发日记】说一说 SpringBoot 中 CommandLineRunner
  • 【强连通分量 缩点 最长路 拓扑排序】P2656 采蘑菇|普及+
  • 游戏常用运行库合集 | GRLPackage 游戏运行库!
  • 机器学习期末复习
  • Dynamics 365 Finance + Power Automate 自动化凭证审核
  • day029-Shell自动化编程-计算与while循环
  • SOC-ESP32S3部分:33-声学前端模型ESP-SR
  • ViiTor实时翻译 2.4.2 | 完全免费的同声传译软件 实测识别率非常高 可以识别视频生成字幕
  • [GitHub] 优秀开源项目
  • vue3 创建图标 按钮
  • 26N60-ASEMI工业电机控制专用26N60
  • 请人做网站需要注意什么条件/seo网络搜索引擎优化
  • 广告推广网站怎么做/网络推广外包公司排名
  • 做展馆好的设计网站/西安百度网站快速优化
  • 做网站代理/抖音seo排名软件哪个好
  • 怎么查网站是哪家公司做的/百度app客服人工电话
  • 东营市城乡建设局网站/营销方案范文100例