Search code examples
pythonpygtkpycairo

Text and lines in pycairo


I'm having a little bit of trouble with pycairo. I want to draw a chord chart, but for some reason I don't quite understand it only displays the text, not the lines it should be drawing.

I use pygtk (3.0) and pycairo. Here's the result of what this code draws

enter image description here

Here's the code:

    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()

Thanks in advance to anyone who can help me.


Solution

  • The problem with the code is that it "draws" a line to the same coordinate, so it becomes a point which has no 2d properties, and that's why it doesn't draw anything. The corrected code:

    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()