Easy calculator Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
flavio96
Posts: 2
Joined: 30 Dec 2015, 05:58

Easy calculator

30 Dec 2015, 09:45

hello i'm new at ahkforum , i readed the guide but i didn't found a way to display the result of a multiplication i learn a way to display and insert variable but i don't know how to make an expression and display it in the gui console
her's the code i use

Gui, Add, Text,, first number:
Gui, Add, edit, number vpve
Gui, Add, Text,, second number:
Gui, Add, edit, number vnpe
Gui, Add, Button, Default , result
Gui, Add, edit, result vres
submitit

Re: Easy calculator

30 Dec 2015, 09:54

You need to create a label for the result button and "Submit" - https://autohotkey.com/docs/commands/Gui.htm#Submit - to get the variables from the Gui. After that you can use GuiControl to update the result edit control.

Code: Select all

Gui, Add, Text,, first number:
Gui, Add, edit, number vpve
Gui, Add, Text,, second number:
Gui, Add, edit, number vnpe
Gui, Add, Button, Default , result
Gui, Add, edit, result vres
Gui, Show
Return

ButtonResult:
Gui, Submit, NoHide
res:=pve*npe
GuiControl,, res, %res%
Return
esc::ExitApp
User avatar
silvex3000
Posts: 188
Joined: 19 Dec 2015, 22:42

Re: Easy calculator

30 Dec 2015, 10:12

Wow!

I wrote this code right now, and I don't even believe that Autohotkey can't handle this:

Code: Select all


gui, add, text, , Type here: 
gui, add, edit, x+5 w100 vUserType, 1+1
gui, add, button, x+5 gcalculate, Calculate
gui, add, text, xm, Result:
gui, add, edit, x+5 w100 vresult, 
gui, show, , Calculator
return

calculate:
gui, submit, nohide
calculation :=  %UserType%
guicontrol, , result, %calculation%
return

guiclose:
exitapp

I'm in Shock (very disappointed with AutoHotkey)
flavio96
Posts: 2
Joined: 30 Dec 2015, 05:58

Re: Easy calculator

30 Dec 2015, 10:32

thank you for support ;)
User avatar
Relayer
Posts: 121
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: Easy calculator

30 Dec 2015, 13:34

Silvex3000 wrote:
I'm in Shock (very disappointed with AutoHotkey)
Don't be disappointed. The issue is with this line.
calculation := %UserType%
AutoHotkey has two equal signs: '=' and ':='
When using ':=' the line would be:
calculation := UserType
Your script runs as expected with:
calculation = %UserType%

Most prefer the former syntax. Learning when to use %var% vs just the variable name is key to using AHK.

Relayer

edit: I may have missed that you wanted the expression evaluated and not simply echoed. I think you are having that discussion in another thread. Good day!
MonuKashyap
Posts: 37
Joined: 06 Jun 2016, 21:32

Re: Easy calculator

16 Jun 2018, 04:16

"calculation = %UserType%"

That also not working.. Relayer,
is there any other option??
User avatar
Flipeador
Posts: 1019
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Easy calculator [Updated 2018-07-29]  Topic is solved

16 Jun 2018, 06:36

AHKv1:

Code: Select all

; ===================================================================================================
; GUI
; ===================================================================================================
Gui Add, Text, x5 y5 w300 h21, Expression to calculate:
Gui Add, Edit, x5 y30 w300 vUserType, 2 * PI * abs(-2)

Gui Add, Text, x5 y70 w300 h21, Result:
Gui Add, Edit, x5 y95 w300 vResult

Gui Add, Button, x5 y125 w300 gCalculate Default, Calculate

Gui Show,, AutoHotkey Simple Calculator
return


; ===================================================================================================
; LABELS
; ===================================================================================================
Calculate:
Gui Submit, NoHide
GuiControl ,, Result, % Eval(UserType)
return

GuiClose:
Exitapp


; ===================================================================================================
; FUNCTIONS
; ===================================================================================================
Eval(Expr, Format := FALSE)
{
    static obj := ""    ; par cuestiones de rendimiento
    if ( !obj )
        obj := ComObjCreate("HTMLfile")

    Expr := StrReplace( RegExReplace(Expr, "\s") , ",", ".")
  , Expr := RegExReplace(StrReplace(Expr, "**", "^"), "(\w+(\.*\d+)?)\^(\w+(\.*\d+)?)", "pow($1,$3)")    ; 2**3 -> 2^3 -> pow(2,3)
  , Expr := RegExReplace(Expr, "=+", "==")    ; = -> ==  |  === -> ==  |  ==== -> ==  |  ..
  , Expr := RegExReplace(Expr, "\b(E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)\b", "Math.$1")
  , Expr := RegExReplace(Expr, "\b(abs|acos|asin|atan|atan2|ceil|cos|exp|floor|log|max|min|pow|random|round|sin|sqrt|tan)\b\(", "Math.$1(")

  , obj.write("<body><script>document.body.innerText=eval('" . Expr . "');</script>")
  , Expr := obj.body.innerText

    return InStr(Expr, "d") ? "" : InStr(Expr, "false") ? FALSE    ; d = body | undefined
                                 : InStr(Expr, "true")  ? TRUE
                                 : ( Format && InStr(Expr, "e") ? Format("{:f}",Expr) : Expr )
} ; CREDITS (tidbit) - https://autohotkey.com/boards/viewtopic.php?f=6&t=15389
AHKv2:

