我想以编程方式绘制图像。本质上,我是在谈论将每个像素设置为图像映射,我想使用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()
我这样尝试过,但是我看不到屏幕上的任何东西,而我希望看到的一切都是红色的。我在这里做错了什么?
谢谢。
您的图片可能会被垃圾回收。您需要保存对图像的持久引用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句