저는 pycairo를 사용하기 시작했습니다. 다음 흥미로운 오류가 발생했습니다. 필자가 작성한 프로그램은 간단한 gtk 창을 만들고 사각형에 그려 넣은 다음 모든 종류의 키보드 입력에 임의의 줄을 그리는 콜백을 갖고 있습니다. 그러나 각 키보드 입력에서 새로운 컨텍스트를 만들어야하거나, 프로그램이 첫 번째 키보드 입력 (특히 .stroke() 행)을 수신하는 순간 오류가 발생하는 것으로 보입니다. 오류가있는 경우 다음과 같습니다. 'BadDrawable (Pixmap 또는 Window 매개 변수가 잘못되었습니다)'. (세부 정보 : 시리얼 230 오류 코드 9 요청 코드 53 마이너 코드 0)카이로 문맥과 지속성?
#! /usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk, gobject, cairo, math, random
# Create a GTK+ widget on which we will draw using Cairo
class Screen(gtk.DrawingArea):
# Draw in response to an expose-event
__gsignals__ = { "expose-event": "override" }
# Handle the expose-event by drawing
def do_expose_event(self, event):
# Create the cairo context
self.cr = self.window.cairo_create()
# Restrict Cairo to the exposed area; avoid extra work
self.cr.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
self.cr.clip()
self.draw(*self.window.get_size())
def key_press_event(self, *args):
# print args
self.cr = self.window.cairo_create() # This is the line I have to add
# in order to make this function not throw the error. Note that cr is only
# given as attribute of self in order to stop it going out of scope when this line
# doesn't exist
self.cr.set_source_rgb(random.random(), random.random(), random.random())
self.cr.move_to(*[z/2.0 for z in self.window.get_size()])
self.cr.line_to(*[z*random.random() for z in self.window.get_size()])
self.cr.stroke()
def draw(self, width, height):
# Fill the background with gray
self.cr.set_source_rgb(.5,.5,.5)
self.cr.rectangle(0, 0, width,height)
self.cr.fill()
self.cr.set_source_rgb(1,0,0)
self.cr.arc(width/2.0, height/2.0, min(width,height)/2.0 - 20.0, 0.0, 2.0*math.pi)
self.cr.stroke()
#create a gtk window, attach to exit button, and whatever is passed as arg becomes the body of the window. AWESOME
def run(Widget):
window = gtk.Window()
widget = Widget()
window.connect("delete-event", gtk.main_quit)
window.connect('key-press-event',widget.key_press_event)
widget.show()
window.add(widget)
window.present()
gtk.main()
if __name__ == "__main__":
run(Screen)
당신의 도움에 감사드립니다!
(업데이트 : 나는 주위를 연주했다, 나는 깨달았다 다음 : 나는 창 크기를 조정할 때, 추가 된 모든 새로운 객체가 더 이상로 삭제 (또는 적어도 표시되지 않습니다)?)
http://wiki.github.com/tbaugis/hamster_experiments/에 관심이있을 수 있습니다. 카이로 위에서 매우 유용한 추상화를 제공합니다. 전체 라이브러리를 사용하지 않더라도 포함 된 트위터는 확실히 볼만한 가치가 있습니다. –