2

我最近使用 Homebrew 安装了 pygtk 以尝试运行以下程序:

#!/usr/bin/python3
from gi.repository import Gtk, GObject
import time

class TimerWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Timer")
        self.timer = Gtk.Label()
        self.add(self.timer)

    def update(self):
        self.timer.set_text(time.strftime('%H:%M:%S'))
        return True

if __name__ == "__main__":
  win = TimerWindow()
  win.connect("delete-event", Gtk.main_quit)
  win.show_all()
  GObject.timeout_add(200, win.update)
  Gtk.main()

该程序在多个 Linux 发行版中运行良好,但在我的 Mac 上运行时遇到了一些麻烦。
似乎python找不到gi包,这很奇怪,因为我已经安装了它(?)。诚然,我对 python 很陌生,所以我可能弄错了。

运行时给出的错误如下:

File "test.py", line 2, in <module>
    from gi.repository import Gtk, GObject
ImportError: No module named gi.repository

我在网上遵循了多个关于如何安装库的指南,但似乎没有一个有效。
有没有人有这方面的经验,或者可以使程序在 OSX 上正常工作?

非常感谢

4

1 回答 1

3

PyGObject(gi) 安装在哪个目录?该包应该已经移动到 python 路径,python 将从这些目录中扫描模块。

我们可以使用它来获取当前的python路径:

import sys
print(sys.path)

是的,将 gi 包放入这些目录之一。

python 和 python3 有不同的路径。

于 2013-06-01T05:49:55.577 回答