使用PIL或枕头绘制图像并用TKinter显示

费德里科·托马塞蒂(Federico Tomassetti)

我想以编程方式绘制图像。本质上,我是在谈论将每个像素设置为图像映射,我想使用PIL /枕头来实现。然后,我想在屏幕上显示它。GUI基于TKinter。

root = Tk()
root.wm_title("Lands - A world generator")
root.resizable(0,0)

prepare_menu()

canvas = Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()

root.mainloop()

prepare_menu套与一个事件处理程序,它调用该函数的菜单和同事一个条目show_elevation_map是这样的:

def show_elevation_map(p, width, height):    
    hm = platec.get_heightmap(p)
    img = PIL.Image.new('RGBA', (width, height))
    pixels = img.load()
    for y in range(0, height):
        for x in range(0, width):            
            pixels[x, y] = (255, 0, 0, 255)
    pi = ImageTk.PhotoImage(img)
    sprite = canvas.create_image(100, 100, image=pi)
    canvas.update()

我这样尝试过,但是我看不到屏幕上的任何东西,而我希望看到的一切都是红色的。我在这里做错了什么?

谢谢。

布莱恩·奥克利(Bryan Oakley)

您的图片可能会被垃圾回收。您需要保存对图像的持久引用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章