1

My code reads from a binary, converts the hex into images and disposes them onto a canvas.

I can click to a single tile on the canvas and change it properly. But I am now aware that I absolutely need an undo command (like a common CRTL+Z).

So I created a stack_list=[]. Inside my function clickA, which is the function that changes the selected tile, I have inserted this code:

stack_list.append(clickA) 

If I run print(stack_list) I obtain [<function clickA at 0x00000226B0CA9CA0>].

Then I wrote another function:

def undo(): 
    if not stack_list: 
        return 
    previous = stack_list.pop(-1)       
    print(stack_list)

and attached it to a button. It seems to works because print(stack_list) gives me [] but the original state is not restored.

It is possible to obtain the original state without having to "fake an original state" (for i.e. by storing the original image into a variable and then call it back)?

Donatello
  • 33
  • 7
  • Does this answer your question? [python/tkinter paint program undo function](https://stackoverflow.com/questions/42510713/python-tkinter-paint-program-undo-function) Also you might want to take a look at [this](https://stackoverflow.com/a/23692118/11106801) – TheLizzard May 19 '21 at 09:31
  • Well not really because I am not using classes and, at the moment, I do not know how to deal with them. – Donatello May 19 '21 at 09:34
  • 2
    `tkinter.Canvas` doesn't have a built in undo mechanic. Therefore, you need to store all of the changes that you make to the canvas in a stack. For example if you call `canvas_id = canvas.create_rectangle(...)` and push it on the stack you can use `canvas_id =stack.pop()` and then `canvas.delete(canvas_id)` – TheLizzard May 19 '21 at 09:37

0 Answers0