安装后应用程序无法启动。运行“快速运行”可以完美运行

安装后应用程序无法启动。运行“快速运行”可以完美运行

我已经为 Ubuntu App Showdown 制作了我的应用程序。当我使用终端启动它时,quickly run它运行良好。我已经用 打包它quickly package并在本地安装。不幸的是它没有启动。我认为这是我的 python 代码中链接到 css 文件的问题,但我不知道应该如何正确链接,所以它在安装后会工作。启动已安装的应用程序时从终端输出:

michal@michal-Inspiron-530-11:~$ easy-stopwatch
/usr/lib/python2.7/dist-packages/gi/overrides/Gtk.py:391: Warning: g_object_set_property: construct property "type" for object `Window' can't be set after construction
  Gtk.Window.__init__(self, type=type, **kwds)

(easy-stopwatch:10446): Gtk-CRITICAL **: gtk_css_section_get_file: assertion `section != NULL' failed

(easy-stopwatch:10446): Gtk-CRITICAL **: gtk_css_section_get_end_position: assertion `section != NULL' failed

(easy-stopwatch:10446): Gtk-CRITICAL **: gtk_css_section_get_end_line: assertion `section != NULL' failed
Traceback (most recent call last):
  File "/usr/bin/easy-stopwatch", line 33, in <module>
    easy_stopwatch.main()
  File "/usr/lib/python2.7/dist-packages/easy_stopwatch/__init__.py", line 33, in main
    window = EasyStopwatchWindow.EasyStopwatchWindow()
  File "/usr/lib/python2.7/dist-packages/easy_stopwatch_lib/Window.py", line 35, in __new__
    new_object.finish_initializing(builder)
  File "/usr/lib/python2.7/dist-packages/easy_stopwatch/EasyStopwatchWindow.py", line 56, in finish_initializing
    css_provider.load_from_path('data/ui/gtk-style.css')
  File "/usr/lib/python2.7/dist-packages/gi/types.py", line 43, in function
    return info.invoke(*args, **kwargs)
gi._glib.GError: <unknown>:1:0: Failed to import: Error opening file: No such file or directory
michal@michal-Inspiron-530-11:~$ 

我的部分python代码:

    # Styling via CSS file
    screen = Gdk.Screen.get_default()
    css_provider = Gtk.CssProvider()
    css_provider.load_from_path('data/ui/gtk-style.css')
    style_context = Gtk.StyleContext()
    style_context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)

- - - - - - -编辑 - - - - - - - -

经过一番搜索,我意识到 并quickly package没有让应用程序安装在 中/opt,但quickly package --extras确实如此。安装后,/opt我编辑了我的 python 文件,因此 css 文件的链接如下所示:

css_provider.load_from_path('/opt/extras.ubuntu.com/easy-stopwatch/share/easy-stopwatch/ui/gtk-style.css')

之后我的应用程序就可以运行了。我不确定我是否可以在我想提交给 Ubuntu App Showdown 的代码中做到这一点。

答案1

不要在以下调用中对路径进行硬编码,而是使用通用函数来加载媒体文件。这将允许您在开发期间或在已安装的系统上正确加载文件。

所以不要这样:

css_provider.load_from_path('/opt/extras.ubuntu.com/easy-stopwatch/share/easy-stopwatch/ui/gtk-style.css')

按照以下思路做些事情:

from foo_lib.helpers import get_media_file

css_provider.load_from_path(get_media_file('gtk-style.css'))

相关内容