How do you layer GraphicsObjects in a window with Zelle's graphics.py?

398 Views Asked by At

Using Zelle's graphics.py module, how do you alter the layering of GraphicsObjects that are already drawn to a GraphWin window? By my understanding, GraphicsObjects are layered in the order that they were drawn with draw(), with shapes drawn later being displayed on top of shapes previously drawn, and the graphics.py documentation I use does not offer any way to change this order afterwards. Given code such as this:

win = GraphWin("Test", 500, 500)

square = Rectangle(Point(70, 70), Point(200, 200))
square.setFill("Lawn Green")

circle = Circle(Point(100, 100), 50)
circle.setFill("Tomato")

square.draw(win)
circle.draw(win)

...which produces a window like this: window opened when program is run

...how do I make it so that square is displayed on top of circle later on? i.e. circle is "sent to the back" or square is "brought to the front," like how you can manipulate shapes in, say, Microsoft Word.

I did some research and found that the tkinter.Canvas class, from which graphics.GraphWin inherits, has tag_raise() and tag_lower() methods that do what I described above with tkinter.Widget objects, so I tried running:

win.tag_raise(square)

...but nothing happened (not even an error was raised). I reasoned that that may be because square is not an instance of Widget, so then I naively tried to alter my graphics.py file so that GraphicsObject inherits from Widget:

# Source code imports tkinter as tk
class GraphicsObject(tk.Widget):

...but that caused my previous line win.tag_raise(square) to throw an error:

Traceback (most recent call last):
  File "C:\Users\*****\Documents\Python Projects\test.py", line 20, in <module>
    main()
  File "C:\Users\*****\Documents\Python Projects\test.py", line 17, in main
    win.tag_raise(square)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 2937, in tag_raise
    self.tk.call((self._w, 'raise') + args)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1658, in __str__
    return self._w
AttributeError: 'Rectangle' object has no attribute '_w'

Any help would be appreciated!

0

There are 0 best solutions below