【问题标题】:Lua script for conky runs without errors but doesn't draw anything用于 conky 的 Lua 脚本运行没有错误,但不绘制任何内容
【发布时间】:2018-09-16 20:52:33
【问题描述】:

我是 lua 新手,并试图通过为 conky 创建脚本来深入了解它。在我的示例中,我试图将 cairo 功能封装到可以添加到画布的 Canvas 对象drawable 对象(即文本对象)中。

当我尝试将 cairo_surfacecairo 对象存储在表中时,我无法再使用它们了。即使没有发生错误(没有消息或段错误或泄漏)在第二个示例中没有显示任何文本。

此示例有效:

Canvas = {
    init = function (w)
        local cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height)
        local cr = cairo_create(cs)
        return cr, cs
    end,

    destroy = function (cr, cs)
        cairo_destroy(cr)
        cairo_surface_destroy(cs)
    end
}

function conky_main ()
    if conky_window == nil then
        return
    else
        local cr, cs = Canvas.init(conky_window)
        local tx = Text:new{text="Hello World!"}
        tx:draw(cr)
        Canvas.destroy(cr, cs)
    end
end

此示例不起作用:

Canvas = {
    init = function (w) -- returns table instead of 2 variables
        return {
            cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height),
            cr = cairo_create(cs)
        }
    end,

    destroy = function (cnv)
        cairo_destroy(cnv.cr)
        cairo_surface_destroy(cnv.cs)
    end
}

function conky_main ()
    if conky_window == nil then
        return
    else
        local cnv = Canvas.init(conky_window)
        local tx = Text:new{text="Hello World!"}
        tx:draw(cnv.cr) -- access table member instead of variable
        Canvas.destroy(cnv)
    end
end

【问题讨论】:

    标签: lua cairo conky


    【解决方案1】:
    return {
        cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height),
        cr = cairo_create(cs)
    }
    

    在 Lua 表构造函数中,无法访问正在构造的表的其他字段。
    表达式cr = cairo_create(cs) 中的cs 指的是(全局)变量cs,而不是表字段cs。 解决方法:引入局部变量cs,并在建表前对其进行初始化。

    local cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height)
    return { cs = cs, cr = cairo_create(cs) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多