gpt4 book ai didi

notifications - gtk 应用内通知 API 引用

转载 作者:行者123 更新时间:2023-12-04 18:01:50 25 4
gpt4 key购买 nike

我最近研究了 gtk 设计模式,发现 in-app notifications .有关于何时使用它的描述,但没有引用 gtk api。

我已经搜索过了,但只找到了 GNotificationGApplication.send_notification ,但这会将通知发送到桌面环境。

任何人都可以帮助查找用于执行应用内通知的教程或示例代码吗?

最佳答案

应用通知“小部件”是小部件、css 类和行为的组合。

您应该在计划使用应用程序通知的窗口中使用 Gtk.Overlay,然后使用带有预定义 app-notification 的容器(例如 Gtk.Box)。风格类。通知容器应该包含在 Gtk.Revealer 中以允许显示“幻灯片”过渡。

这是一个带有示例的林间空地 ui 文件 (app-notification.ui):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="default_width">640</property>
<property name="default_height">480</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkOverlay" id="overlay">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">APP-NOTIFICATION EXAMPLE</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">show app-notification</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="index">-1</property>
</packing>
</child>
<child type="overlay">
<object class="GtkRevealer" id="revealer2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">start</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="valign">start</property>
<property name="spacing">20</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">This is an app-notification. Click the button to dismiss</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">none</property>
<child>
<object class="GtkImage" id="image2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">window-close-symbolic</property>
</object>
</child>
<style>
<class name="image-button"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<style>
<class name="app-notification"/>
</style>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>

Glade 中的结果:

enter image description here

这里有一些 python 代码,它使用了之前的 glade 文件,并为通知提供了一些动态行为,以便您可以通过单击按钮来查看它的运行情况。林间空地文件应命名为 app-notification.ui,否则更改代码以反射(reflect)给定名称:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

def onButtonShow(self):
revealer.set_reveal_child(True)

def onButtonClose(self):
revealer.set_reveal_child(False)

builder = Gtk.Builder()
builder.add_from_file("app-notification.ui")

window = builder.get_object("window1")

buttonShow = builder.get_object("button1")
buttonClose = builder.get_object ("button2")
revealer = builder.get_object("revealer2")

buttonShow.connect ("clicked", onButtonShow)
buttonClose.connect ("clicked", onButtonClose)
window.connect ("destroy", Gtk.main_quit)
window.show_all()

Gtk.main()

关于notifications - gtk 应用内通知 API 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com