Opa galerinha, refiz a calculadora agora, só pra ver o quanto eu melhorei.
Ai vai o código:
require("wx")
MeuPalco = wx.wxFrame(wx.NULL, wx.wxID_ANY, "Calculadora WxLua", wx.wxDefaultPosition, wx.wxSize(200,250), wx.wxDEFAULT_FRAME_STYLE + wx.wxRESIZE_BOX + wx.wxRESIZE_BORDER + wx.wxMAXIMIZE_BOX)
ButtonWx_table = {}
OperationsWx = {"=", "+", "*", "/", "-", "^", "%", "."}
Operation_log = false
Number_log = 0
PositionWx = -1
for ID, Operation in ipairs(OperationsWx) do
PositionWx = ID % 5 == 1 and PositionWx +1 or PositionWx
table.insert(ButtonWx_table, wx.wxButton(MeuPalco, ID, Operation, wx.wxPoint((ID - PositionWx*5 -1)*30+15, 120 +PositionWx*30), wx.wxSize(30, 30)))
end
for i = 0, 9 do
local n = i >= 5 and i - 5 or i
table.insert(ButtonWx_table, wx.wxButton(MeuPalco, i+100, tostring(i), wx.wxPoint(n*30+15, i > 4 and 90 or 60), wx.wxSize(30, 30)))
end
TextWx1 = wx.wxTextCtrl(MeuPalco, wx.wxID_ANY, "", wx.wxPoint(15, 10), wx.wxSize(120, 30), wx.wxTE_READONLY)
ButtonWx1 = wx.wxButton(MeuPalco, wx.wxID_ANY, "C", wx.wxPoint(140, 10), wx.wxSize(30, 30), wx.wxTE_READONLY)
ButtonWx1:Connect(wx.wxID_ANY, wx.wxEVT_COMMAND_BUTTON_CLICKED, function() TextWx1:Clear() Number_log = 0 Operation_log = false end)
MeuPalco:Connect(wx.wxID_ANY, wx.wxEVT_MAXIMIZE, function() MeuPalco:SetSize(200, 250) end)
for _, Button in pairs(ButtonWx_table) do
if Button:GetId() >= 100 then
Button:Connect(wx.wxID_ANY, wx.wxEVT_COMMAND_BUTTON_CLICKED, function() TextWx1:WriteText(tostring(Button:GetId()-100)) end)
else
Button:Connect(wx.wxID_ANY, wx.wxEVT_COMMAND_BUTTON_CLICKED, function() Operate(OperationsWx[button:GetId()]) end)
end
end
function Operate(signal)
if signal == "." then
if TextWx1:GetValue() ~= "" and not string.find(TextWx1:GetValue(), "%.") then
TextWx1:WriteText(signal)
end
return true
elseif not Operation_log then
if TextWx1:GetValue() ~= "" then
if #TextWx1:GetValue() > 1 or string.sub(TextWx1:GetValue(), 1, 1) ~= "-" then
if signal ~= "=" then
Operation_log = signal
Number_log = TextWx1:GetValue()
TextWx1:Clear("")
end
end
else
if signal == "-" then
TextWx1:WriteText("-")
end
end
return true
elseif signal ~= "=" then
return true
end
loadstring("i = " .. Number_log .. Operation_log .. TextWx1:GetValue())()
TextWx1:Clear()
TextWx1:WriteText(tostring(i))
Operation_log = false
return true
end
MeuPalco:Show(true)
wx.wxGetApp():MainLoop()
Download
Scan
Imagem
É isso ai, valeu.