如何在主窗口中显示声明为vbox/hbox的数组



我正在创建一个函数,该函数应该在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元素的数组变量工作,以便将其显示在主窗口中?我不知道该怎么做,在经历了所有的研究之后,我基本上陷入了困境。

如有任何答案和建议,我们将不胜感激。

提前感谢!

我试图创建一个最小的可复制示例:

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]) -- Append the text to the new-box
iup.Append(hrbox, a[1])  -- Append the new-box to the main box
iup.Map(a[1])            -- Map the new-box and its children
iup.Refresh(hrbox)       -- Re-calculate the layout
end
Button.action = function (Ih)
wa()
end
Frame:show()
iup.MainLoop()

最新更新