添加环境变量,让后台程序的notify_send弹窗能显示在当前桌面环境
时间: 2024-05-07 07:20:43 浏览: 12
如果你正在使用 Linux 操作系统,可以尝试在终端中执行以下命令:
```
export DISPLAY=:0
```
这将在当前 shell 环境中设置 `DISPLAY` 环境变量,使得后台程序的弹窗能够显示在当前桌面环境中。
如果你希望这个设置在每次打开终端时都生效,可以将这个命令添加到你的 shell 配置文件中,比如 `~/.bashrc` 或者 `~/.zshrc`。
注意:这个方法只在当前用户下有效,如果你希望其他用户也能够使用这个设置,需要在相应的用户环境中设置 `DISPLAY` 环境变量。
相关问题
如何在后台程序中使用notify_send
notify-send 是一个命令行工具,用于在桌面环境中显示通知。如果你想在后台程序中使用 notify-send,可以使用系统调用或者调用命令行工具。
使用系统调用的方法如下:
```python
import os
def send_notification(title, message):
os.system(f"notify-send '{title}' '{message}'")
```
这个函数接受两个参数,标题和消息。它使用 os.system 函数调用 notify-send 命令行工具来显示通知。
如果你使用的是 GNOME 桌面环境,你也可以使用 Python 的 dbus 库来显示通知。下面是一个使用 dbus 的示例:
```python
import dbus
def send_notification(title, message):
bus = dbus.SessionBus()
notifications = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
notifications.Notify('my_app_name', 0, '', title, message, [], {}, 5000)
```
这个函数也接受两个参数,标题和消息。它使用 dbus.SessionBus 函数获取系统的 D-Bus 总线,然后使用 get_object 函数获取通知服务的对象。最后,它调用 Notify 方法来显示通知。
如何在后台C程序中使用notify_send
`notify-send`是一个Linux桌面通知工具,它通过DBus通信协议向桌面发送通知消息。在C程序中使用`notify-send`可以通过调用DBus API来实现。
首先,需要安装`libdbus-1-dev`开发库。
然后,可以使用以下代码示例来发送通知消息:
```c
#include <dbus/dbus.h>
int main(int argc, char** argv) {
DBusError error;
DBusConnection* connection;
dbus_error_init(&error);
connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
if (dbus_error_is_set(&error)) {
fprintf(stderr, "DBus Connection Error: %s\n", error.message);
dbus_error_free(&error);
return -1;
}
dbus_error_free(&error);
dbus_uint32_t serial = 0;
dbus_uint32_t timeout = -1;
char* app_name = "My App";
char* summary = "Notification";
char* body = "This is a notification message";
char* icon = "dialog-information";
DBusMessage* msg;
msg = dbus_message_new_method_call("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
"Notify");
dbus_message_append_args(msg,
DBUS_TYPE_STRING, &app_name,
DBUS_TYPE_UINT32, &serial,
DBUS_TYPE_STRING, &icon,
DBUS_TYPE_STRING, &summary,
DBUS_TYPE_STRING, &body,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &app_name, 1,
DBUS_TYPE_UINT32, &timeout,
NULL);
dbus_connection_send(connection, msg, &serial);
dbus_connection_flush(connection);
dbus_message_unref(msg);
dbus_connection_unref(connection);
return 0;
}
```
此代码使用DBus API向桌面发送通知消息。要使用不同的应用程序名称、摘要、正文和图标,请更改代码中的相应字符串。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)