Python 使用Cairo在GTK DrawinArea上使用鼠标绘制
我想在鼠标位置画一些点(点击鼠标) 我在Cairo中使用Python 我已经写了一些代码,但效果不是很好(我在点击按钮时看到了要点) 我使用Glade制作了GUI,在问题的末尾也链接了GladePython 使用Cairo在GTK DrawinArea上使用鼠标绘制,python,gtk,cairo,drawingarea,Python,Gtk,Cairo,Drawingarea,我想在鼠标位置画一些点(点击鼠标) 我在Cairo中使用Python 我已经写了一些代码,但效果不是很好(我在点击按钮时看到了要点) 我使用Glade制作了GUI,在问题的末尾也链接了Glade import os,sys,platform from os.path import join import cairo import gi class Interface: __gtype_name__ = "MonNom" def
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()
无法添加HELLO!相关您好,我的问题是在绘图上,而不是用鼠标和事件。所有绘图都是在我点击界面上的按钮时完成的!解决:这是一个刷新问题。我将self.window.queue\u draw()添加到“on\u da\u button\u press”的末尾,并在def init(windows->self.window)中更改了“window”的定义