我想在鼠标位置画一些点(点击鼠标)
我在开罗使用 Python
我已经编写了一些代码,但效果不佳(单击按钮时我看到了要点)。
我使用 Glade 制作了 GUI,这也在问题的末尾链接。
import os,sys,platform
from os.path import join
import cairo
import gi
class Interface:
__gtype_name__ = "MonNom"
def __init__(self):
file_path = os.path.join('Interface.glade')
interface = Gtk.Builder()
interface.add_from_file(file_path)
self.double_buffer = None
interface.connect_signals(self)
window = interface.get_object("window")
window.show_all()
def on_draw(self, widget, cr):
if self.double_buffer is not None:
cr.set_source_surface(self.double_buffer, 0, 0)
cr.paint()
else:
print('Invalid double buffer')
return False
def on_configure(self, widget, event, data=None):
if self.double_buffer is not None:
self.double_buffer.finish()
self.double_buffer = None
self.double_buffer = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600,400)
db = self.double_buffer
cc = cairo.Context(db)
cc.set_source_rgb(0,0, 0)
cc.rectangle(0, 0, 600, 400)
cc.set_source_rgb(1, 0, 0)
cc.stroke()
db.flush()
return False
def on_da_button_press(self, widget, event):
print ("Mouse clicked... at ", event.x, ", ", event.y)
# self.widget.queue_draw()
db = self.double_buffer
if db is not None:
cc = cairo.Context(db)
cc.move_to(event.x-5,event.y)
cc.line_to(event.x+5,event.y)
cc.move_to(event.x, event.y-5)
cc.line_to(event.x, event.y+5)
cc.set_source_rgb(0, 1, 0)
cc.stroke()
# db.flush()
return True
def on_destroy(self, widget):
Gtk.main_quit()
if __name__ == "__main__":
app = Interface()
Gtk.main()