小编csr*_*ins的帖子

如何使用 pygi 在 Gtk.DrawingArea 中绘制鼠标单击

我正在使用 PyGI 编写一个小应用程序,它将在鼠标点击之间以交互方式在 Gtk.DrawingArea 上绘制一条线。但是我无法弄清楚如何在鼠标单击事件返回的坐标处使用 cairo 上下文进行绘制。

应该如何从“button-press-event”处理绘图以绘制类似于附加代码中“draw”回调产生的图案?

class MyApp(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Draw on button press")
        self.set_size_request(800, 500)
        self.connect('delete-event', Gtk.main_quit)

        self.drawing_area = Gtk.DrawingArea()
        self.drawing_area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) 
        self.drawing_area.connect('draw', self.on_drawing_area_draw)
        self.drawing_area.connect('button-press-event', self.on_drawing_area_button_press)
        self.drawing_area.show()
        self.add(self.drawing_area)

        self.show_all()

    def on_drawing_area_button_press(self, widget, event):
        print "Mouse clicked... at ", event.x, ", ", event.y
        # How to draw a line starting at this point on the drawing area?        
        return True

    def on_drawing_area_draw(self, drawing_area, cairo_context):
        cairo_context.move_to(50, 50)
        cairo_context.rel_line_to(0, 200)
        cairo_context.rel_line_to(200, 0)
        cairo_context.rel_line_to(0, -200)
        cairo_context.set_source_rgb(0, 0, 0)
        cairo_context.stroke()

        return False

app …
Run Code Online (Sandbox Code Playgroud)

python programming gtk application-development pygi

5
推荐指数
1
解决办法
5849
查看次数

标签 统计

application-development ×1

gtk ×1

programming ×1

pygi ×1

python ×1