AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1179739
Accepted
Newtron Malayalam
Newtron Malayalam
Asked: 2019-10-10 05:55:16 +0800 CST2019-10-10 05:55:16 +0800 CST 2019-10-10 05:55:16 +0800 CST

如何通过具有“onClick”功能的命令在 gnome 中创建通知?

  • 772

我们可以使用简单的命令创建通知。

前任:notify-send 'SUPER IMPORTANT!' 'This is an urgent message!' -u critical

我们可以让它可点击并在点击它时运行脚本吗?喜欢

当我们点击像这样由 Nautilus 文件管理器发送的通知时 在此处输入图像描述

它直接打开一个新窗口。但是我们的自定义通知没有做任何事情。当我们点击它时,如何让我们的自定义通知做一个活动。

notification gnome command-line
  • 1 1 个回答
  • 2163 Views

1 个回答

  • Voted
  1. Best Answer
    Jacob Vlijm
    2019-10-12T11:40:57+08:002019-10-12T11:40:57+08:00

    有趣的问题!

    在此处输入图像描述

    ...这是在UB 实验性回购中探索以下主题的触发因素。结果是一个通知/消息弹出窗口,它具有以下选项:

    具有(可选)点击功能的通知示例

    • 设置角落出现
    • 设置单击时运行的命令
    • 设置标题(粗体)
    • 设置消息文本
    • 设置一个图标
    • 设置生命长度(秒)<- 在片段中设置

    前四个选项仅在您设置参数时适用,角默认为右下角(在主要位置),除非设置不同。

    生命长度是硬编码的,默认为 10 秒,除非设置不同。

    笔记

    • 请注意,这些通知 - 按原样 - 不会通过dbus,因此无法“收听”它们。进一步的发展可能是使它成为一个类似守护进程的后台进程——保持 Gtk 循环活着——,只在 dbus 提示上调用窗口。
    • 许多值/首选项可以移动到 gsettings

    如何设置

    • 将代码段复制到一个空文件中,将其另存为alternotify.py,使其可执行
    • 使用以下选项的任意组合运行它,只需选择您需要的:

      • 点击命令:command="command_to_run"
      • 标题:title="Title to show"
      • 消息文本(正文):body="Text body of the notification message, text, text, text"
      • 图标(来自图标名称):icon="icon-name"
      • 要出现的角,1 = NE,2 = NW,3 = SE,4 = SW:position=1

    一个完整的命令可能如下所示:

    /path/to/alternotify.py title="Missing applet" body="To use this functionality, you need to run previews. Click this notification to switch it on." icon="budgie-hotcorners-symbolic" command="gedit /home/jacob/Bureaublad/Kap" position=4
    

    在此处输入图像描述

    编码

    #!/usr/bin/env python3
    import gi
    gi.require_version("Gtk", "3.0")
    from gi.repository import Gtk, Gdk, GLib
    import sys
    import subprocess
    
    class NotifyWindow(Gtk.Window):
    
        def __init__(self):
            Gtk.Window.__init__(self)
            self.set_decorated(False)
            distance = 80 # gsettings
            winwidth = 300 # gsettings
            winheight = 80 # gsettings
            self.set_default_size(winwidth, winheight)
            self.maingrid = Gtk.Grid()
            self.add(self.maingrid)  
            self.set_space()
            self.winpos = 4 # gsettings? default = SE
            self.get_args()
            self.currage = 0
            self.targetage = 10 # gsettings,life seconds
            GLib.timeout_add_seconds(1, self.limit_windowlife) 
            self.maingrid.show_all()
            self.position_popup(self.winpos, winwidth, winheight, distance)
            self.show_all()
            Gtk.main()
    
        def get_winpos(self, arg):
            self.winpos = int(arg)
    
        def limit_windowlife(self):
            if self.currage >= self.targetage:
                Gtk.main_quit()
            self.currage = self.currage + 1;
            return True        
    
        def position_popup(self, winpos, winwidth, winheight, distance):
            monitordata = self.get_primarymonitor()
            winsize = self.get_size()
            winwidth, winheight = winsize.width, winsize.height
            monitor_xpos = monitordata[2]
            monitor_ypos = monitordata[3]
            monitor_width = monitordata[0]
            monitor_height = monitordata[1]
    
            if winpos == 1:
                wintargetx = monitor_xpos + distance
                wintargety = monitor_ypos + distance
            elif winpos == 2:
                wintargetx = monitor_width + monitor_xpos - winwidth - distance
                wintargety = monitor_ypos + distance
            elif winpos == 3:
                wintargetx = monitor_xpos + distance
                wintargety = monitor_ypos + monitor_height - (
                    distance + winheight
                )
            elif winpos == 4:
                wintargetx = monitor_width + monitor_xpos - winwidth - distance
                wintargety = monitor_ypos + monitor_height - (
                    distance + winheight
                )
            self.move(wintargetx, wintargety)
    
        def get_primarymonitor(self):
            # see what is the resolution on the primary monitor
            prim = Gdk.Display.get_default().get_primary_monitor()
            geo = prim.get_geometry()
            [width, height, screen_xpos, screen_ypos] = [
                geo.width, geo.height, geo.x, geo.y
            ]
            height = geo.height
            return width, height, screen_xpos, screen_ypos
    
        def show_title(self, title):
            title_label = Gtk.Label(label=title)
            self.maingrid.attach(title_label, 3, 1, 1, 1)
            title_label.set_xalign(0)
            # set title bold
            self.noti_css = ".title {font-weight: bold; padding-bottom: 5px;}"
            self.provider = Gtk.CssProvider.new()
            self.provider.load_from_data(self.noti_css.encode())
            self.set_textstyle(title_label, "title")
    
        def set_body(self, body):
            body_label = Gtk.Label(
                label=body
            )
            self.maingrid.attach(body_label, 3, 2, 1, 1)
            body_label.set_xalign(0)
            body_label.set_size_request(250, -1)
            body_label.set_line_wrap(True)
    
        def set_icon(self, icon):
            self.maingrid.attach(Gtk.Label(label="\t"), 2, 0, 1, 1)
            if not "/" in icon:
                newicon = Gtk.Image.new_from_icon_name(
                    icon, Gtk.IconSize.DIALOG
                )
                self.maingrid.attach(newicon, 1, 1, 1, 2)
                self.maingrid.show_all()
    
        def get_args(self):
            args = sys.argv[1:]
            funcs = [
                self.show_title, self.set_body, self.set_icon,
                self.connect_action, self.get_winpos,
            ]
            argnames = ["title", "body", "icon", "command", "position"]
            for arg in args:
                argdata = arg.split("=")
                argname = argdata[0]
                arg = argdata[1]
                try:
                    i = argnames.index(argname)
                    funcs[i](arg)
                except ValueError:
                    print("invalid argument:", arg)             
    
        def connect_action(self, arg):
            self.connect("button_press_event", self.run_command, arg)
            pass
    
        def set_textstyle(self, widget, style):
            widget_cont = widget.get_style_context()
            widget_cont.add_class(style)
            Gtk.StyleContext.add_provider(
                widget_cont,
                self.provider,
                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION,
            )               
    
        def run_command(self, event, key, command):
            if key.get_button()[1] == 1:
                subprocess.Popen(["/bin/bash", "-c", command])
    
        def set_space(self):
            for cell in [[0, 0], [100, 0], [0, 100], [100, 100]]:
                self.maingrid.attach(
                    Gtk.Label(label="\t"), cell[0], cell[1], 1, 1
                )
    
    NotifyWindow()
    
    • 3

相关问题

  • 是否有适用于 IMAP 邮件帐户的 Gnome 小程序?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

  • 如果顶部面板中缺少会话小程序,如何注销?

  • 如何让“您的电池坏了”消息消失?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve