我写了一个模拟恒星周围行星轨道的程序,我想要一个可视化工具来显示恒星周围当前行星的位置。由于所有的行星都存储了它们的x和y坐标,所以在图像上绘制它们很简单,但我不知道更新图像并在每个模拟步骤后重新显示的好方法。
目前,我这样绘制图像(使用行星位置将像素变为红色)
def createwindow():
img = Image.new( 'RGB', (750,730), "black")
pixels = img.load()
for thing in celestials:
pixels[thing.xpos+375+sun.xpos,thing.ypos+375+sun.xpos]=250,20,20所以我有一张很好地显示行星的图像,它可以在每次行星运动后重新制作。但是我如何在tkinter窗口中显示它呢?
有没有一种方法可以在不使用必须保存和加载的Photoimage的情况下做到这一点?或者一种更好的方式一起完成这一切?也许为每个行星分配一个标签,并直接在tkinter黑色窗口上绘制它们,然后在每个模拟步骤中更新标签的位置?
感谢所有的帮助
发布于 2018-04-12 11:50:55
您可能应该在画布上绘制动画,而不是在静态图像上;然后可以移动在模拟的每个步骤中修改的动画元素。
类似的东西:(按底部的start开始)
import tkinter as tk
def animate():
canvas.move(1, 5, 0) # 1 refers to the object to be moved ID, dx=5, dy=0
root.update()
root.after(50, animate)
if __name__ == '__main__':
root = tk.Tk()
frame = tk.Frame(root)
canvas = tk.Canvas(root, width=800, height=400)
canvas.pack()
canvas.create_polygon(10, 10, 50, 10, 50, 50, 10, 50) # canvas object ID created here by tkinter
btn = tk.Button(root, text='start', command=animate)
btn.pack()
root.mainloop()https://stackoverflow.com/questions/49787315
复制相似问题