Computer Science Canada

Can you have if statements in with GUI buttons?

Author:  Jesse.A [ Sun May 25, 2014 1:34 pm ]
Post subject:  Can you have if statements in with GUI buttons?

What is it you are trying to achieve?
I am trying to use if statements with buttons.

What is the problem you are having?
I realy have no idea how i would go about doing it.


Describe what you have tried to solve this problem
I have messed around a little bit but nothing seems to be working.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)



Turing:



import GUI
var button1, button2, button3 : int
var x : string
forward procedure display


procedure menu
    button1 := GUI.CreateButton (150, 200, 0, "Button1", display)
    button2 := GUI.CreateButton (250, 200, 0, "Button2", display)
    button3 := GUI.CreateButton (350, 200, 0, "Quit", GUI.Quit)
   
    loop
    if button1 = 1 and GUI.ProcessEvent then
    x := "1"
    exit
    elsif button2 = 1 and GUI.ProcessEvent then
        x := "2"
        end if
        end loop
end menu

body procedure display
         put "You pressed button" +x
end display
%main
menu
display





Please specify what version of Turing you are using
4.1.1

Author:  Insectoid [ Sun May 25, 2014 4:01 pm ]
Post subject:  RE:Can you have if statements in with GUI buttons?

Instead of using an if statement, have your display procedure take x as a parameter and pass it on your button lines.

code:
button1 := GUI.CreatButton (150, 200, 0, "Button1", display(1))

Author:  Zren [ Sun May 25, 2014 8:14 pm ]
Post subject:  Re: RE:Can you have if statements in with GUI buttons?

Insectoid @ Sun May 25, 2014 4:01 pm wrote:
Instead of using an if statement, have your display procedure take x as a parameter and pass it on your button lines.

code:
button1 := GUI.CreatButton (150, 200, 0, "Button1", display(1))


That doesn't work. Turing doesn't support lambdas, so you can't make simple callbacks like that.

Author:  Tony [ Mon May 26, 2014 2:31 am ]
Post subject:  RE:Can you have if statements in with GUI buttons?

That's not lambda (in a way of anonymous function definition). The display is assumed to be already defined, and here it's referenced with an argument. I'm pretty sure that this works, although in a non-documented way.

Oddly enough, there's GUI.GetEventWidgetID, which could be used to distinguish between different widgets calling the same procedure.

Author:  Jesse.A [ Mon May 26, 2014 8:35 pm ]
Post subject:  RE:Can you have if statements in with GUI buttons?

Thank you very much!

Author:  Zren [ Mon May 26, 2014 8:56 pm ]
Post subject:  Re: RE:Can you have if statements in with GUI buttons?

Tony @ Mon May 26, 2014 2:31 am wrote:
That's not lambda (in a way of anonymous function definition). The display is assumed to be already defined, and here it's referenced with an argument. I'm pretty sure that this works, although in a non-documented way.

Oddly enough, there's GUI.GetEventWidgetID, which could be used to distinguish between different widgets calling the same procedure.


So wait, calling a procedure with arguments returns a value? That value being a new procedure without parameters? What version of Turing supports this?

Author:  Tony [ Tue May 27, 2014 12:48 pm ]
Post subject:  RE:Can you have if statements in with GUI buttons?

I don't remember this well, you might be right -- an argumented procedure might been to return a procedure without parameters. Turing does support procedures as types -- see subprogramType


: