学习LUA,使用IUP进行GUI程序设计

2023-05-16

     原创 visualfc


     在LUA中进行GUI程序设计,可以选择的GUI库一般有wxLua和IupLua。wxLua具备典型的面向对象风格,功能相对强大。而IUP的LUA绑定则非常简洁易用。本文主要介绍IupLua。
    IUPLUA目前稳定版本为2.7.1,最新版本为3.0beta1。
    IUP项目主页为:http://sourceforge.net/projects/iup
    在IupLua程序设计中,主要使用表来设计应用程序界面,表的键值则为GUI部件的属性,通过回调函数完成窗口消息的获取。下面给出一个完整的例子。

 1 require "iuplua"
 2
 3 text_location = iup.text{expand="HORIZONTAL", id="text_location"}
 4 btn_browse = iup.button{title="Browse", rastersize="x22",id="btn_browse"}
 5 dlg = iup.dialog
 6 {
 7     iup.vbox
 8     {
 9         iup.label{title="Location:"},
10         iup.hbox
11         {
12             text_location,
13             btn_browse
14             ; margin="0x0"
15         },
16         iup.label{title="Text:"},
17         iup.multiline{expand="YES"},
18     }
19     ;title="iuplua sample", size="200x100", margin="10x10"
20 }
21
22 function btn_browse:action()
23     local dlg = iup.filedlg{dialogtype="DIR"}
24     dlg:popup()
25     if dlg.status == "0" then
26         text_location.value = dlg.value
27     end
28 end
29
30 dlg:show()
31
32 iup.MainLoop()
33

上面的例子通过hbox和vbox进行界面的自动调整。程序很简单,如下图所示:

其中22行的fuctnion btn_browse:action即为回调函数,相比wxLua的Bind方法,这种用法更为简洁。
     另外我们也可以看到在此程序中我们需要对text_location和btn_browse进行声明,而后在dialog的box中进行引用,我们也可以使用类似HTML控制中的ID值来进行设计而无需事先声明。如下:

 1 require "iuplua"
 2 require "iupid"
 3
 4 dlg = iup.dialog
 5 {
 6     iup.vbox
 7     {
 8         iup.label{title="Location:", id="ok"},
 9         iup.hbox
10         {
11             iup.text{expand="HORIZONTAL", id="text_location"},
12             iup.button{title="Browse", rastersize="x22", id="btn_browse"}
13             ; margin="0x0"
14         },
15         iup.label{title="Text:"},
16         iup.multiline{expand="YES"},
17     }
18     ;title="iuplua sample", size="200x100", margin="10x10"
19 }
20
21 function btn_browse:action()
22     local dlg = iup.filedlg{dialogtype="DIR"}
23     dlg:popup()
24     if dlg.status == "0" then
25         text_location.value = dlg.value
26     end
27 end
28
29 dlg:show()
30
31 iup.MainLoop()
32

如上所示,使用id值的方式GUI设计代码显得更为一致。那么如何做到这一点呢,在LUA中实现起来很简单,使用upvalue就可以做到。

 1 function iup_id(func)
 2     return function(o)
 3         if o.id ~= nil then
 4             _G[o.id] = func(o)
 5             return _G[o.id]
 6         else
 7             return func(o)
 8         end
 9     end
10 end

然后对所需部件进行声明。

--standard
iup.button = iup_id(iup.button)
iup.text = iup_id(iup.text)

这样就可以使用id的方式来直接引用GUI部件了,另外需要注意的是各个GUI部件要取不同的id值。
下面给出了我写的iupcd.lua的完整源代码以供参考。

require "iuplua"
require "iupluacontrols"


function iup_id(func)
    return function(o)
        if o.id ~= nil then
            _G[o.id] = func(o)
            return _G[o.id]
        elseif o.ID ~= nil then
            _G[o.ID] = func(o)
            return _G[o.ID]
        else
            return func(o)
        end
    end
end

