python3 中的 PyGTK、PyGObject 和 Pgi

PyGTK, PyGObject and Pgi in python3

https://github.com/cztomczak/cefpython/blob/master/src/linux/binaries_64bit/kivy_.py中使用了pygtkgtk。 (注意源码只在Linux测试,我用的是Windows)

当我尝试 pip install pygtk 时,出现错误

Collecting pygtk
  Using cached pygtk-2.24.0.tar.bz2 (2.4 MB)
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\jared\AppData\Local\Temp\pip-install-rle_vbsd\pygtk\setup.py'"'"'; __file__='"'"'C:\Users\jared\AppData\Local\Temp\pip-install-rle_vbsd\pygtk\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\jared\AppData\Local\Temp\pip-pip-egg-info-pex79haa'
         cwd: C:\Users\jared\AppData\Local\Temp\pip-install-rle_vbsd\pygtk\
    Complete output (1 lines):
    ERROR: Could not import dsextras module: Make sure you have installed pygobject.
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

于是,我又去了pip install pygobject,又报错了。

Collecting pygobject
  Using cached PyGObject-3.38.0.tar.gz (712 kB)
  Installing build dependencies ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' 'c:\users\jared\appdata\local\programs\python\python37\lib\site-packages\pip' install --ignore-installed --no-user --prefix 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay' --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel pycairo
       cwd: None
  Complete output (34 lines):
  Collecting setuptools
    Using cached setuptools-50.3.0-py3-none-any.whl (785 kB)
  Collecting wheel
    Using cached wheel-0.35.1-py2.py3-none-any.whl (33 kB)
  Collecting pycairo
    Using cached pycairo-1.19.1.tar.gz (205 kB)
  Using legacy 'setup.py install' for pycairo, since package 'wheel' is not installed.
  Installing collected packages: setuptools, wheel, pycairo
      Running setup.py install for pycairo: started
      Running setup.py install for pycairo: finished with status 'error'
      ERROR: Command errored out with exit status 1:
       command: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\jared\AppData\Local\Temp\pip-install-faiuku8y\pycairo\setup.py'"'"'; __file__='"'"'C:\Users\jared\AppData\Local\Temp\pip-install-faiuku8y\pycairo\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\jared\AppData\Local\Temp\pip-record-kq9tlvv8\install-record.txt' --single-version-externally-managed --prefix 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay' --compile --install-headers 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay\Include\pycairo'
           cwd: C:\Users\jared\AppData\Local\Temp\pip-install-faiuku8y\pycairo\
      Complete output (18 lines):
      running install
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-3.7
      creating build\lib.win-amd64-3.7\cairo
      copying cairo\__init__.py -> build\lib.win-amd64-3.7\cairo
      copying cairo\__init__.pyi -> build\lib.win-amd64-3.7\cairo
      copying cairo\py.typed -> build\lib.win-amd64-3.7\cairo
      running build_ext
      building 'cairo._cairo' extension
      creating build\temp.win-amd64-3.7
      creating build\temp.win-amd64-3.7\Release
      creating build\temp.win-amd64-3.7\Release\cairo
      C:\Program Files (x86)\Microsoft Visual Studio19\BuildTools\VC\Tools\MSVC.27.29110\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DPYCAIRO_VERSION_MAJOR=1 -DPYCAIRO_VERSION_MINOR=19 -DPYCAIRO_VERSION_MICRO=1 -Ic:\users\jared\appdata\local\programs\python\python37\include -Ic:\users\jared\appdata\local\programs\python\python37\include "-IC:\Program Files (x86)\Microsoft Visual Studio19\BuildTools\VC\Tools\MSVC.27.29110\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\include.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\include.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\include.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\include.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\include.0.18362.0\cppwinrt" /Tccairo/device.c /Fobuild\temp.win-amd64-3.7\Release\cairo/device.obj
      device.c
      C:\Users\jared\AppData\Local\Temp\pip-install-faiuku8y\pycairo\cairo\pycairo.h(37): fatal error C1083: Cannot open include file: 'cairo.h': No such file or directory
      error: command 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\bin\HostX86\x64\cl.exe' failed with exit status 2
      ----------------------------------------
  ERROR: Command errored out with exit status 1: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\jared\AppData\Local\Temp\pip-install-faiuku8y\pycairo\setup.py'"'"'; __file__='"'"'C:\Users\jared\AppData\Local\Temp\pip-install-faiuku8y\pycairo\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\jared\AppData\Local\Temp\pip-record-kq9tlvv8\install-record.txt' --single-version-externally-managed --prefix 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay' --compile --install-headers 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay\Include\pycairo' Check the logs for full command output.
  ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' 'c:\users\jared\appdata\local\programs\python\python37\lib\site-packages\pip' install --ignore-installed --no-user --prefix 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay' --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel pycairo Check the logs for full command output.

