gpt4 book ai didi

python - TK Framework双重实现问题

转载 作者:太空狗 更新时间:2023-10-29 18:03:43 33 4
gpt4 key购买 nike

我正在测试使用 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

我在网上查看过,我的系统库中的 Tk 框架和 anaconda 库中的另一个框架似乎存在问题。然而,这些解决方案似乎都没有真正奏效。任何可能的解决方案或解决方法?

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

最佳答案

我知道我创造了赏金,但我不耐烦了,决定进行调查,现在我得到了对我有用的东西。我有一个与你的非常相似的 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

发生的事情是二进制文件~/anaconda/envs/py35/lib/python3.5/site-packages/PIL/_imagingtk.so链接到框架,而不是环境中的 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 位了吗?这意味着您在路径上找到的任何一个。这对 python 很有用。 _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 绑定(bind),即当前事件的 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/。这可能也有效,甚至可能更好。

关于python - TK Framework双重实现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35593602/

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