python gi.repository通知和换行“ \ n”

用户名

我无法在“ gi.repository通知”中显示新行。当我在程序中使用字符串常量时,它可以工作,但是当我使用ConfigParser类从配置文件中读取字符串时,它会失败。

test.ini

[NOTIFICATIONS]
test1 = Hello,\n{username}!

test.py:

import ConfigParser
from gi.repository import Notify

# notifyText = "Hello, {username}" - will work
data = {'username': 'sudo', 'test': 'test'}


if __name__ == '__main__':
    cfg = ConfigParser.ConfigParser()                              
    cfg.read('test.ini')
    notifyText = cfg.get('NOTIFICATIONS', 'test1').format(**data)

    Notify.init('Test')
    notification = Notify.Notification('Test', notifyText)
    notification.show()

当前程序的输出将是:'Hello \ nsudo!' 但是,如果我在程序中对此字符串(注释行)进行了硬编码,则将按原样显示它。

眼睛

\n当a读取配置文件时ConfigParser,它不会被特别对待,它会被解释为litreral \n

如果要换行,只需在下一行继续选项字符串:

[NOTIFICATIONS]
test1 = Hello,
        {username}!

以空格开头的每一行都被视为前一行的延续,空格将被删除,但换行符保持不变:

>>> print(cfg.get('NOTIFICATIONS', 'test1'))
Hello,
{username}!
>>> 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章