如何使用 Lua 和 IUP 显示进度条

发布于 2024-09-17 08:10:40 字数 707 浏览 8 评论 0原文

我构建了一个长时间运行的脚本,其中添加了一个进度条,代码如下:

function StartProgressBar()
   gaugeProgress = iup.gaugeProgress{}
   gaugeProgress.show_text = "YES"
   gaugeProgress.expand = "HORIZONTAL"
   dlgProgress = iup.dialog{gaugeProgress; title = "Note Replacement in Progress"}
   dlgProgress.size = "QUARTERxEIGHTH"
   dlgProgress.menubox = "NO"  --  Remove Windows close button and menu.
   dlgProgress:showxy(iup.CENTER, iup.CENTER)  --  Put up Progress Display 
   return dlgProgress
end

这是在循环之前调用的,进度条在循环期间更新(我没有调用 MainLoop)。在该过程结束时,我调用 dlgProgress.destroy 来清除它。

只要我不从进度条上获取焦点,它就可以正常工作,但是如果焦点丢失,程序就会崩溃,所以我确信我这样做的方式是错误的。谁能告诉我正确的方法。详细的google没有给我找到任何iup、lua进度条的例子。

先感谢您。

I have built a long running script to which I have added a progress bar with the following code:

function StartProgressBar()
   gaugeProgress = iup.gaugeProgress{}
   gaugeProgress.show_text = "YES"
   gaugeProgress.expand = "HORIZONTAL"
   dlgProgress = iup.dialog{gaugeProgress; title = "Note Replacement in Progress"}
   dlgProgress.size = "QUARTERxEIGHTH"
   dlgProgress.menubox = "NO"  --  Remove Windows close button and menu.
   dlgProgress:showxy(iup.CENTER, iup.CENTER)  --  Put up Progress Display 
   return dlgProgress
end

This is called before the loop and the progress bar updated during the loop (I am not calling MainLoop). At the end of the process I call dlgProgress.destroy to clear it.

As long as I don't take focus from the progress bar it works fine, but if focus is lost the program crashes, so I am sure I am doing this the wrong way. Can any one tell me the correct way. A detailed google did not find me any examples for iup, lua progress bars.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2024-09-24 08:10:40

这是一个工作示例。

require "iuplua"

local cancelflag
local gaugeProgress

local function StartProgressBar()
    cancelbutton = iup.button {
        title = "Cancel",
        action=function()
            cancelflag = true
            return iup.CLOSE
        end
    }
    gaugeProgress = iup.progressbar{ expand="HORIZONTAL" }
    dlgProgress = iup.dialog{
        title = "Note Replacement in Progress",
        dialogframe = "YES", border = "YES",
        iup.vbox {
            gaugeProgress,
            cancelbutton,
    }
    }
    dlgProgress.size = "QUARTERxEIGHTH"
    dlgProgress.menubox = "NO"  --  Remove Windows close button and menu.
    dlgProgress.close_cb = cancelbutton.action
    dlgProgress:showxy(iup.CENTER, iup.CENTER)  --  Put up Progress Display
    return dlgProgress
end


dlg = StartProgressBar()
gaugeProgress.value = 0.0

for i=0,10000 do
    -- take one step in a long calculation
    -- update progress in some meaningful way
    gaugeProgress.value = i / 10000
    -- allow the dialog to process any messages
    iup.LoopStep()
    -- notice the user wanting to cancel and do something meaningful
    if cancelflag then break end
end

-- distinguish canceled from finished by inspecting the flag
print("cancled:",cancelflag)

我在这里使用了 IUP 3.0 及其标准 Lua 绑定。仪表控件在 IUP 3.0 中被命名为 iup.progressbar,在早期版本中被命名为 iup.gauge。在早期版本中,它也可能位于扩展控件库中。

我已经在 Windows 上对此进行了测试。人们认为它在其他平台上也有类似的行为,但你的里程可能会有所不同。

Here is a working sample.

require "iuplua"

local cancelflag
local gaugeProgress

local function StartProgressBar()
    cancelbutton = iup.button {
        title = "Cancel",
        action=function()
            cancelflag = true
            return iup.CLOSE
        end
    }
    gaugeProgress = iup.progressbar{ expand="HORIZONTAL" }
    dlgProgress = iup.dialog{
        title = "Note Replacement in Progress",
        dialogframe = "YES", border = "YES",
        iup.vbox {
            gaugeProgress,
            cancelbutton,
    }
    }
    dlgProgress.size = "QUARTERxEIGHTH"
    dlgProgress.menubox = "NO"  --  Remove Windows close button and menu.
    dlgProgress.close_cb = cancelbutton.action
    dlgProgress:showxy(iup.CENTER, iup.CENTER)  --  Put up Progress Display
    return dlgProgress
end


dlg = StartProgressBar()
gaugeProgress.value = 0.0

for i=0,10000 do
    -- take one step in a long calculation
    -- update progress in some meaningful way
    gaugeProgress.value = i / 10000
    -- allow the dialog to process any messages
    iup.LoopStep()
    -- notice the user wanting to cancel and do something meaningful
    if cancelflag then break end
end

-- distinguish canceled from finished by inspecting the flag
print("cancled:",cancelflag)

I've used IUP 3.0, and its standard Lua binding here. The gauge control is named iup.progressbar in IUP 3.0, and was named iup.gauge in earlier versions. In earlier versions it may have been in the extended controls library as well.

I've tested this on Windows. One assumes it has similar behavior on other platforms, but your mileage may vary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文