pycairo中的文本和行

时间:2016-12-03 00:06:45

标签: python pygtk pycairo

我和pycairo有点麻烦。我想绘制一个chord chart,但由于某种原因我不太明白它只显示文本,而不是它应该绘制的线条。

我使用pygtk(3.0)和pycairo。 Here's the result of what this code draws

enter image description here

以下是代码:

    def gen_chart(self, wid, cr):

            x = 10
            y = 60

            cr.set_source_rgb(0, 0, 0)
            cr.set_line_width(1)
            cr.select_font_face("Open Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
            cr.set_font_size(40)

            cr.move_to(x, y)
            cr.line_to(x, y)

            counter = 1
            for measure in chord_list: # The list is declared earlier in the source                    
                    x += 10
                    for chord in measure:
                            cr.move_to(x, y)
                            cr.show_text(chord)
                            x += 100
                    if counter % 4 == 0 or counter == 4:
                            x = 10
                            y += 60
                            cr.move_to(x, y)
                            cr.line_to(x, y)
                            counter += 1
                    counter += 1

            cr.move_to(x, y + 10)
            cr.move_to(x, y + 10)
            cr.stroke()

提前感谢任何可以帮助我的人。

1 个答案:

答案 0 :(得分:0)

代码的问题在于它" draw"一条线到同一个坐标,所以它变成了一个没有2d属性的点,这就是为什么它没有绘制任何东西。更正后的代码:

def gen_chart(self, wid, cr):

    x = 10
    y = 60

    cr.set_source_rgb(0, 0, 0)
    cr.set_line_width(1)
    cr.select_font_face("Open Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
    cr.set_font_size(40)

    cr.move_to(x, y)
    cr.line_to(x, y)

    counter = 1
    for measure in chord_list:                   
        x += 10
        for chord in measure:
            cr.move_to(x, y)
            cr.show_text(chord)
            x += 100
        if counter % 4 == 0 or counter == 4:
            x = 10
            y += 60
            cr.move_to(x, y)
            cr.line_to(x, y + 10)
        counter += 1
    counter += 1

cr.move_to(x, y + 80)
cr.stroke()