import tkinter
from tkinter import *
import math
class Template:
def __init__(self):
self.window = Tk()
self.window.title("2D Display")
self.canvas = self.canvas_display()
self.line1 = self.line_creation(500 * .3,0,500 * .3, 1000)
self.line3 = self.line_movement_creation(0, 0,2000, 0)
self.speedx = 0 # Movement of Line3
self.speedy = 9 # Movement of line3
self.active = True
#self.move_active() #Code that creates the problem
It makes a grid currently. As soon as I try to make the horizontal lines move. The program crashes. Works without self.move_active()
so I commented it out.
self.canvas.update()
def canvas_display(self): #canvas
canvas = Canvas(self.window, width=500, height=400,
background='black')
canvas.pack(expand=True, fill="both")
canvas.update()
return canvas
def line_creation(self,x,y,x1,y1): #creation of multpie lines
spacing = 0
for i in range(11):
self.canvas.create_line(x spacing, y ,x1 spacing ,y1 ,
width=2, fill="white")
spacing = 100
def line_movement_creation(self,x,y,x1,y1):
spacing1 = 0
for i in range(11):
self.canvas.create_line(x, y spacing1 ,x1 ,y1 spacing1
,width=2, fill="white")
spacing1 = 100
These two functions line_creation and line_movement_creation create horizontal and vertical lines. Both are working well.
def line_update(self): #line movement method
self.canvas.move(self.line3, self.speedx, self.speedy)
pos = self.canvas.coords(self.line3)
if pos[3] >= 800: #o
self.canvas.move(self.line3, self.speedx,self.speedy-800)
def move_active(self):
if self.active:
self.line_update()
self.window.after(40, self.move_active)
def run(self):
self.window.mainloop()
These two functions make the lines move. I think the problem is making the 10 lines and trying to move them all at once, or just these functions itself but I have no idea.
if __name__ == '__main__':
Temp = Template()
Temp.run()
CodePudding user response:
This code should work:
import time
from tkinter import *
class Template:
def __init__(self):
self.window = Tk()
self.window.title("2D Display")
self.canvas = self.canvas_display()
self.line1 = self.line_creation(500 * .3,0,500 * .3, 1000)
self.line3 = self.line_movement_creation(0, 0,2000, 0)
self.speedx = 0 # x movement of line3
self.speedy = 9 # y movement of line3
self.active = True
self.move_active() #Code that creates the problem
self.canvas.update()
def canvas_display(self): #canvas
canvas = Canvas(self.window, width=500, height=400, background='black')
canvas.pack(expand=True, fill="both")
canvas.update()
return canvas
def line_creation(self,x,y,x1,y1): #creation of multple lines
spacing = 0
lines = []
for i in range(11):
id = self.canvas.create_line(
x spacing,
y,
x1 spacing,
y1,
width=2,
fill="white",
)
lines.append(id)
spacing = 100
return lines
def line_movement_creation(self,x,y,x1,y1):
spacing1 = 0
lines = []
for i in range(11):
id = self.canvas.create_line(
x,
y spacing1,
x1,
y1 spacing1,
width=2,
fill="white"
)
lines.append(id)
spacing1 = 100
return lines
def line_update(self): #line movement method
for line in self.line3:
self.canvas.move(line, self.speedx, self.speedy)
pos = self.canvas.coords(line)
if pos[3] >= 800:
self.canvas.move(line, self.speedx, self.speedy - 800)
def move_active(self):
if self.active:
self.line_update()
self.window.after(40, self.move_active)
def run(self):
self.window.mainloop()
if __name__ == '__main__':
Temp = Template()
Temp.run()
The difference: I made line_creation()
and line_movement_creation()
each return a list of the IDs of the lines that they created. line_creation()
's list is saved to self.line1
, and line_movement_creation()
's list is saved to self.line3
. line_update()
then uses self.line3
to move the horizontal lines.
Here is the old line_update()
method:
def line_update(self): #line movement method
self.canvas.move(self.line3, self.speedx, self.speedy)
pos = self.canvas.coords(self.line3)
if pos[3] >= 800: #o
self.canvas.move(self.line3, self.speedx,self.speedy-800)
...and here is the new line_update()
method:
def line_update(self): #line movement method
for line in self.line3:
self.canvas.move(line, self.speedx, self.speedy)
pos = self.canvas.coords(line)
if pos[3] >= 800:
self.canvas.move(line, self.speedx, self.speedy - 800)
As you can see, the only difference here is that it iterates through all the lines in self.line3
and moves each one individually. The movement coordinates are exactly the same.