每次调用draw事件时都会清除我的绘图区域。如何避免绘图区域被清除?
谢谢
#!/usr/bin/env python3
import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk, Gdk
import cairo
import math
class MouseButtons:
LEFT_BUTTON = 1
RIGHT_BUTTON = 3
class Example(Gtk.Window):
def __init__(self):
super(Example, self).__init__()
self.init_ui()
def init_ui(self):
self.darea = Gtk.DrawingArea()
self.darea.connect("draw", self.on_draw)
self.darea.set_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self.add(self.darea)
self.set_title("Fill & stroke")
self.resize(230, 150)
self.set_position(Gtk.WindowPosition.CENTER)
self.connect("delete-event", Gtk.main_quit)
self.darea.connect("button-press-event", self.on_button_press)
self.coords = []
self.show_all()
def on_draw(self, wid, cr):
cr.set_source_rgb(0.6, 0.6, 0.6)
cr.arc(self.coords[0], self.coords[1], 40, 0, 2*math.pi)
cr.fill()
def on_button_press(self, w, e):
if e.type == Gdk.EventType.BUTTON_PRESS \
and e.button == MouseButtons.LEFT_BUTTON:
self.coords = [e.x, e.y]
self.darea.queue_draw()
def main():
app = Example()
Gtk.main()
if __name__ == "__main__":
main()
在此示例中,每次单击绘图区域时,都会绘制一个圆圈。我想画一个新的圆圈,但不要重画上一个圆圈。有可能吗?
我建议您调整绘图区域的思维模型;不要认为每次调用绘图处理程序时都会将其“清除”。而是这样想:每次需要从头重新绘制绘图区域时,都会调用绘图处理程序(其他原因:因为其他窗口移到了它的前面,或者因为您的程序要求绘图更新)。一旦绘制,绘图区域的内容就不会保留在任何地方。
如果需要持久的窗口内容,则应使用后备存储并将其绘制在绘图处理程序中的屏幕上,或者,如果要将现有的绘制对象视为独立存在,则可以使用画布库。