1

我真的希望对此有所帮助,因为它让我非常难过。我有自己的代码,如下所示:

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 图像。没有错误日志或任何东西。当我把它放在我的应用程序中时,我所做的唯一改变是在这一行中添加“自我”。(我当然删除了'mainloop()')。

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

任何建议将不胜感激。

4

1 回答 1

1

感谢布莱恩,为我指明了正确的方向。

固定:

c.image = photo
于 2015-06-22T12:02:38.270 回答