Sign in to follow this  
Followers 0
derkaderka

Beginner GUI Calculator Code

6 posts in this topic

Hello, I have seen a lot of people on this forum saying they are beginners on gui (obviously because this is a gui help and support forum). So I made a basic script that shows how gui's work and what they look like.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $var = "" ;Global variables are variables that apply to the whole script. You cannot pass variables between functions so you can store them here if you need to access them by another function
Global $Readplus = ""
Global $Readminus = ""
Global $Readmultiply = ""
Global $Readdivide = ""

$Calculator = GUICreate("Calculator", 205, 214, 192, 124) ;Windows form is created, the numbers are the dimentions (KODA does this for you)
$Output = GUICtrlCreateInput("", 8, 16, 185, 21) ;Input box is created (It will be used as output)
$7 = GUICtrlCreateButton("7", 8, 48, 43, 25) ;GuiCtrlCreateButton Creates a button (numbers 0-9 and special characters)
$8 = GUICtrlCreateButton("8", 56, 48, 43, 25)
$9 = GUICtrlCreateButton("9", 104, 48, 43, 25)
$6 = GUICtrlCreateButton("6", 104, 80, 43, 25)
$4 = GUICtrlCreateButton("4", 8, 80, 43, 25)
$5 = GUICtrlCreateButton("5", 56, 80, 43, 25)
$1 = GUICtrlCreateButton("1", 8, 112, 43, 25)
$3 = GUICtrlCreateButton("3", 104, 112, 43, 25)
$2 = GUICtrlCreateButton("2", 56, 112, 43, 25)
$0 = GUICtrlCreateButton("0", 8, 144, 43, 25)
$decimal = GUICtrlCreateButton(".", 56, 144, 43, 25)
$multiply = GUICtrlCreateButton("*", 152, 80, 43, 25)
$minus = GUICtrlCreateButton("-", 152, 112, 43, 25)
$plus = GUICtrlCreateButton("+", 152, 144, 43, 25)
$divide = GUICtrlCreateButton("/", 152, 48, 43, 25)
$equals = GUICtrlCreateButton("=", 104, 144, 43, 25)
$clear = GUICtrlCreateButton("Clear", 120, 176, 75, 25)
GUISetState(@SW_SHOW) ;This means that the windows form is being shown (it is default) you can also set it so that it doesn't show (but I dont know why you would do that)


While 1 ;This while statement will continue to loop until something inside happens
    $nMsg = GUIGetMsg() ;This continuously tries to get the gui message
    $Read = GUICtrlRead($Output) ; This reads what the input box says (In gui you must do this to read an inputbox)
    Switch $nMsg ;Once a button is pressed, if it is part of the switch it will activate
        Case $GUI_EVENT_CLOSE ;$GUI_EVENT_CLOSE is just the same thing as saying when you press the close button
            Exit
        Case $0 ;Case means that if the button is pressed, or the checkbox checked, etc. then do the code inside
            GUICtrlSetData($Output, $Read & 0)
        Case $1
            GUICtrlSetData($Output, $Read & 1) ;GuiCtrlSetData is populating the $Output input box with the reading from above and then adding a number on the end
        Case $2
            GUICtrlSetData($Output, $Read & 2)
        Case $3
            GUICtrlSetData($Output, $Read & 3)
        Case $4
            GUICtrlSetData($Output, $Read & 4)
        Case $5
            GUICtrlSetData($Output, $Read & 5)
        Case $6
            GUICtrlSetData($Output, $Read & 6)
        Case $7
            GUICtrlSetData($Output, $Read & 7)
        Case $8
            GUICtrlSetData($Output, $Read & 8)
        Case $9
            GUICtrlSetData($Output, $Read & 9)
        Case $decimal
            $search = StringInStr($Read, ".") ;The StringInStr function looks for a specific character in the sequence, since we do not want two decimals in the calculator.
            If $search = 1 Then ;With StringInStr, 1 means that it found the character, 0 menas that it did not
                Return
            EndIf
            If $search = 0 Then
                GUICtrlSetData($Output, $Read & ".")
            EndIf
        Case $multiply
            $Readmultiply = GUICtrlRead($Output) ;Here the code is saving whatever is in the texbox to the global variable above so that other functions can access it.
            GUICtrlSetData($Output, "") ;This makes the input box empty
            $var = "*" ;this is setting another global variable, in this case $var is the operator for the calculator
        Case $minus
            $Readminus = GUICtrlRead($Output)
            GUICtrlSetData($Output, "")
            $var = "-"
        Case $divide
            $Readdivide = GUICtrlRead($Output)
            GUICtrlSetData($Output, "")
            $var = "/"
        Case $plus
            $Readplus = GUICtrlRead($Output)
            GUICtrlSetData($Output, "")
            $var = "+"
        Case $equals ;If the equals button is press then continue
            If $var = "+" Then ;This is accessing the global variable that defines the operator
                $Readplus2 = GUICtrlRead($Output) ;Reading the final number in the sequence
                GUICtrlSetData($Output, $Readplus + $Readplus2) ;This is setting the output box to show the sum of the two variables
            EndIf
            If $var = "-" Then
                $Readminus2 = GUICtrlRead($Output)
                GUICtrlSetData($Output, $Readminus - $Readminus2)
            EndIf
            If $var = "*" Then
                $Readmultiply2 = GUICtrlRead($Output)
                GUICtrlSetData($Output, $Readmultiply * $Readmultiply2)
            EndIf
            If $var = "/" Then
                $Readdivide2 = GUICtrlRead($Output)
                GUICtrlSetData($Output, $Readdivide /$Readdivide2)
                If $Readdivide2 = "0" Then ;Since you cannot divide by 0, this states that if the denominator is 0 then send an error message
                    GUICtrlSetData($Output, "ERROR")
                EndIf
            EndIf
        Case $clear
            GUICtrlSetData($Output, "")
    EndSwitch
WEnd

I hope this helps at least somebody out with basic GUI.

Share this post


Link to post
Share on other sites



Thats cool


shutdown(1)

Share this post


Link to post
Share on other sites

Think you need Exit on line 63, not Return. :oops:

D:au3test.au3(63,23) : ERROR: 'Return' not allowed from global scope.
                Return
~~~~~~~~~~~~~~~~~~~~~~^
D:au3test.au3 - 1 error(s), 0 warning(s)

But really not a bad example script :bye:


MsgBox(0x40040, "", "Hello Forum!")

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The documentation covers all of this. Secondly, not everyone knows or uses KODA (I know I don't). Thirdly, this should be in the example scripts forum.

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites

Nice! but when i perform the function 1 + 2 + 3 should be six but the return value i get 5 :oops:


Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Nice! but when i perform the function 1 + 2 + 3 should be six but the return value i get 5 :oops:

Yeah, it forgets 1+ as soon as 2+ is clicked. Same to be said with all forms of math. Only equation of 2 numbers can be done!

Edited by rogue5099

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0