【问题标题】:Text and lines in pycairopycairo 中的文本和线条
【发布时间】:2016-12-03 00:06:45
【问题描述】:

我在使用 pycairo 时遇到了一点麻烦。我想画一个chord chart,但由于某种原因我不太明白它只显示文本,而不是它应该绘制的线条。

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

代码如下:

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

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

【问题讨论】:

  • 好的,所以我正在回复自己,以防有人遇到同样的情况。我要求 cairo 在同一点画一条线,所以它什么也没画。每个 move_to 下的 line_to 应将 y 轴移动所需的像素数量。
  • 可以创建一个答案并将其标记为正确,而不是对自己发表评论。以及在答案中包含更正的代码以及结果图像。

标签: python pygtk pycairo


【解决方案1】:

代码的问题在于它“绘制”了一条线到相同的坐标,所以它变成了一个没有二维属性的点,这就是它不绘制任何东西的原因。更正后的代码:

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

【讨论】:

    猜你喜欢
    • 2013-01-22
    • 2017-02-17
    • 2010-09-10
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多