--standard
iup.button = iup_id(iup.button)
iup.canvas = iup_id(iup.canvas)
iup.frame = iup_id(iup.frame)
iup.multiline = iup_id(iup.multiline)
iup.progressbar = iup_id(iup.progressbar)
iup.spin = iup_id(iup.spin)
iup.tabs = iup_id(iup.tabs)
iup.val = iup_id(iup.val)
iup.toggle = iup_id(iup.toggle)
iup.radio = iup_id(iup.radio)
iup.text = iup_id(iup.text)
iup.list = iup_id(iup.list)
iup.label = iup_id(iup.label)
--dialog
iup.dialog = iup_id(iup.dialog)
iup.filedlg = iup_id(iup.filedlg)
iup.messagedlg = iup_id(iup.messagedlg)
iup.colordlg = iup_id(iup.colordlg)
iup.fontdlg = iup_id(iup.fontdlg)
iup.alarm = iup_id(iup.alarm)
iup.getfile = iup_id(iup.getfile)
iup.gettext = iup_id(iup.gettext)
iup.listdialog = iup_id(iup.listdialog)
iup.message = iup_id(iup.message)
iup.scanf = iup_id(iup.scanf)
iup.getcolor = iup_id(iup.getcolor)
iup.getparam = iup_id(iup.getparam)
--layout
iup.fill = iup_id(iup.fill)
iup.vbox = iup_id(iup.vbox)
iup.hbox = iup_id(iup.hbox)
iup.zbox = iup_id(iup.zbox)
iup.cbox = iup_id(iup.cbox)
iup.sbox = iup_id(iup.cbox)
--additional
iup.cells = iup_id(iup.cells)
iup.colorbar = iup_id(iup.colorbar)
iup.colorbrowser = iup_id(iup.colorbrowser)
iup.dial = iup_id(iup.dial)
iup.gauge = iup_id(iup.gauge)
iup.tabs = iup_id(iup.tabs)
iup.matrix = iup_id(iup.matrix)
iup.tree = iup_id(iup.tree)
iup.glcanvas = iup_id(iup.glcanvas)
iup.pplot = iup_id(iup.pplot)
iup.olecontrol = iup_id(iup.olecontrol)
iup.speech = iup_id(iup.speech)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

学习LUA,使用IUP进行GUI程序设计 的相关文章