然后我参考了 Whosebug 上的一些帖子,他们告诉我 python3 中不再需要 gtk 并用 pgi 代替了它。 (不知道源码里的pygtk还是gtk应该换掉)于是试了一下pip install pgi,又报错

Collecting pgi
  Using cached pgi-0.0.11.2.tar.gz (239 kB)
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\setup.py'"'"'; __file__='"'"'C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\jared\AppData\Local\Temp\pip-pip-egg-info-5e57ceft'
         cwd: C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\
    Complete output (21 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\setup.py", line 18, in <module>
        import pgi
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\__init__.py", line 10, in <module>
        from .importer import require_version, get_required_version
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\importer.py", line 15, in <module>
        from .clib.gir import GIRepository
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\clib\gir\__init__.py", line 8, in <module>
        from .giargument import GIArgument
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\clib\gir\giargument.py", line 9, in <module>
        from ..glib import gboolean, gint8, guint8, gint16, guint16, gint32, guint32
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\clib\glib.py", line 16, in <module>
        _glib = find_library("glib-2.0")
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\clib\_utils.py", line 99, in find_library
        _internal[name] = cdll.LoadLibrary(_so_mapping[name])
      File "c:\users\jared\appdata\local\programs\python\python37\lib\ctypes\__init__.py", line 442, in LoadLibrary
        return self._dlltype(name)
      File "c:\users\jared\appdata\local\programs\python\python37\lib\ctypes\__init__.py", line 364, in __init__
        self._handle = _dlopen(self._name, mode)
    OSError: [WinError 126] The specified module could not be found
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

我不知道发生了什么。我希望有人能把我从这个无限的错误循环中拉出来。非常感谢。

首先我想说的是我使用的是 Xubuntu 20.04,所以 Linux.

为了使 kivy 示例正常工作,您需要将 pygtk 模块替换为 PyGobject 模块。所以安装它,这里是说明:

https://pygobject.readthedocs.io/en/latest/getting_started.html#windows-logo-windows

现在在这个文件中 https://github.com/cztomczak/cefpython/blob/master/src/linux/binaries_64bit/kivy_.py 您需要更改导入和其他一些内容。这是我的进口商品的样子

...

from cefpython3 import cefpython as cef

import gi       # notice this line is added
# import pygtk  # this one is commented out or you can delete it
from gi.repository import Gtk as gtk    # this one is added
# import gtk    # this one is commented out or you can delete it
import sys

...

from kivy.graphics.texture import Texture
from kivy.uix.boxlayout import BoxLayout
from kivy.base import EventLoop

# Global variables
g_switches = None

# PyGTK required
# pygtk.require('2.0') # this one is commented out or you can delete it


class BrowserLayout(BoxLayout):
...

我还需要修复传递给在第 208 行某处调用的 windowInfo.SetAsOffscreen 方法的参数(在我的例子中),查找 start_cef 方法定义。我根据这个 https://github.com/cztomczak/cefpython/blob/master/api/WindowInfo.md#setasoffscreen 将参数设置为 0,因为它提到:

You can pass 0 as parentWindowHandle, but then some things like context menus and plugins may not display correctly.

因此代码如下所示:

...
#popup widgets to work.
gtkwin = gtk.Window()
gtkwin.realize()

# WindowInfo offscreen flag
windowInfo = cef.WindowInfo()
windowInfo.SetAsOffscreen(0)
    
# Create Broswer and naviagte to empty page <= OnPaint won't get
# called yet
...