如何使用Zelle的graphics.py对窗口中的GraphicsObject进行分层?

2024-04-25 22:24:07 发布

您现在位置:Python中文网/ 问答频道 /正文

使用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'

任何帮助都将不胜感激


Tags: inpytkintertagline绘制widgetwin