How can I make an GUI Application in Lua

后端 未结 5 2018
广开言路
广开言路 2021-02-04 09:53

First I\'ll show you an example of what I am talking about: GUI Example

I\'ve been studying Lua for around a week now, and I\'m really curious of how I would do this. Ba

5条回答
  •  情歌与酒
    2021-02-04 10:12

    If you are an absolute beginner, i.e. you don't have any programming experience in other programming languages, I advice you to learn Lua very well without trying to mess with GUI programming, which is inherently much harder. When you will have a good understanding of Lua, then go for a GUI toolkit for Lua. I use wxLua so I can only give you some hints on that.

    Since it is not a "native" Lua toolkit, but it is a "binding" to a well-known cross-platform GUI library (wxWidgets) you must study both the wxLua documentation and wxWidgets manual (at least to some degree).

    wxLua binary distribution comes with everything needed to use it (you don't even need a separate Lua interpreter, it has its own) and contains a good number of example applications.

    The following script is a trivial approximation of what you want to do, but (I repeat myself) you should really learn the basics of Lua before attempting GUI programming.

    local wx = require 'wx'
    
    local PATH_TO_APPLICATION = [[notepad.exe]]     -- Windows assumed for sake of exemplification
    
    local ans = wx.wxMessageBox( "Should the application be started?", "Hi there!",
        wx.wxOK + wx.wxCANCEL + wx.wxICON_QUESTION )
    if ans == wx.wxOK then
        wx.wxExecute( PATH_TO_APPLICATION )
    end
    

    To run the previous script you must be sure that wxLua is installed correctly in your interpreter search path. Otherwise you must use the wxlua.exe interpreter that comes with the distribution.

    Note also that wxLua interpreter (latest wxLua stable release) runs with a version of Lua 5.1, so try not to use features of Lua 5.2 in your scripts. Basic Lua syntax and semantics is almost the same, but there are some slight differences and Lua 5.2 has a couple of added features. So be careful with your learning path.

提交回复
热议问题