图像不会显示在tkinter画布上

时间:2015-06-22 11:42:11

标签: python-3.x canvas tkinter photoimage

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

@ECHO OFF
set def="0"
SET /P uname=Enter the drive (letter:): 
IF "%uname%"=="" GOTO Error
    RemoveDrive.exe "%uname%" -L
if %ERRORLEVEL% == def
  shutdwon /s
else
  echo 'Something have problem'
GOTO End
:Error
ECHO Please enter your drive's name! 
:End

然而,当我把它放在我的主应用程序中它自己的函数(我的代码的其余部分)时,图像将不会显示。图形和画布显示,而不是water.png图像。没有错误日志或任何东西。我把它放在我的应用程序中时所做的唯一更改是在此行中添加“self”。 (我当然删除'mainloop()'。

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

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

感谢布莱恩,指出我正确的方向。

修正:

c.image = photo