Programming and Scripting :: Lua Radio buttons



I am trying to get radio buttons to work in a short lua program:

Code Sample

selbc = fltk:Fl_Round_Button(130,20,18,25,"Breads_Cereals")
-- selbc:value(1)
selbc:type(fltk.FL_RADIO_BUTTON)
selbc:selection_color(fltk.FL_RED)

selmd = fltk:Fl_Round_Button(260,20,18,25,"Main Dishes")
selmd:type(fltk.FL_RADIO_BUTTON)
selmd:selection_color(fltk.FL_RED)

selveg = fltk:Fl_Round_Button(130,40,18,25,"Vegetables")
selveg:type(fltk.FL_RADIO_BUTTON)
selveg:selection_color(fltk.FL_RED)

selmisc = fltk:Fl_Round_Button(260,40,18,25,"Miscellaneous")
selmisc:type(fltk.FL_RADIO_BUTTON)
selmisc:selection_color(fltk.FL_RED)

-- setonly():void

if selmisc:value() == 1 then
foodcat = "Miscellaneous"
elseif selmd:value() == 1 then
foodcat = "Main Dishes"
elseif selveg:value() == 1 then
foodcat = "Vegetables"
elseif selbc:value() == 1 then
foodcat = "Breads_Cereals"
end


No matter what I click the value is never set unless I do it manually.  I tried to do it like the fdtool program but I am missing something.  Can somebody point me to a site that has some lua radio button code?

What is your enclosing container, group or window?
What gui is going to have the callback attached so you can display the results of the radio button selection.

We have 5 lua programs that use radio buttons, from simple to more complex for you to study.

icontool
mouse-config
rdesktop
wallpaper
fdtool

Something like this might help?

Code Sample

function catfood(self)
foodcat=self:label();
print(foodcat);
end

w=fltk.Fl_Window(300,100,"catfood");
selbc = fltk:Fl_Round_Button(30,20,18,25,"Breads_Cereals")
selbc:type(fltk.FL_RADIO_BUTTON); selbc:callback(catfood);
--
selmd = fltk:Fl_Round_Button(160,20,18,25,"Main Dishes")
selmd:type(fltk.FL_RADIO_BUTTON); selmd:callback(catfood);
--
selveg = fltk:Fl_Round_Button(30,40,18,25,"Vegetables")
selveg:type(fltk.FL_RADIO_BUTTON); selveg:callback(catfood);
--
selmisc = fltk:Fl_Round_Button(160,40,18,25,"Miscellaneous")
selmisc:type(fltk.FL_RADIO_BUTTON); selmisc:callback(catfood);

w:show();
Fl:run();


If you have multiple sets of radio buttons you'll probably need to create a unique group for each set of buttons.  Also, this example uses the label of each button to determine the value of foodcat

Thanks for the help.  I was really stuck.  The radio buttons work now.  That is a pretty neat trick to pull the labels from the buttons.  I don't know the lua programming language very well but I am trying to build a data entry form for a sqlite database.  As a starting point I took the window routines from the fdtool.lua program and changed a few things like button names and input fields.  If I can get it to produce the sql statement that I want I will post the code or make a dsl package.

One thing that I found strange was the function has to be before the radio button statements.

Apparently some languages don't require the function to be defined first, but  Lua seems to be one that does. I assume this is because Lua is interpreted line-by-line at runtime, so it needs to know what a callback does before it can use the callback. Maybe the function could first be declared without being fully defined, like in C, but I haven't really had any need or desire to experiment with that yet.

I know nothing about using sql databases yet, so I can't help there. So far my experience with saving and retreiving data has been in parsing plain text rc files, like many of the DSL tools do.

Next Page...
original here.