Hello! This is my first post. I come from Ye' Olde BASIC for the DOS days. I actually still used until a couple days ago. It's incompatibility with Windows has brought me to PureBasic.
I copied an example straight out of the help file for the ButtonGadget command:
; Shows possible flags of ButtonGadget in action...
If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
ButtonGadget(3, 10,100, 200, 60, "Multiline Button (longer text gets automatically wrapped)", #PB_Button_MultiLine)
ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
It works as expected. Then I modified it by inserting some code after the window and buttons were created. It no longer worked. I simplified the program to result with:
; Shows possible flags of ButtonGadget in action...
If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
ButtonGadget(3, 10,100, 200, 60, "Multiline Button (longer text gets automatically wrapped)", #PB_Button_MultiLine)
ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
EndIf
; The delay is used in place of some code segments to do something else.
Delay(5000)
loophere:
If WaitWindowEvent() <> #PB_Event_CloseWindow
Goto loophere
EndIf
End
As the comment says..... I put my "Other" code where the Delay(5000) command is. The window opens, but the buttons do not display until the execution reaches the If WaitWindowEvent().......... At that point, the buttons display and are functional.
If I comment out the "Delay" (or my other code) the buttons work just fine.
What in the world is wrong??? I can't sit there and do a "If WaitWindowEvent().........." all the time or else my program wouldn't do a stinking thing except display buttons!
Help with using ButtonGadget
Re: Help with using ButtonGadget
There's nothing wrong.
Delay delays the execution of the next code.
If your code (that you removed) takes 5seconds to execute that's either loading time, like many programs have, or in your case, since you are a beginner, there's an error in the code eg endless loop.
Delay delays the execution of the next code.
If your code (that you removed) takes 5seconds to execute that's either loading time, like many programs have, or in your case, since you are a beginner, there's an error in the code eg endless loop.
Re: Help with using ButtonGadget
Ok, I had a thought and changed the code as follows:
; Shows possible flags of ButtonGadget in action...
If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
ButtonGadget(3, 10,100, 200, 60, "Multiline Button (longer text gets automatically wrapped)", #PB_Button_MultiLine)
ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
EndIf
loophere:
; The delay is used in place of some code segments to do something else.
Delay(500)
If WaitWindowEvent() <> #PB_Event_CloseWindow
Goto loophere
EndIf
End
Now the program opens a window which stays blank for 1000ms, then displays a button every 500ms until all buttons are displayed.
I don't get it.
; Shows possible flags of ButtonGadget in action...
If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
ButtonGadget(3, 10,100, 200, 60, "Multiline Button (longer text gets automatically wrapped)", #PB_Button_MultiLine)
ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
EndIf
loophere:
; The delay is used in place of some code segments to do something else.
Delay(500)
If WaitWindowEvent() <> #PB_Event_CloseWindow
Goto loophere
EndIf
End
Now the program opens a window which stays blank for 1000ms, then displays a button every 500ms until all buttons are displayed.
I don't get it.
Re: Help with using ButtonGadget
Thank you for the kind reply. There's no error in my "Other" code, it just takes a long time to execute: I built the "Other" code first and got it working. It makes a polar plot based on distances registered by an ultrasonic sensor along a ~360 degree sweep.TomS wrote:There's nothing wrong.
Delay delays the execution of the next code.
If your code (that you removed) takes 5seconds to execute that's either loading time, like many programs have, or in your case, since you are a beginner, there's an error in the code eg endless loop.
.... then I said, y'know, it would be cool to have some actual buttons to control this thing.... so that's where I am now.
Why doesn't the window with the buttons update until it reaches the "If WaitWindowEvent()......." stuff??? From the description in the help file, WaitWindowEven() is a check to see if any gadgets have been pressed eg: Exit, Minimize or Maximize. It doesn't sound like it's an "Update Window" function. But that's what it acts like.
Re: Help with using ButtonGadget
The window doesn't update because it doesn't get any events (including drawing etc.) until WaitWindowEvent(). Try putting "While WindowEvent() : Wend" before the loophere: -label to clear the event queue and update the window.
Re: Help with using ButtonGadget
The event loop handles events, including button presses, window moves, and importantly, window redrawing. So you have to keep this loop running.
There are a few options:
A. Break up your function so that it takes less time to run but must be called many times to complete. For this case, this is probably a bad solution.
B. Use a thread for the processing. A thread is basically a procedure that runs at the same time as the main program, but independently. So the event loop runs in the main thread (which is automatically created) and your procedure runs in another thread (which you created with CreateThread()).
C. Use a thread for the main window and event loop. This is similar to above, except that your long-winded procedure runs in the main thread and the event loop runs in another thread.
D. Place event handling in a procedure, and then call this event handling procedure from inside your long-winded procedure.
The proper way to solve this is with threads (B. or C.). It is however an unfortunate fact that thread programming may require some intermediate knowledge. For instance, it's not straightforward to pass parameters to the thread, nor is it straightforward to return a value from the thread.
D. is also a fair choice provided you have a thorough understanding of event handling, which you as a beginner lack (so it won't be a good choice for you). So I'll recommend that you learn threads as it's the most powerful and best solution once you know it.
There are a few options:
A. Break up your function so that it takes less time to run but must be called many times to complete. For this case, this is probably a bad solution.
B. Use a thread for the processing. A thread is basically a procedure that runs at the same time as the main program, but independently. So the event loop runs in the main thread (which is automatically created) and your procedure runs in another thread (which you created with CreateThread()).
C. Use a thread for the main window and event loop. This is similar to above, except that your long-winded procedure runs in the main thread and the event loop runs in another thread.
D. Place event handling in a procedure, and then call this event handling procedure from inside your long-winded procedure.
The proper way to solve this is with threads (B. or C.). It is however an unfortunate fact that thread programming may require some intermediate knowledge. For instance, it's not straightforward to pass parameters to the thread, nor is it straightforward to return a value from the thread.
D. is also a fair choice provided you have a thorough understanding of event handling, which you as a beginner lack (so it won't be a good choice for you). So I'll recommend that you learn threads as it's the most powerful and best solution once you know it.
Re: Help with using ButtonGadget
Hmmmm.
Thank you both. I'll play with both of your suggestions and see what I come up with.
It looks like things work very different to what I'm used to.
Have a great day!...........Or night for you
Thank you both. I'll play with both of your suggestions and see what I come up with.
It looks like things work very different to what I'm used to.
Have a great day!...........Or night for you
