Skip to main content
 首页 » 编程设计

python之Gtk 通知不显示持有 '' 的正文

2024年10月24日14leader

我正在尝试在 python 中使用 pygobject(版本 3.16)的通知模块显示通知。我的代码运行良好,除非有 <在正文消息中。在这种情况下,不会显示正文。

例如使用这段代码,一切正常:

from gi.repository import Gtk, Notify 
 
def callback(notification, action_name): 
    notification.close() 
    Gtk.main_quit() 
 
Notify.init('test') 
notification = Notify.Notification.new('Title', 'body') 
notification.set_timeout(Notify.EXPIRES_NEVER) 
notification.add_action('quit', 'Quit', callback) 
notification.show() 
 
Gtk.main() 

但是这个有一个问题:

from gi.repository import Gtk, Notify 
 
def callback(notification, action_name): 
    notification.close() 
    Gtk.main_quit() 
 
Notify.init('test') 
notification = Notify.Notification.new('Title', '<body') 
notification.set_timeout(Notify.EXPIRES_NEVER) 
notification.add_action('quit', 'Quit', callback) 
notification.show() 
 
Gtk.main() 

我明白了:

当 <在标题中,或者当我使用 > 时没有问题。

我试图逃避 < ,但它什么也没做。 那么,我怎样才能显示包含 < 的正文呢? ?

请您参考如下方法:

我注意到在 Gtk 中有时会有 html 代码,比如在使用 set_markup 时。我尝试使用等效的 hmtl < 并且它工作正常:

from gi.repository import Gtk, Notify 
 
def callback(notification, action_name): 
    notification.close() 
    Gtk.main_quit() 
 
Notify.init('test') 
notification = Notify.Notification.new('Title', '&lt;body&gt;') 
notification.set_timeout(Notify.EXPIRES_NEVER) 
notification.add_action('quit', 'Quit', callback) 
notification.show()