gpt4 book ai didi

python - gtk+2、gtk+3 和 pygobject3 全部安装到 mac 后为 "No module named pygtk found"

转载 作者:行者123 更新时间:2023-12-01 02:47:53 24 4
gpt4 key购买 nike

我正在尝试让 gtk 在我的 Mac 上运行。

Gtk+2、Gtk+3 和 pygobject3 都已使用自制程序安装到我的 Mac 上:

Terminal screenshot for the installs

但是,当我尝试将 gtk 和/或 pygtk 导入到 python 项目中时,无论是 2.7.10 项目还是 3.6.1 项目,以下代码总是突出显示,表示该模块不存在(未找到名为 pygtk 的模块):

#!/usr/bin/env python

import sys
import pygtk
pygtk.require("3.22.16") '''For 3.6.1 version of python'''
import gtk
import gtk.glade

如何让 GTK 导入到我的 Mac 上的 pycharm 上的 python 项目中,为什么这不起作用?

编辑:尝试通过 pycharm 安装 Pygobject 时,我得到以下命令输出:

The directory '/Users/shihaoheng/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Could not find a version that satisfies the requirement PyGObject (from versions: )

No matching distribution found for PyGObject (1)

当我尝试从 PyCharm 安装 pygtk 时,出现此错误:

Collecting pygtk-shell Using cached pygtk-shell-1.90.1.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "", line 1, in File "/private/var/folders/hn/9m609ll170942x44r13zxg980000gn/T/pycharm-packaging/pygtk-shell/setup.py", line 26, in from PyGTKShell.Core import pygtkshell_version File "/private/var/folders/hn/9m609ll170942x44r13zxg980000gn/T/pycharm-packaging/pygtk-shell/PyGTKShell/Core.py", line 26, in import gtk ModuleNotFoundError: No module named 'gtk'

----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/hn/9m609ll170942x44r13zxg980000gn/T/pycharm-packaging/pygtk-shell/

最佳答案

我认为 pygtk 仅适用于 Gtk2。您正在安装 Gtk3 (pygobject3)试试这个代码:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

gi.repository 包含整个 Gtk 对象堆栈,例如:Gtk、Gdk、GLib、GdkPixbuf...,因此您可以从那里导入它们

顺便说一句,这里有一些很好的 Gtk3 代码示例:http://python-gtk-3-tutorial.readthedocs.io/en/latest/introduction.html

关于python - gtk+2、gtk+3 和 pygobject3 全部安装到 mac 后为 "No module named pygtk found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45093778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com