mikshaw
Group: Members
Posts: 4856
Joined: July 2004 |
|
Posted: Jan. 05 2006,18:57 |
|
It took me almost 2 hours of error messages before coming up with something to solve my current problem. I was looking for a way to automatically draw multiple buttons according to a list of button labels. The drawing wasn't a big problem, but i couldn't seem to get the callback functions built into the loop. I needed a way to avoid having to type a new callback every time I added a string to the list. The answer was to create a global function and put "callback=<that function>" into the button properties. The function tests to see which button is being pressed, and acts accordingly.
This script builds a pack of buttons and labels them according to what is in the button_labels table. Add more items to the table, and you get more buttons. It draws a single column of buttons, but i'm sure the script could be tweaked to pack them into multiple columns. Also, the callback is limited...i was just looking for a way to do the same action on each item in the list.
Code Sample | ww = 360 wh = 300
button_labels = {"first button","second button","third button","the fourth"}
w = Window{ww,wh,"Lua FLTK Templates"} display = Output{140,10,ww-150,25}
buttons = Pack{10,10,120,wh-30}
function do_butt(self) for i = 1,getn(button_labels) do if self.label == button_labels[i] then display.value = "Button label: "..button_labels[i] break end end end
button = {} for i = 1,getn(button_labels) do button[i] = Button{25+i*10,25+i*10,25,25,button_labels[i];callback=do_butt} end buttons:end_layout()
w:show() |
-------------- http://www.tldp.org/LDP/intro-linux/html/index.html
|