嵌入Python GI

发布于 2024-12-28 02:33:10 字数 1573 浏览 1 评论 0原文

我想通过使用 gi 存储库将 pdf 文档嵌入到 Python 中来查看它。我正在尝试遵循此处的代码,

#!/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 文档...?

I want to view a pdf document by embedding it in Python using the gi repository. I am trying to follow the code here which is

#!/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)

But I get the error

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'

It is obvious that there is no 'factory_get_document' method. What then is the alternative...? How can one embed a pdf document using python and gtk+3...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆躺在深渊里 2025-01-04 02:33:10

您需要更新版本的 Evince。

这在使用 Fedora 17 (rawhide) 的最新 Evince 3.3.3.1 中适用,但在使用 Fedora 16 的 Evince 3.2.1 中不起作用。

也许您可以编译 Evince 以使绑定正常工作。

You need a newer version of Evince.

This is working for me in latest Evince, 3.3.3.1, using Fedora 17 (rawhide), and is not working in Evince 3.2.1 using Fedora 16.

Maybe you can compile Evince to get the bindings working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文