2015-04-17 111 views
0

我正在使用IPython筆記本在Python中創建Tk應用程序。我可以做一個按鈕,文本標籤:不能使用IPython Notebook製作tk圖像按鈕

%matplotlib tk 
import tkinter as tk 

root = tk.Tk() 
button = tk.Button(root, text = 'IPython') 
button.pack() 

但如果我嘗試做一個按鈕,圖像我得到一個錯誤信息。在命令行中運行時,此代碼工作正常,但崩潰時在IPython的筆記本執行:

%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() 

print語句驗證光象創建對象(即無「找不到文件」的錯誤)。到tk.Button調用導致這個錯誤:

TclError: image "pyimage2" doesn't exist 

我使用的IPython/jupyter 3.0.0在Mac OS X 10.10.3

任何人有一個建議?

+0

ipython運行在同一臺機器上嗎? ipython環境中的當前工作目錄是什麼?如果使用圖像文件的完整路徑,會發生什麼情況?在按鈕中使用圖像之前,能夠獲得圖像的大小(寬度,高度)嗎? –

+0

同一臺機器,筆記本與圖像位於同一目錄。是的,我可以調用img.height()和img.width()來獲取屬性,它們就是我所期望的。 –

回答