随机推荐

  • 调试llvm时出现collect2: fatal error: ld terminated with signal 9

    安装llvm debug版本 release版本的安装过程可以参考https blog csdn net vincentuva article details 82993563 在安装使用debug版本时 xff0c 只需要进入到build
  • 地址栏中的#是什么意思

    我们在开发vue时 xff0c 地址栏中会出现 xff0c 如下图所示 xff1a 当我们点击跳转A页面时 xff1a 那么地址栏中的 到底是什么意思 xff1f 如何去掉 xff1f 路由的两种显示模式 Hash模式 这个模式下地址栏中包
  • Anaconda静默安装

    我们默认安装Anaconda时 xff0c 需要进行交互才可以完成安装 xff0c 例如需要输入yes xff0c 或者回车 有时候我们只需要他进行默认安装即可 xff0c 不需要进行交互 命令如下 xff1a span class tok
  • 使用godoc创建可以本地浏览的go文档站点

    Golang 的官网地址是golang org xff0c 有的时候国内打不开 现在国内的可以使用 xff08 golang google cn xff09 其实 xff0c 如果已经安装好了 go xff0c 可以在本地直接查看go 文档
  • 慎用 dpkg --force-all 安装 linuxqq

    最近与一个朋友联络 xff0c 要用 QQ 去 xff31 xff31 官网上只有比较早的版本 xff0c 安装后 xff0c 还提示必须更新 xff0c 点击还找不到下载 xff0c 悲了个催 后面幸好有 web qq 暂时解决了这个问题
  • 又开了一个BLOG,发个贴纪念一下

    很久没写技术方面的BLOG了 xff0c 最近因为查询资料看了很多比人的BLOG 感觉心里有些痒痒的 xff0c 所以决定再次开一个BLOG xff0c 本人是搞嵌入式开发的 xff0c 对各种通讯终端 xff0c CPU以及LINUX比较
  • How to make linux boot from network

    1 Enable dhcp server on a linux server the etc dhcpd conf should be looked like this ddns update style interim ignore cl
  • 转回CSDN了

    在BLOGSPOT上面挣扎了几个月 xff0c 还是放弃了 xff0c 毕竟看我的BLOG绝大多数肯定是中国人 xff0c 一个老被中国政府封的BLOG是没办法让别人接受的 xff0c 哎 xff0c 还是对BLOGSPOT恋恋不舍阿 xf
  • 最近在研究Mythtv

    mythtv是一个linux下的开源电视节目观看 录制和管理的软件 xff0c 如果装在普通PC上就可以变成一台电视PC xff0c 可以使用遥控器来看电视 xff0c 录节目 xff0c 如果装在一个嵌入式系统里就是一个机顶盒啦 xff0
  • arm linux 内核移植及驱动调试-网卡(1)

    最近在给一块ARM开发板 扬创的2440 移植新的kernel xff0c 原来的驱动都在 xff0c 不过还是碰到不少问题 xff0c 主要是对ARM LINUX的一些结构不甚了解 xff0c 这里作个笔记以便自己或他人查阅 前面没有什么
  • UBUNTU 下的IE6搞定

    可以上网上银行 支付宝 就是有点卡 不知道为什么 另外还有些小bug 不过我已经非常满意了 先装好wine 在ubuntu下面就是 sudo apt get install wine cabextract 然后直接下载安装ie4linux就
  • UBUNTU 下编译POKY

    记录一下我在UBUNTU下编译POKY的一些TIPS xff0c 防止以后再编的时候忘掉 xff0c 又要重新GOOGLE 1 解开pinky的包 2 进入pinky目录输入 source poky init build env 3 修改b
  • LLVM编译collect2: fatal error: ld terminated with signal 9

    报错 xff1a collect2 fatal error ld terminated with signal 9 查了一下这个报错 xff0c 可能是内存不足 xff0c 看到有前人的解决方法 xff0c 创建了20G的交换空间 xff1
  • Rust安装与编写第一个rust程序

    Rust 是 Mozilla 开发的注重安全 性能和并发性的编程语言 下边来演示一下如何安装rust xff0c 并尝试创建第一个rust项目 使用 rustup 脚本安装 xff1a 第一步 xff1a 执行 curl https sh
  • (C语言)栈应用简易计算器实现

    利用栈实现简易计算器 进行包含 43 间的计算 include lt stdio h gt include lt string h gt define MaxSize 100 typedef struct CharStack 字符栈 cha
  • 【实战原创】Centos7下Samba服务器配置(实战)

    这篇文章主要介绍了 Centos7 下 Samba 服务器配置 xff08 实战 xff09 xff0c 文中通过示例代码介绍的非常详细 xff0c 对大家的学习或者工作具有一定的参考学习价值 xff0c 需要的朋友们下面随着小编来一起学习
  • 批量修改word中的公式--mathtype

    1 用word打开一个需要编辑公式的文档 2 编辑公式的格式 xff0c 首先 xff0c 双击一个公式 xff0c 打开数学公式编辑器MathType xff0c 进入编辑状态 xff0c 点击大小菜单中的自定义选项 xff0c 修改字号
  • 免费使用office365和5TOneDrive空间

    免费使用office365和5TOneDrive空间 免费使用office365和OneDrive空间 免费使用office365和OneDrive空间 个人版本的office账号只可以拥有5G的存储空间 xff08 可以通过邀请的方式免费
  • UBUNTU 22.04 使用 SUNSHINE 和 MOONLIGHT 进行串流

    参考 ubuntu22 04 sunshine安装使用总结 xff0c 远程游戏 哔哩哔哩 bilibili sunshine README md at master loki 47 6F 64 sunshine GitHub GitHub
  • 学习LUA,使用IUP进行GUI程序设计

    原创 visualfc 在LUA中进行GUI程序设计 xff0c 可以选择的GUI库一般有wxLua和IupLua wxLua具备典型的面向对象风格 xff0c 功能相对强大 而IUP的LUA绑定则非常简洁易用 本文主要介绍IupLua I