This is how nUI handles clicking a button during combat ( it switches display panels linked to the button ), I only maintain the addon so not all elements of the addon are fully understandable to me but I have dabbled with secure frames to fix action button issues so some of this does make some element of sense to me now. Hopefully, reading through it will give you something to help you with your own button.
Lua Code:
local frame = CreateFrame( "Frame", "nUI_InfoPanelSelector", background, "SecureFrameTemplate" );
frame.button = CreateFrame( "Button", "$parent_Button", frame, "SecureHandlerClickTemplate" );
frame.button:SetAttribute(
"_onclick",
[[
if not IsAltKeyDown() or not IsControlKeyDown() or IsShiftKeyDown() then
if button == "LeftButton" then
if CurrentPanel == #PanelList then
CurrentPanel = 1;
else
CurrentPanel = CurrentPanel+1;
end
elseif button == "RightButton" then
if CurrentPanel == 1 then
CurrentPanel = #PanelList;
else
CurrentPanel = CurrentPanel-1;
end
end
for i=1,#PanelList do
local frame = self:GetFrameRef( PanelList[i] );
if i == CurrentPanel then
frame:Show();
else
frame:Hide();
end
end
end
]]
);
frame.button:SetAllPoints( frame );
frame:SetAttribute( "addchild", frame.button );
Elsewhere in the code the panel list is also setup in secure mode
Lua Code:
for item in pairs( rotation ) do
local name = rotation[item].name;
frame.button:SetAttribute( "nUI_InfoPanel"..item, name );
frame.button:SetFrameRef( name, rotation[item].frame );
end
frame.button:Execute(
[[
local i = 1;
PanelList = newtable();
while true do
local panel_name = self:GetAttribute( "nUI_InfoPanel"..i );
if not panel_name then
break;
end
PanelList[i] = panel_name;
i = i+1;
end
CurrentPanel = self:GetAttribute( "state" ) or 1;
]]
);