Python Tkinter update canvas

Hello, I have a tuple in python with colors that are related to the squares that are drawn in the canvas with the following text:

colour_mapping = {0: "red", 1: "green", 2: "blue" , 3:"purple"}

To be more specific, for example, the node in the tuple:

((2, 3), (3, 3))

This means that 4 squares should be made as follows:

blue square    purple square
purple square     purple square

and then their colors should be changed according to the next node in my tuple

To do this, I repeat the tuple, and for each element I draw a new rectangle on the canvas, and then call the function time.sleep()to give the user time to see the differences in the previous state. My problem is that only the last node is displayed correctly, and all the rest are not displayed. Can you help me?

Here is my code:

self.parent.title("AlienTiles")
self.style = Style()
self.style.theme_use("default")

self.frame =  Frame(self, relief=RAISED, borderwidth=1)
self.frame.pack(fill=BOTH, expand=1)

self.canvas = Canvas(self.frame)
self.canvas.pack(fill=BOTH, expand=1)

self.pack(fill=BOTH, expand=1)


for i in range(len(path)) : #the tuple is path

            state = path[i].state
            print state
            time.sleep(1)
            y_offset=10
            for x in state:
                start_x=40
                start_y=10
                i=1
                x_offset=0

                for y in x:

                    x0=(start_x*i)+x_offset
                    y0=(start_y*i)+y_offset
                    x1=x0+size
                    y1=y0+size
                    colour=colour_mapping[y]

                    print colour

                    self.canvas.create_rectangle(x0, y0, x1, y1, fill=colour)
                    x_offset=x_offset+size+10


                y_offset=y_offset+size+10

, , . -, -, ?

+6
2

canvas - "". , .

- self.canvas.update_idletasks, , .

- . , - . after.

, , . , while . , , . after , , .

, :

def do_one_frame(self, ...):
    # do whatever you need to draw one frame

    if (there_is_more_work_to_be_done):
        self.after(10, do_one_frame)

, , - , , 10 . , , , .

- . , . fooobar.com/questions/332772/...

+11

-, , ((2,3) (3,3)) . , -, , , ?

-, , ", node ". , - ((2,3) (3,3)) 4 , ((2,3) (3,3) (1, 2)), , 6 ?

-, ? , , , .

, , , , , . :

for i in range(len(path)) :

, , , , . , , . , .

-1

Source: https://habr.com/ru/post/1523739/


All Articles