在Python中嵌入evince GI

2 投票
1 回答
2242 浏览
提问于 2025-04-17 10:42

我想在Python中通过gi库嵌入一个pdf文档来查看它。我正在尝试按照这里的代码来做

#!/usr/bin/env python

from gi.repository import Gtk, Gio
from gi.repository import EvinceDocument
from gi.repository import EvinceView

class HelloWorldApp(Gtk.Application):
   def __init__(self):
       Gtk.Application.__init__(self, application_id="apps.test.helloevince", flags=Gio.ApplicationFlags.FLAGS_NONE)
       self.connect("activate", self.on_activate)

   def on_activate(self, data=None):
       window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
       window.set_title("Evince Gtk3 Python Example")
       window.set_border_width(24)
       scroll = Gtk.ScrolledWindow()
       window.add(scroll)
       EvinceDocument.init()
       doc = EvinceDocument.Document.factory_get_document('file:///home/user/test.pdf')
       view = EvinceView.View()
       model = EvinceView.DocumentModel()
       model.set_document(doc)
       view.set_model(model)
       scroll.add(view)
       window.show_all()
       self.add_window(window)

if __name__ == "__main__":
   app = HelloWorldApp()
   app.run(None)

但是我遇到了这个错误

Traceback (most recent call last):
  File "./pdfViewer_pygi.py", line 19, in on_activate
    doc = EvinceDocument.Document.factory_get_document('file:///home/user/test.pdf')
AttributeError: type object 'Document' has no attribute 'factory_get_document'

很明显,'factory_get_document'这个方法不存在。那么有什么替代的方法呢...? 我该如何使用Python和gtk+3来嵌入一个pdf文档呢...?

1 个回答

2

你需要一个更新版本的Evince。

在我使用的最新版本Evince 3.3.3.1上,这个功能可以正常运行,我是在Fedora 17(开发版)上测试的,而在Fedora 16上使用Evince 3.2.1时则无法正常工作。

也许你可以尝试自己编译Evince,这样就能让绑定功能正常工作。

撰写回答