Jump to content

Recommended Posts

Posted

I am trying to create a calculator with lua as scriptengine. It displays the current version of the gui fine, but I am having problems with the accept loop at the end. At the moment I have only entered a little code for number 1 and 2. The problem is that the textbox shows the number 1 and 2 when I hover over the buttons and not when they are clicked. Also I do not have a proper way of closing the window. Any suggestions to how to improve this accept loop? The code is as follows: 

 

-- Get the displays
local displays = GetDisplays()

-- Create a window
local window = CreateWindow("Calculator", 0, 0, 800, 660, displays[1], WINDOW_CENTER | WINDOW_TITLEBAR)-- | WINDOW_RESIZABLE)

-- Create User Interface
local ui = CreateInterface(window)


--Create buttons
local sz = window:ClientSize()
local x = (sz.x - 120) / 2
local y = 50
local sep = 40

-- Create widget
local panel = CreatePanel(50, 50, sz.x - 100, sz.y - 100, ui.background)
panel:SetColor(0, 0, 0, 1)
panel:SetLayout(1, 1, 1, 1)

-- Create text field
local textfield = CreateTextField(70, 10, 600, 30,ui.background);
textfield:SetText("0");
textfield:SetFontScale(2);
textfield:SetColor(0,0,0,1)

-- Row 1
   local buttonMC = CreateButton("MC", 30, 30, 70, 70, panel);
   local buttonMR = CreateButton("MR", 110, 30, 70, 70, panel);
   local buttonM_Plus = CreateButton("M+", 190, 30, 70, 70, panel);
   local buttonM_Minus = CreateButton("M-", 270, 30, 70, 70, panel);

-- Row 2
local buttonCE = CreateButton("CE", 30, 110, 70, 70, panel);
local buttonC = CreateButton("C", 110,110, 70, 70, panel);
local buttonPct = CreateButton("%", 190, 110, 70, 70, panel);
local buttonDivide = CreateButton("/",270, 110, 70, 70, panel);

-- Row 3
local button1 = CreateButton("1", 30, 190, 70, 70, panel);
local button2 = CreateButton("2", 110, 190, 70, 70, panel);
local button3 = CreateButton("3", 190, 190, 70, 70, panel);
local buttonMultiply = CreateButton("X", 270, 190, 70, 70, panel);

-- Row 4
local button4 = CreateButton("4", 30, 270, 70, 70, panel);
local button5 = CreateButton("5", 110, 270, 70, 70, panel);
local button6 = CreateButton("6", 190, 270, 70, 70, panel);
local button_Minus = CreateButton("-", 270, 270, 70, 70, panel);

-- Row 5
local button7 = CreateButton("7", 30, 350, 70, 70, panel);
local button8 = CreateButton("8", 110, 350, 70, 70, panel);
local button9 = CreateButton("9", 190, 350, 70, 70, panel);
local button_Plus = CreateButton("+", 270, 350, 70, 70, panel);

-- Row 6
local buttonPlusMinus = CreateButton("+/-", 30, 430, 70, 70, panel);
local button0 = CreateButton("0", 110, 430, 70, 70, panel);
local buttonDecimal = CreateButton(",", 190, 430, 70, 70, panel);
local buttonResult = CreateButton("=", 270, 430, 70, 70, panel);


while true do
    local ev = WaitEvent()
    
        if (ev.source == button1 ) then        
            FirstNum = 1;
            textfield:SetText("1");
        
        elseif (ev.source == button2) then
        
            FirstNum = 2;
            textfield:SetText("2");
        end    
--    elseif EVENT_WINDOWCLOSE then return 0;
        
 --   if ev.id == EVENT_WIDGETACTION then
 --       Print(Widget(ev.source).text)
 --   elseif ev.id == EVENT_WINDOWCLOSE then
 --       return 0
 --   end
end

  • Like 1
Posted

Your application looks very nice. Try this code:

 -- Get the displays
local displays = GetDisplays()

-- Create a window
local window = CreateWindow("Calculator", 0, 0, 800, 660, displays[1], WINDOW_CENTER | WINDOW_TITLEBAR)-- | WINDOW_RESIZABLE)

-- Create User Interface
local ui = CreateInterface(window)


--Create buttons
local sz = window:ClientSize()
local x = (sz.x - 120) / 2
local y = 50
local sep = 40

-- Create widget
local panel = CreatePanel(50, 50, sz.x - 100, sz.y - 100, ui.background)
panel:SetColor(0, 0, 0, 1)
panel:SetLayout(1, 1, 1, 1)

-- Create text field
local textfield = CreateTextField(70, 10, 600, 30,ui.background);
textfield:SetText("0");
textfield:SetFontScale(2);
textfield:SetColor(0,0,0,1)

-- Row 1
   local buttonMC = CreateButton("MC", 30, 30, 70, 70, panel);
   local buttonMR = CreateButton("MR", 110, 30, 70, 70, panel);
   local buttonM_Plus = CreateButton("M+", 190, 30, 70, 70, panel);
   local buttonM_Minus = CreateButton("M-", 270, 30, 70, 70, panel);

-- Row 2
local buttonCE = CreateButton("CE", 30, 110, 70, 70, panel);
local buttonC = CreateButton("C", 110,110, 70, 70, panel);
local buttonPct = CreateButton("%", 190, 110, 70, 70, panel);
local buttonDivide = CreateButton("/",270, 110, 70, 70, panel);

-- Row 3
local button1 = CreateButton("1", 30, 190, 70, 70, panel);
local button2 = CreateButton("2", 110, 190, 70, 70, panel);
local button3 = CreateButton("3", 190, 190, 70, 70, panel);
local buttonMultiply = CreateButton("X", 270, 190, 70, 70, panel);

-- Row 4
local button4 = CreateButton("4", 30, 270, 70, 70, panel);
local button5 = CreateButton("5", 110, 270, 70, 70, panel);
local button6 = CreateButton("6", 190, 270, 70, 70, panel);
local button_Minus = CreateButton("-", 270, 270, 70, 70, panel);

-- Row 5
local button7 = CreateButton("7", 30, 350, 70, 70, panel);
local button8 = CreateButton("8", 110, 350, 70, 70, panel);
local button9 = CreateButton("9", 190, 350, 70, 70, panel);
local button_Plus = CreateButton("+", 270, 350, 70, 70, panel);

-- Row 6
local buttonPlusMinus = CreateButton("+/-", 30, 430, 70, 70, panel);
local button0 = CreateButton("0", 110, 430, 70, 70, panel);
local buttonDecimal = CreateButton(",", 190, 430, 70, 70, panel);
local buttonResult = CreateButton("=", 270, 430, 70, 70, panel);

while true do
    local ev = WaitEvent()
    
	if ev.id == EVENT_WIDGETACTION then
		if (ev.source == button1 ) then        
			FirstNum = 1;
			if textfield.text == "0" then textfield:SetText("") end
			textfield:SetText(textfield.text.."1");
		elseif (ev.source == button2) then  
			FirstNum = 2;
			if textfield.text == "0" then textfield:SetText("") end
			textfield:SetText(textfield.text.."2");
		end
	elseif ev.id == EVENT_WINDOWCLOSE then
		break
	end
end 

 

  • Like 1

Let's build cool stuff and have fun. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...