使用Zelle的graphics.py模块,如何更改已绘制到GraphWin
窗口的GraphicsObject
的分层?根据我的理解,GraphicsObject
是按照draw()
绘制的顺序分层的,之后绘制的形状显示在之前绘制的形状的顶部,并且我使用的graphics.py documentation没有提供任何方法在之后更改此顺序。给定的代码如下所示:
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)
…生成如下窗口: window opened when program is run
…如何使square
稍后显示在circle
的顶部?i、 e.circle
被“发送到后面”或square
被“带到前面”,就像你如何在微软Word中操纵形状一样
我做了一些研究,发现tkinter.Canvas
类(从中graphics.GraphWin
继承)有tag_raise()
和tag_lower()
方法,这些方法与我前面描述的tkinter.Widget
对象相同,因此我尝试运行:
win.tag_raise(square)
…但什么也没发生(甚至没有出现错误)。我推断这可能是因为square
不是Widget
的实例,因此我天真地尝试更改我的graphics.py
文件,以便GraphicsObject
继承自Widget
:
# Source code imports tkinter as tk
class GraphicsObject(tk.Widget):
…但这导致我的前一行win.tag_raise(square)
抛出一个错误:
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'
任何帮助都将不胜感激
目前没有回答
相关问题 更多 >
编程相关推荐