我正在使用 IPython 笔记本在 Python 中创建 Tk 应用程序。我可以制作一个带有文本标签的按钮:
%matplotlib tk
import tkinter as tk
root = tk.Tk()
button = tk.Button(root, text = 'IPython')
button.pack()
但是,如果我尝试用图像制作一个按钮,我会收到一条错误消息。此代码从命令行运行时运行良好,但在 IPython Notebook 中执行时会崩溃:
%matplotlib tk
import tkinter as tk
root = tk.Tk()
img = tk.PhotoImage(file='IPy_header.gif')
print(img)
button = tk.Button(root, image = img)
button.pack()
打印语句验证 PhotoImage 对象是否已创建(即没有“找不到文件”错误)。对 tk.Button 的调用会导致此错误:
TclError: image "pyimage2" doesn't exist
我在 Mac OS X 10.10.3 上使用 ipython/jupyter 3.0.0
有人有建议吗?