Code: Select all

; ===================================================================================================
; GUI
; ===================================================================================================
Gui := GuiCreate("+ToolWindow", "AutoHotkey Simple Calculator")
Gui.AddText("x5 y5 w300 h21", "Expression to calculate:")
Input := Gui.AddEdit("x5 y30 w300", "2 * PI * abs(-2)")

Gui.AddText("x5 y70 w300 h21", "Result:")
Output := Gui.AddEdit("x5 y95 w300")

Gui.AddButton("x5 y125 w300 Default", "Calculate").OnEvent("Click", () => Output.Text := Eval(Input.Text))

Gui.Show()
return


; ===================================================================================================
; FUNCTIONS
; ===================================================================================================
Eval(Expr, Format := FALSE)
{
    static obj := ""    ; par cuestiones de rendimiento
    if ( !obj )
        obj := ComObjCreate("HTMLfile")

    Expr := StrReplace( RegExReplace(Expr, "\s") , ",", ".")
  , Expr := RegExReplace(StrReplace(Expr, "**", "^"), "(\w+(\.*\d+)?)\^(\w+(\.*\d+)?)", "pow($1,$3)")    ; 2**3 -> 2^3 -> pow(2,3)
  , Expr := RegExReplace(Expr, "=+", "==")    ; = -> ==  |  === -> ==  |  ==== -> ==  |  ..
  , Expr := RegExReplace(Expr, "\b(E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)\b", "Math.$1")
  , Expr := RegExReplace(Expr, "\b(abs|acos|asin|atan|atan2|ceil|cos|exp|floor|log|max|min|pow|random|round|sin|sqrt|tan)\b\(", "Math.$1(")

  , obj.write("<body><script>document.body.innerText=eval('" . Expr . "');</script>")
  , Expr := obj.body.innerText

    return InStr(Expr, "d") ? "" : InStr(Expr, "false") ? FALSE    ; d = body | undefined
                                 : InStr(Expr, "true")  ? TRUE
                                 : ( Format && InStr(Expr, "e") ? Format("{:f}",Expr) : Expr )
} ; CREDITS (tidbit) - https://autohotkey.com/boards/viewtopic.php?f=6&t=15389
Eval Function: https://github.com/flipeador/Library-Au ... h/Eval.ahk.
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
MonuKashyap
Posts: 37
Joined: 06 Jun 2016, 21:32

Re: Easy calculator

21 Jul 2018, 07:33

yeah this is working,
thank you Flipeador!!
Rohwedder
Posts: 1510
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Easy calculator

21 Jul 2018, 11:05

Hallo,
try:

Code: Select all

InputBox, Calc, Calculation, type your Calculation e.g.: 7+Sqrt(17)
FileDelete, A.ahk
FileAppend, ClipBoard := %Calc%, A.ahk
RunWait, A.ahk
MsgBox,, Result, %Calc% = %ClipBoard%
garry
Posts: 1563
Joined: 22 Dec 2013, 12:50

Re: Easy calculator

22 Jul 2018, 10:47

thank you for examples ,
@Rohwedder, short example with input , similar here from user 'Laszlo' :

Code: Select all

;- calculator user Laszlo  20050214
;https://autohotkey.com/board/topic/8751-minimalist-calculator-and-enhancements/
file=%a_scriptdir%\test.ahk
Gui,font,s14,Lucida Console
c=2*sqrt(16)
Gui,show,w440 h120,Calculator_Laszlo
Gui,add,edit, x10 y10 w400 h50 vEd1,%c%
Gui,add,button,gClear,Clear
Guicontrol,1:Focus,Ed1
send,^{end}
return
Guiclose:
exitapp
clear:
GuiControl,1:,ed1,
Guicontrol,1:Focus,Ed1
return
~$enter::
GuiControlGet,c,,ed1
   FileDelete %file%             ; delete old temporary file -> write new
   FileAppend #NoTrayIcon`nFileDelete %file%`nFileAppend `% %c%`, %file%, %file%
   RunWait %A_AhkPath% %file%    ; run AHK to execute temp script, evaluate expression
   FileRead, Result, %file%      ; get result
   FileDelete %file%
   GuiControl,1:,ed1,%c%=%Result%
return

Return to “Ask For Help”

Who is online

Users browsing this forum: 8bitgunny, Ggirf14, Google [Bot], tplarkin7, vvhitevvizard and 96 guests