【问题标题】:TK Framework double implementation issueTK Framework双重实现问题
【发布时间】:2016-06-06 05:42:58
【问题描述】:

我正在测试使用 Tkinter 模块创建 GUI。我试图使用 PIL 将图像添加到 GUI。我的代码如下所示:

import Tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()
root.title('background image')

imfile = "foo.png"
im = Image.open(imfile)
im1 = ImageTk.PhotoImage(im)

当我运行这段代码时,我遇到了一些导致段错误的错误。

objc[5431]: Class TKApplication is implemented in both/Users/sykeoh/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[5431]: Class TKMenu is implemented in both /Users/sykeoh/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[5431]: Class TKContentView is implemented in both /Users/sykeoh/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[5431]: Class TKWindow is implemented in both /Users/sykeoh/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
Segmentation fault: 11

我在网上查看过,它看起来是我的 Systems 库中的 Tk 框架和 anaconda 库中的另一个问题。然而,似乎没有一个解决方案真正奏效。任何可能的解决方案或变通方法?

运行 ImageTk.Photoimage 时会出现问题。如果我删除那行代码,就没有问题了。

【问题讨论】:

  • 您可以在IDLE 或终端中运行相同的代码吗?如果这是 anaconda 特定问题,您可能需要提供有关设置的更多具体信息
  • 只是为了确定,您使用的是原始 PIL 还是 Pillow?

标签: python python-2.7 tkinter python-imaging-library anaconda


【解决方案1】:

我知道我创造了赏金,但我不耐烦了,决定进行调查,现在我有一些对我有用的东西。我有一个与你的非常相似的 python 示例,除了尝试使用 Tkinter 显示在命令行上传递的图像外,它几乎没有任何作用,如下所示:

calebhattingh $ python imageview.py a.jpg
objc[84696]: Class TKApplication is implemented in both /Users/calebhattingh/anaconda/envs/py35/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[84696]: Class TKMenu is implemented in both /Users/calebhattingh/anaconda/envs/py35/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[84696]: Class TKContentView is implemented in both /Users/calebhattingh/anaconda/envs/py35/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[84696]: Class TKWindow is implemented in both /Users/calebhattingh/anaconda/envs/py35/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
Segmentation fault: 11

发生的情况是 binary 文件 ~/anaconda/envs/py35/lib/python3.5/site-packages/PIL/_imagingtk.so 已链接到框架,而不是 env 中的 Tcl/Tk 库。您可以使用otool 查看链接设置:

(py35)?  ~/anaconda/envs/py35/lib/python3.5/site-packages/PIL
calebhattingh $ otool -L _imagingtk.so 
_imagingtk.so:
        /System/Library/Frameworks/Tcl.framework/Versions/8.5/Tcl (compatibility version 8.5.0, current version 8.5.9)
        /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk (compatibility version 8.5.0, current version 8.5.9)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)

看到那两个“框架”行了吗?对于 anaconda,我们不希望这样。我们想使用环境中的库。所以让我们改变它们吧!

首先备份你的二进制文件(以防你想恢复):

$ cp _imagingtk.so _imagingtk.so.bak

现在在命令行上运行它(假设您与envname/lib 在同一个文件夹中):

$ install_name_tool -change "/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk" "@rpath/libtk8.5.dylib" _imagingtk.so
$ install_name_tool -change "/System/Library/Frameworks/Tcl.framework/Versions/8.5/Tcl" "@rpath/libtcl8.5.dylib" _imagingtk.so

你看到里面的@rpath 位了吗?这意味着您在路径上找到的任何一个。这对蟒蛇很有用。 _imagingtk.so 库中的链接现在如下所示:

(py35)?  ~/anaconda/envs/py35/lib/python3.5/site-packages/PIL
calebhattingh $ otool -L _imagingtk.so
_imagingtk.so:
        @rpath/libtcl8.5.dylib (compatibility version 8.5.0, current version 8.5.9)
        @rpath/libtk8.5.dylib (compatibility version 8.5.0, current version 8.5.9)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)

在此之后,您的代码将运行。有人可能应该尝试在上游获取此信息。

附录:python 发行版中的 Tkinter 绑定,即当前活动的 conda env,具有以下链接:

?  ~/anaconda/envs/py35/lib/python3.5/lib-dynload
calebhattingh $ otool -L _tkinter.cpython-35m-darwin.so 
_tkinter.cpython-35m-darwin.so:
        @loader_path/../../libtcl8.5.dylib (compatibility version 8.5.0, current version 8.5.18)
        @loader_path/../../libtk8.5.dylib (compatibility version 8.5.0, current version 8.5.18)
        /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.0.0)

如果您愿意,您可以使用install_name_tool 来代替我上面使用的@loader_path/../../,即@rpath/。这可能也会起作用,甚至可能会更好。

【讨论】:

  • 它对我有用,谢谢!此外,您还可以找到通过简单命令链接到系统 Tkinter 的二进制文件:grep -rnw 'Tk.framework ' grep -rnw 'Tcl.framework '
  • 非常感谢。那么这是 Conda 中的错误吗?
  • 不,我认为这与枕头有关。如果包是 conda 包,那么错误是 pkg 没有正确构建。如果包是 pip 安装的(我不记得了),那么这个错误是我期望将非 conda 包安装到 conda 环境中可以工作;)
【解决方案2】:

我尝试对这个问题进行自己的调查(在我的情况下是 matplotlib 的问题)

  • 我尝试找到与 System Tcl/Tk 库路径链接的二进制文件
  • 通过库我找到了文件,这看起来很有趣。它的名称为osx-tk.patch,并被放置在~/anaconda/pkgs/matplotlib-1.5.1-np111py35_0/info/recipe/
  • 找到osx-tk.patch 源到GitHub

  • 在阅读了补丁和Conda documentation之后,我终于确信包安装错误,我记得我使用pip安装matplotlib!

  • 我使用pip 删除了旧软件包,并使用conda install matplotlib 命令安装了新软件包

  • 现在一切都开始运转起来了!

简要总结:

在 conda 环境中使用 pip 包管理器,我弄乱了我的依赖关系,因为 conda 可能使用特殊的 recipes,这点安装程序如何在 conda 虚拟环境中正确安装包

超简短摘要:

完全删除依赖包并使用 conda 包管理器重新安装(仅在困难的情况下使用重新绑定)

【讨论】:

  • 这是一个非常有趣的点,我没有考虑过。我相当肯定我是通过 conda 安装 Pillow 的,但是对于其他将包安装到 conda 和 pip 的同一环境中的情况,仍然值得考虑。
【解决方案3】:

我完全按照@cjrh 对_imagetk.so 所说的去做,但在~/anaconda/lib/python3.5/lib-dynload/ 中为_tkinter.so 做了,效果很好!

cd ~/anaconda/lib/python3.5/lib-dynload

$ install_name_tool -change "/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk" "@rpath/libtk8.5.dylib" _tkinter.so
$ install_name_tool -change "/System/Library/Frameworks/Tcl.framework/Versions/8.5/Tcl" "@rpath/libtcl8.5.dylib" _tkinter.so

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-22
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多