Get the button id at time event clicked ?

User avatar
AlbertoGZ
Posts: 37
Joined: Sat Apr 21, 2018 3:15 am
Location: Barcelona, Spain
Contact:

Get the button id at time event clicked ?

#1

Post by AlbertoGZ » Thu May 10, 2018 3:56 am

I have a lot of buttons all them with an id like btn1, btn2, btn3... My doubt is how I could to have access to each one of them on the event handler without to repeat one function for each button.

I put an example.

Code: Select all

default='blabla'
var1 = 'null'
var2 = 'null'
var3 = 'null'

# UI buttons 
ui.Button({ 'ID': 'btn1', 'Text': 'btn1' }),
ui.Button({ 'ID': 'btn2', 'Text': 'btn2' }),
ui.Button({ 'ID': 'btn3', 'Text': 'btn3' }),
#...
#...

def _func(ev):
    #actions to do...
    var1 = default
dlg.On.btn1.Clicked = _func

def _func(ev):
    #actions to do...
    var2 = default
dlg.On.btn2.Clicked = _func

def _func(ev):
    #actions to do...
    var3 = default
dlg.On.btn3.Clicked = _func

#...

#...
The 'actions to do' should do when each of these buttons are clicked are the same excepts changes a numerics ids for some vars inside.

I could manage this with lists, so also using a master function for common actions, but my question is about how I can avoid to have repeat onClicked event for each button.

I'm thinking in something like this:

Code: Select all

dlg.On.btn[i].Clicked = _func
Then get the id from button at press the button time. I'm not sure if this is possible.

User avatar
Midgardsormr
Fusionista
Posts: 743
Joined: Wed Nov 26, 2014 8:04 pm
Location: Los Angeles, CA, USA
Been thanked: 76 times
Contact:

Re: Get the button id at time event clicked ?

#2

Post by Midgardsormr » Wed May 16, 2018 5:55 pm

I've got some serious mad scientist cackling going on over here!

I needed to solve the exact same problem today. I can't help you with translating it to Python, but this works in Lua:

  1. local dynamicFunctions = {}
  2.  
  3. for i, button in pairs(buttons) do
  4.      dynamicFunctions[button.ID] = {}      -- Declares a table
  5.      dynamicFunctions[button.ID].Clicked = function (ev) print("Button clicked.") end
  6. end
  7.  
  8. for i, func in pairs(dynamicFunctions) do
  9.     versionWindow.On[i] = func
  10. end

User avatar
PeterLoveday
Fusioneer
Posts: 126
Joined: Sun Sep 14, 2014 6:09 pm
Been thanked: 10 times

Re: Get the button id at time event clicked ?

#3

Post by PeterLoveday » Wed May 16, 2018 7:08 pm

You can use a single function, if all you need is to differentiate which button it is, the information is in the event. You have a couple of options; ev.who will give you the ID of the button, or ev.sender will give you the UIButton object itself.

Dumping the event will often give you useful information for many different event types; but almost all of them will contain 'who', 'sender', 'when', 'what', and 'window'. Other fields may be present for specific event types.

  1. function clicked(ev)
  2.   print("Button clicked.")
  3.   dump(ev)
  4. end
  5.  
  6. for i, button in pairs(buttons) do
  7.     versionWindow.On[button.ID].Clicked = clicked
  8. end

User avatar
AlbertoGZ
Posts: 37
Joined: Sat Apr 21, 2018 3:15 am
Location: Barcelona, Spain
Contact:

Re: Get the button id at time event clicked ?

#4

Post by AlbertoGZ » Thu May 17, 2018 1:48 pm

Thank you for your reply guys!

More or less I understand the idea, I think.
I will attempt coding this example in python.
By the moment I have already the ID's of buttons in a dict using it for other function. Dumping this data from event.. I need research a bit.