2015-06-22 95 views
1

我真的很希望對此有所幫助,因爲它讓我絕對難住。我對自己的工作按下面的代碼:圖片將不會顯示在tkinter畫布上

from tkinter import * 
from PIL import ImageTk 

dam_level = [75] 

c = Canvas(width = 200, height = 235, relief = "sunken", borderwidth = 2) 
c.grid(row = 11, rowspan = 8, column = 4, columnspan = 2) 

c_width = 200 
c_height = 250 

y_stretch = 1.9 

y_gap = 35 

x_stretch = 15 
x_width = 90 

x_gap = 30 

for x, y in enumerate(dam_level): 

    x0 = x * x_stretch + x * x_width + x_gap 
    y0 = c_height - (y * y_stretch + y_gap) 
    x1 = x * x_stretch + x * x_width + x_width + x_gap 
    y1 = c_height - y_gap 

    c.create_rectangle(x0, y0, x1, y1, fill = "#008ae8") 

    y = (str(y)) 
    c.create_text(x0 + 10, y0, anchor = SW, text = (y, "%")) 
    c.create_text(x0 + 60, y1 + 5, anchor = N, text = "Catchment") 
    photo = ImageTk.PhotoImage(file =  
    "/Users/Name/Desktop/python3.4/water.png") 
    c.create_image(10, 10, image = photo, anchor = NW) 

mainloop() 

然而,當我把它放在我的主要應用在其自身的功能(用我的代碼的其餘部分),圖像將不顯示。圖形和畫布顯示,只是不water.png圖像。沒有錯誤日誌或任何東西。當我把這個功能放到我的應用程序中時,我做的唯一改變就是將'self'添加到這行中。 (我當然刪除'mainloop()')。

c = Canvas(self, width = 200, height = 235, relief = "sunken", borderwidth = 2) 

任何建議將不勝感激。

+0

http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm –

回答

0

感謝布賴恩,指引我朝着正確的方向前進。

修正了:

c.image = photo