Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help problem with buttons
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sp14




PostPosted: Fri Jan 13, 2006 12:43 am   Post subject: help problem with buttons

heyy guys i hope someone can help im trying to put 2 buttons in my program and it wont work one of them is to start the program and the other is to quit and stup it can some one help here is the sorce code:



import GUI

var pic : int := Pic.FileNew ("C:/Documents and Settings/Administrator/Desktop/project/untitled1.JPG")
Pic.Draw (pic, 0, 10, picCopy)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%lights on north side

procedure redlight %red light- N
drawfillbox (185, 310, 215, 390, 7)
drawfilloval (200, 330, 10, 10, 2) %green light
drawfilloval (200, 352, 10, 10, 6) %yellow light
drawfilloval (200, 374, 10, 10, 12) %red light
drawfillbox (475, 10, 505, 90, 7)
drawfilloval (490, 78, 10, 10, 2) %green light
drawfilloval (490, 54, 10, 10, 6) %yellow light
drawfilloval (490, 30, 10, 10, 12) %red light
end redlight

procedure yellowlight %yellow light- N-S
drawfillbox (185, 310, 215, 390, 7)
drawfilloval (200, 330, 10, 10, 2) %green light
drawfilloval (200, 352, 10, 10, 14) %yellow light
drawfilloval (200, 374, 10, 10, 4) %red light
drawfillbox (475, 10, 505, 90, 7)
drawfilloval (490, 78, 10, 10, 2) %green light
drawfilloval (490, 54, 10, 10, 14) %yellow light
drawfilloval (490, 30, 10, 10, 4) %red light
end yellowlight

procedure greenlight %green light- N-S
drawfillbox (185, 310, 215, 390, 7)
drawfilloval (200, 330, 10, 10, 10) %green light
drawfilloval (200, 352, 10, 10, 6) %yellow light
drawfilloval (200, 374, 10, 10, 4) %red light
drawfillbox (475, 10, 505, 90, 7)
drawfilloval (490, 78, 10, 10, 10) %green light
drawfilloval (490, 54, 10, 10, 6) %yellow light
drawfilloval (490, 30, 10, 10, 4) %red light
end greenlight
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%lights on east side

procedure greenlight2 %green light- S
drawfillbox (595, 260, 515, 295, 7)
drawfilloval (530, 277, 10, 10, 10) %green light
drawfilloval (553, 277, 10, 10, 6) %yellow light
drawfilloval (576, 277, 10, 10, 4) %red light
drawfillbox (185, 100, 110, 130, 7)
drawfilloval (170, 115, 10, 10, 10) %green light
drawfilloval (147, 115, 10, 10, 6) %yellow light
drawfilloval (125, 115, 10, 10, 4) %red light
end greenlight2

procedure redlight2 %red light- S
drawfillbox (595, 260, 515, 295, 7)
drawfilloval (530, 277, 10, 10, 2) %green light
drawfilloval (553, 277, 10, 10, 6) %yellow light
drawfilloval (576, 277, 10, 10, 12) %red light
drawfillbox (185, 100, 110, 130, 7)
drawfilloval (170, 115, 10, 10, 2) %green light
drawfilloval (147, 115, 10, 10, 6) %yellow light
drawfilloval (125, 115, 10, 10, 12) %red light
end redlight2

procedure yellowlight2 %yellow light- S
drawfillbox (595, 260, 515, 295, 7)
drawfilloval (530, 277, 10, 10, 2) %green light
drawfilloval (553, 277, 10, 10, 14) %yellow light
drawfilloval (576, 277, 10, 10, 4) %red light
drawfillbox (185, 100, 110, 130, 7)
drawfilloval (170, 115, 10, 10, 2) %green light
drawfilloval (147, 115, 10, 10, 14) %yellow light
drawfilloval (125, 115, 10, 10, 4) %red light
end yellowlight2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%






procedure button
loop
redlight
greenlight2
delay (2000)
redlight
yellowlight2
delay (1000)
greenlight
redlight2
delay (2000)
yellowlight
redlight2
delay (1000)
end loop
end button


var start : int := GUI.CreateButton (1, 10, 0, "Start", button)
var quitbutton : int := GUI.CreateButton (80, 10, 0, "Quit", GUI.Quit)





loop
exit when GUI.ProcessEvent
end loop
Sponsor
Sponsor
Sponsor
sponsor
Albrecd




PostPosted: Fri Jan 13, 2006 9:02 am   Post subject: (No subject)

The reason that you can't quit while the program is running is that you need an exit when GUI.ProcessEvent statement in your main loop (procedure button). One problem with this is that you will have to put multiple exit whens; otherwise, it will only quit if you click on the button at the exact time that the program is running over the exit when statement. Also, if the user clicks during one of the delays, It will not register the button.

My suggestion would be to split up the delays. It's impractical and sloppy, but there isn't really another option.

code:
procedure button
loop
exit when GUI.ProcessEvent
redlight
greenlight2
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
redlight
yellowlight2
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
greenlight
redlight2
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
yellowlight
redlight2
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
delay (500)
exit when GUI.ProcessEvent
end loop
end button


These delays, while much smaller, will still cause gaps in the click recognition. If it's important to you, I would suggest splitting them up further.
Delos




PostPosted: Fri Jan 13, 2006 9:36 am   Post subject: (No subject)

Please use [code] or [syntax] tags when posting code.

No other option?! Hardly...

Ok, so you're playing around with concurrency. This stuff is never pretty, and as Albrecd mentioned, solutions are often quite sloppy and messy.
You may choose to use processes in this case. I high recommend aversion to these, since they cause more problems than they fix unless you know them inside out.
These tuts will explain why not to, and how to use processes if you have to:
Basics
Rants on why not to
Processes Pt 1
Processes Pt 2

That being said, it would be more expediant for you to set up everything in a single 'main loop' similar to what Albrecd has there. The difference being that you should minimize the use of delays for the reasons that they gave (button clicking will not necassarily produce results). Instead, you'll have to control your animation be using counters to determine how much time has passed since the last change of frame.
So, at t (time) = 0, all lights are red. When the t=500, variables change and the lights change to yellow. When t=1650, vars change again, and you get green lights. At t=2000, some vars are reset, the colours go back to red, and the cycle begins again.
Because in this case you minimize the use of delays, your buttons will respond faster. Keep in mind that using absolute values (500, 1650, etc) will only work for the first cycle - instead, use variable values (like multiples of numbers) so that they work indefinitely.
sp14




PostPosted: Fri Jan 13, 2006 9:51 am   Post subject: (No subject)

thanks so much man it worked thanks for helping
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: