PyGTK 3 (gi.repository) PangoCairo 在绘图文本和线条之间改变颜色
PyGTK 3 (gi.repository) PangoCairo changing color between drawing text and line
我在 PangoCairo
DrawingArea
中更改文本和线条之间的颜色时遇到问题。它们的颜色相同。
这是简单的 Python 代码:
from gi.repository import Gtk, Pango, PangoCairo
class Bug(Gtk.DrawingArea):
def __init__ (self):
Gtk.DrawingArea.__init__(self)
def do_draw_cb(self, widget, cr):
cr.translate ( 10, 10)
layout = PangoCairo.create_layout (cr)
desc = Pango.font_description_from_string ("Sans 14")
layout.set_font_description( desc)
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0)
layout.set_text("It is not easy being green", -1 )
cr.move_to(40, 20)
cr.line_to(70, 20)
cr.set_source_rgba(0.0, 0.0, 0.0, 1.0) # messes up previous set_text
cr.stroke()
PangoCairo.show_layout (cr, layout)
def destroy(window):
Gtk.main_quit()
window = Gtk.Window()
window.set_title ("Green?")
app = Bug()
app.set_size_request (300, 200)
window.add(app)
app.connect('draw', app.do_draw_cb)
window.connect_after('destroy', destroy)
window.show_all()
Gtk.main()
如果我删除第二个 set_source_rgba
,那么文本和线条都是绿色而不是黑色。但我想要的是绿色的文字和黑色的线条。我该怎么做呢?
对于布局,调用 show_layout
时会读取颜色,因此第一个 set_source_rgba
没有效果。您应该在设置颜色后立即显示布局。
def do_draw_cb(self, widget, cr):
cr.translate ( 10, 10)
layout = PangoCairo.create_layout (cr)
desc = Pango.font_description_from_string ("Sans 14")
layout.set_font_description( desc)
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0)
layout.set_text("It is not easy being green", -1 )
PangoCairo.show_layout (cr, layout)
cr.move_to(40, 20)
cr.line_to(70, 20)
cr.set_source_rgba(0.0, 0.0, 0.0, 1.0) # messes up previous set_text
cr.stroke()
我在 PangoCairo
DrawingArea
中更改文本和线条之间的颜色时遇到问题。它们的颜色相同。
这是简单的 Python 代码:
from gi.repository import Gtk, Pango, PangoCairo
class Bug(Gtk.DrawingArea):
def __init__ (self):
Gtk.DrawingArea.__init__(self)
def do_draw_cb(self, widget, cr):
cr.translate ( 10, 10)
layout = PangoCairo.create_layout (cr)
desc = Pango.font_description_from_string ("Sans 14")
layout.set_font_description( desc)
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0)
layout.set_text("It is not easy being green", -1 )
cr.move_to(40, 20)
cr.line_to(70, 20)
cr.set_source_rgba(0.0, 0.0, 0.0, 1.0) # messes up previous set_text
cr.stroke()
PangoCairo.show_layout (cr, layout)
def destroy(window):
Gtk.main_quit()
window = Gtk.Window()
window.set_title ("Green?")
app = Bug()
app.set_size_request (300, 200)
window.add(app)
app.connect('draw', app.do_draw_cb)
window.connect_after('destroy', destroy)
window.show_all()
Gtk.main()
如果我删除第二个 set_source_rgba
,那么文本和线条都是绿色而不是黑色。但我想要的是绿色的文字和黑色的线条。我该怎么做呢?
对于布局,调用 show_layout
时会读取颜色,因此第一个 set_source_rgba
没有效果。您应该在设置颜色后立即显示布局。
def do_draw_cb(self, widget, cr):
cr.translate ( 10, 10)
layout = PangoCairo.create_layout (cr)
desc = Pango.font_description_from_string ("Sans 14")
layout.set_font_description( desc)
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0)
layout.set_text("It is not easy being green", -1 )
PangoCairo.show_layout (cr, layout)
cr.move_to(40, 20)
cr.line_to(70, 20)
cr.set_source_rgba(0.0, 0.0, 0.0, 1.0) # messes up previous set_text
cr.stroke()