【问题标题】:Image won't display on tkinter canvas图像不会显示在 tkinter 画布上
【发布时间】:2015-06-22 11:42:11
【问题描述】:

我真的希望能得到一些帮助,因为它让我非常难过。我有自己的代码,如下所示:

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)

任何建议将不胜感激。

【问题讨论】:

标签: python-3.x canvas tkinter photoimage


【解决方案1】:

感谢 Bryan,为我指明了正确的方向。

固定为:

c.image = photo

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2013-12-02
    相关资源
    最近更新 更多