如何将声明为vbox/hbox的数组显示到主窗口?

我正在创建一个函数,该函数在执行 certain function 之后应该将元素显示到IUP for Lua的主窗口中。

问题在于,每当我运行没有vbox/hbox数组的函数时,程序正常显示GUI元素(在这个测试案例中是文本框)。我还通过在新窗口上显示此内容进行了测试,这也起作用。这是我使用的正常代码:

function wa()
    local txt = {}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(hrbox, txt[1])
    iup.Map(txt[1])
    iup.Refresh(hrbox)
end

但是,当我通过将数组变量声明为hbox/vbox来运行代码时,突然间程序就不会将元素显示到主窗口中,也不会在新窗口中显示:

function wa()
    local txt = {}
    local a = {}
    a[1] = iup.vbox{}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(a[1], txt[1])
    iup.Map(txt[1])
    iup.Refresh(a[1])
    iup.Append(hrbox, a[1])
    iup.Refresh(hrbox)
end

然后最奇怪的事情是,当我将hrbox(我用来将元素显示到主窗口中的箱子)变成一个完全独立的变量时,它就不显示在主窗口中,但它可以在新窗口中显示。这之后的代码如下所示:

function wa()
    local txt = {}
    hrbox = iup.hbox{}
    a = {}
    a[1] = iup.vbox{}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(a[1], txt[1])
    iup.Map(txt[1])
    iup.Refresh(a[1])
    iup.Append(hrbox, a[1])
    iup.Refresh(hrbox)
end

我该如何使声明为hbox/vbox元素的数组变量起作用,以便可以将其显示到主窗口中?我不知道该怎么做,在研究了所有资料之后,我陷入了困境。

任何答案和建议都将不胜感激。

先感谢您!

点赞
用户10953006
用户10953006

我试图创建一个 最小复现示例

local iup = require("iuplua")

local Button = iup.button{TITLE="Add"}
local hrbox  = iup.vbox{Button}
local Frame  = iup.dialog{hrbox,SIZE="THIRDxTHIRD"}

function wa()
    local txt = {}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(hrbox, txt[1])
    iup.Map(txt[1])
    iup.Refresh(hrbox)
end

Button.action = function (Ih)
  wa()
end

Frame:show()
iup.MainLoop()

因此,基本上,你的代码正常工作。请确保在 wa 函数上方声明 hrbox

编辑:最终我理解了你的问题

你在这段代码中有一个问题:

function wa()
    local txt = {}
    local a = {}
    a[1] = iup.vbox{}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(a[1], txt[1])
    iup.Map(txt[1])
    iup.Refresh(a[1])
    iup.Append(hrbox, a[1])
    iup.Refresh(hrbox)
end

你实际上需要 map 新创建的 hbox。子项会同时自动映射。调用 refresh 一次就足够了。

local iup = require("iuplua")

local Button = iup.button{TITLE="Add"}
local hrbox  = iup.vbox{Button}
local Frame  = iup.dialog{hrbox,SIZE="THIRDxTHIRD"}

function wa()
    local txt = {}
    local a = {}
    a[1] = iup.vbox{}
    txt[1] = iup.text{
      multiline = "NO",
      padding = "5x5",
      size="100x15",
      scrollbar="Horizontal",
    }
    iup.Append(a[1], txt[1]) -- 将文本附加到新盒子
    iup.Append(hrbox, a[1])  -- 将新盒子附加到主盒子
    iup.Map(a[1])            -- 映射新盒子及其子项
    iup.Refresh(hrbox)       -- 重新计算布局
end

Button.action = function (Ih)
  wa()
end

Frame:show()
iup.MainLoop()
2021-04-19 01:31:41