2016-12-03 28 views
1

我對pycairo有點麻煩。我想繪製一個chord chart,但出於某種原因,我不太明白它只顯示文本,而不是它應該繪製的線條。pycairo中的文本和行

我使用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

好吧,所以我在回覆我自己以防萬一有人跑過去。我開始要求開羅在同一點上畫一條線,所以它什麼也沒畫。每個move_to下的line_to應該將y軸移動所需的像素數量。 –

+0

而不是評論你的自我可以創建一個答案並將其標記爲正確的。以及在答案中包含結果圖像中的更正代碼。 – Sam

回答

0

代碼的問題在於它「繪製」了一條直線到同一個座標,所以它變成了一個沒有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()