0

朋友们。

在附件中,我展示了一个使用 IUP gui 的 Lua 语言表单示例。

需要的是,在第一个文本框中按下每个键都会进行一些计算,并且结果必须显示在第二个文本框中。

我发现的问题是它最近显示了答案......这意味着......

按下了一个数字...但未显示结果。

按下第二个数字.... 显示的结果好像在第一个文本框中只有第一个数字。

按下第三个数字......显示的结果就像在第一个文本框中是前两个数字一样。

等等。

你能说我与代码的不一致在哪里吗?

我的目标是开发 7GUIs 中提出的七个 GUI: https ://github.com/eugenkiss/7guis

但我需要解决一些问题。

谢谢朋友。

赫南

---------------------------------
-- Temperatura.lua
-- Hernán Cano Martínez
-- Ene-2018
---------------------------------

package.cpath = "?.dll;?53.dll;lua533/?.dll;lua533/?53.dll;"   -- [in Windows]

require ( "iuplua" )

-- iup.key_open()  -- is obsolete and not necessary anymore in since IUP 3.0 RC 3 .

-- ***************************************************************************

-- los controles que usaremos en nuestro formulario

txtC = iup.text   { value="0"          , expand='NO', floating='NO', rastersize = "70"   , cx="030", cy= "30", font ="Courier New, 10", NC='10', ACTIVE='YES', ALIGNMENT='ACENTER' }
lblC = iup.label  { title = "Celsius =", expand='NO', floating='NO', rastersize = "85x25", cx="110", cy= "27", font ="Courier New, 10" }  -- size  in pixels

txtF = iup.text   { value="0"          , expand='NO', floating='NO', rastersize = "70"   , cx="200", cy= "30", font ="Courier New, 10", NC='10', ACTIVE='YES', ALIGNMENT='ACENTER' }
lblF = iup.label  { title = "Farenheit", expand='NO', floating='NO', rastersize = "85x25", cx="280", cy= "27", font ="Courier New, 10" }  -- size  in pixels

btnC = iup.button { title = "Cerrar"   , expand='NO', floating='NO', rastersize = "75x25", cx="200", cy="100", font ="Segoe IU, 9", TIP='Haga click para cerrar' }

-- ***************************************************************************

-- el Contenedor de controles

vArea = iup.cbox{ expand='NO', floating='NO', size = "450x200",
  txtC, lblC, txtF, lblF, --btnC,
  nil
}

-- El formulario

frmTemperatura = iup.dialog{ expand='NO', floating='NO', 
  vArea,
  title = "Temperatura", 
  size = "300x100"
}

-- ********************************** Callbacks *****************************************

function btnC:action()
  -- Exits the main loop
  return iup.CLOSE  
end


function txtC:action(t)

   if txtC.value and tonumber(txtC.value) then
      local nNum = 0 
      nNum = tonumber(txtC.value)
      txtF.value = nNum * (9/5) + 32
   end
   -- return iup.CONTINUE  -- HCM: I am not sure         
end

function txtF:action(t)

   if txtF.value and tonumber(txtF.value) then
      local nNum = 0 
      nNum = tonumber(txtF.value)
      txtC.value = nNum * (9/5) + 32
   end
   -- return iup.CONTINUE  -- HCM: I am not sure         
end

--------------------------------------------

-- Ahora sí: mostremos el formulario

frmTemperatura:showxy(iup.CENTER,iup.CENTER)

-- to be able to run this script inside another context
-- if (iup.MainLoopLevel()==0) then
if (not iup.MainLoopLevel or iup.MainLoopLevel()==0) then
  iup.MainLoop()
end

----------------------------------------------
4

1 回答 1

0

在值修改期间调用操作回调,因此如果您在回调期间查询属性,它将返回旧值。新值作为回调中的参数传递。因此,您应该将代码更改为:

function txtC:action(c, new_value)
   if new_value and tonumber(new_value) then
      local nNum = tonumber(new_value)
      txtF.value = nNum * (9/5) + 32
   end
end

function txtF:action(c, new_value)
   if new_value and tonumber(new_value) then
      local nNum = tonumber(new_value)
      txtC.value = nNum * (9/5) + 32
   end
end
于 2018-01-17T10:42:14.770 回答