tkinter分子模拟问题

时间:2018-01-04 17:20:41

标签: python tkinter tkinter-canvas

我用tkinter构建了一个粒子网格,现在我希望格子中的每个粒子都按照我从两个文本文件读入数组的x和y坐标列表移动。我试图创建一个内部函数的类,用canvas.move函数定义移动但是我得到错误TclError:错误#coordinates:预期0或4,得到400.如何解决这个问题?

from Tkinter import *
import random
import time
import csv


tk = Tk()
N = 100
T = 500
canvas = Canvas(tk, width=100, height=100)
tk.title("Test")
canvas.pack()
n = 5
t = 10

step1 = []
step2 = []
textFile1 = open('/Users/francislempp/Desktop/major project/C++     programs/Molecular Dynamics 2D/Molecular_Dynamics_2D-gupnvjunowwmjcfiyoursdhzytow/Build/Products/Debug/motionX', 'r')
lines = textFile1.readlines()
for line in lines:
    step1.append(line.split(" "))

textFile2 = open('/Users/francislempp/Desktop/major project/C++ programs/Molecular Dynamics 2D/Molecular_Dynamics_2D-gupnvjunowwmjcfiyoursdhzytow/Build/Products/Debug/motionY', 'r')
lines = textFile2.readlines()
for line in lines:
    step2.append(line.split(" "))


def moves(xspeed, yspeed):
    canvas.move(xspeed, yspeed)        

class Ball:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.shape = canvas.create_oval((x,y,x,y), fill = color)


    def move(self):
        canvas.move(self.shape, self.x, self.y)
        pos = canvas.coords(self.shape)
        if pos[3] >= 100 or pos[1] <= 0:
            self.y = -self.y
        if pos[2] > 100 or pos[0] <= 0:
            self.x = -self.x

    def delete(self):
        canvas.delete(self.shape)


balls = []


for x in range(4,100,10):
    for y in range(4,100,10):
        #canvas.create_oval((x,y,x,y), fill='red')
        Ball(x,y,"red")

tk.update()

for i in step1:
    for j in step2:
        Ball(i,j,"red")
        Ball.move()
        tk.update()




tk.mainloop()

1 个答案:

答案 0 :(得分:0)

您必须在列表中保存项目并使用此列表移动

我复制上一个问题的例子

import tkinter as tk
import random

# --- functions ---

def move():
    for point_id in points:
        x = random.randint(-1, 1)
        y = random.randint(-1, 1)
        canvas.move(point_id, x, y)

    root.after(100, move)

# --- main ---

points = []

root = tk.Tk()

canvas = tk.Canvas(root, width=100, height=100)
canvas.pack()

for x in range(4, 100, 10):
    for y in range(4, 100, 10):
        point_id = canvas.create_oval(x, y, x, y, fill="red")
        points.append(point_id)

move()

root.mainloop()

编辑:与您的课程但没有来自文件的数据。

我使用random代替文件中的数据来显示如何创建粒子并移动它们。

每个球都以自己的速度/方向移动,当触摸边界时,它会发生变化。

import tkinter as tk
import random

# --- classes ---

class Ball:

    def __init__(self, x, y, speed_x, speed_y, color):
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.shape = canvas.create_oval((x, y, x, y), fill=color)

    def move(self):

        canvas.move(self.shape, self.speed_x, self.speed_y)

        pos = canvas.coords(self.shape)

        # change speed/direction when touch border
        if pos[3] >= 100 or pos[1] <= 0:
            self.speed_y = -self.speed_y
        if pos[2] > 100 or pos[0] <= 0:
            self.speed_x = -self.speed_x

    def delete(self):
        canvas.delete(self.shape)

# --- functions ---

def move():
    for ball in all_balls:
        ball.move()

    root.after(100, move)

# --- main ---

root = tk.Tk()

canvas = tk.Canvas(root, width=100, height=100)
canvas.pack()

all_balls = []

for x in range(4, 100, 10):
    for y in range(4, 100, 10):
        offset_x = random.randint(-2, 2)
        offset_y = random.randint(-2, 2)
        all_balls.append(Ball(x, y, offset_x, offset_y, "red"))

move()

root.mainloop()