Continuosly pressing a button in a traitsui -based GUI - enthought

I'm building a GUI for a machine with Traits and TraitsUI. I would like the machine to perform an action as long as a button on the GUI is pressed and of course having the GUI not freeze and display the outcome of this continuous action, i.e. in a separate thread.
I can define in the class which inherits HasTraits a button and a function which performs the actions when the button is pressed (def _button_fired:), but it seems to me that the way the _button_fired is defined, the GUI actually waits for the button-pressing to be over before performing the "Action". As stated above, I would like it to execute a function the second I press the button and stop this function execution (or execute a different function) when I release the button.
You can take as an example code snippet no. 7 from Gael's tutorial and just imagine that while pressing the button, the counter should increase.
Your help will be greatly appreciated.
Hellbourne

Related

How to detect Matlab "uiopen" cancel button

I'm trying to use uiopen in a Matlab script to build up a dataset. If the user clicks the cancel button though I can't figure out how to deal with that event. With uigetfile it seems like you can just check to see if the input is used, but I can't see a way to do that with uiopen.
Can you detect or use the cancel button when using uiopen, or should I rewrite my script for uigetfile? If it's the second... what is uiopen for? The documentation doesn't seem to deal with the cancel button at all from what I can see.

How to make a button trigger an event (Timer)?

I am working on a fire evacuation project and would like to trigger an event after clicking a button. Basically I want to begin the fire evacuation process manually using the button feature but would like to trigger a timer for about 60 seconds to give the pedestrians time to evacuate using the event feature.
I have tried to make pedestrians stop the evacuation process by manually clicking another button but would like to use an event trigger timer instead as this will be a better way to conduct my simulation. I cannot seem to trigger an event once the button is clicked. I've tried changing the trigger type but not sure where to go from there. Is there any code that is required or another step that is required?
Even though a dynamic event is fine in your application, you can also use a user control event:
And then in your button you can use the following code:
event.restart(60,SECOND); //you can choose any timeout here
or
event.restart(); // this will use the configuration, which in this case I set up to 60 seconds
The difference between a dynamic event and a user controlled event is that if you click the button again:
With the dynamic event, you will generate another instance of the event, meaning that if you click the button at t=0 seconds, and t=30 seconds, you will generate 2 events at t=60 seconds and t=90 seconds
With the user control event, you will restart the same event. if you click the button at t=0 seconds, and t=30 seconds, you will generate a unique event at t=90 seconds.
So depending on which one you prefer... you can choose. I would prefer the user control in case you click the button 2 times... or if you regret clicking it so fast... I don't know
Use the Dynamic Event for this.
Set it up by pulling it from the AnyLogic Agent Library to the Workspace and give it a name (here: MyDynamicEvent).
Add the code you want to have executed when the timer runs off in the Action field
Set one (or several) timed instance for this event by using the code create_MyDynamicEvent(60,SECOND);. Note: The syntax of this statement is always create_ followed by the actual name you gave your DynamicEvent type.

How to block GUI while function with output executes?

I am creating a MATLAB application in GUIDE and now I'm facing a problem. I need to call a function that takes a long time to execute and returns a value but while executing the function I want the GUI to wait for the returned value.
I tried with waitfor but this way I can still interact with the GUI and I can't take the returned value...
waitfor(function);
I can think of something that disables all the GUI then enables it back but I have both enabled and disabled objects...
Do you know any solution to this problem?
A simple solution is to create a modal dialog box with a message "Please wait..." just before executing your long-running function, and then to close the dialog box just after it completes. A modal dialog will be in front of the GUI, and will not allow interactions with the window behind.
It's possible for the user to click the "Close" button on the dialog, but you can override this by setting the "CloseRequestFcn" property of the dialog, so that the close button does nothing (unfortunately you can't easily hide the button).
I like the modal dialog proposed by Sam Roberts. There is no mystery and it is user friendly.
Another dirty and easy solution may be to hide the GUI completely, if it is okay:
set(hFig, 'Visible', 'off');
And set it to 'on' after done. It will be good practice to make sure to set it to 'on' in catch block, to avoid disappearing GUI due to error during execution.

Enabling and Disabling GUI button Matlab

This is a very strange problem, because I am quite sure the logic is proper but it is still not working. So when the GUI starts, in the starting function, I set most of the buttons to be inactivated using the following line of code for every button, Kbutton, Bbutton etc..:
set(handles.Kbutton,'Enable','off');
set(handles.Bbutton,'Enable','off');
Then, when the user clicks on any cell in the uitable, I use the cell selection callback to get the info about the contents of the cell, and re-activate the buttons that can be used from then on:
set(handles.Kbutton,'Enable','on');
set(handles.Bbutton,'Enable','on');
And till now everything is ok.
So then the user clicks on a button, say Kbutton, and the underlying function is executed thanks to the button's callback function. Once the method is ready I would like to return the buttons to their initial state i.e. inactivated. So logic tells me, re-put the initial line of code at the end of the callback function and done:
set(hObject,'Enable','off');
drawnow;
set(handles.BButton,'Enable','off');
drawnow;
Where the first line of code in the above snippet, refers to the button who fired the callback and BButton is any other button in the GUI.
For some strange reason only the second one is being enabled off. I.e. the button that fired the callback is staying enabled on :/
Has anyone encountered the same problem?
Any ideas?
Thanks in advance
Try changing
set(hObject,'Enable','off');
to
set(handles.Kbutton,'Enable','off');
You click on tools =>gui options =>Generate FlG file and MATLAB file

Inner Workings of Unity3d's GUI.Button

I'm still pretty new to scripting in Unity3D, and I'm following along with a tutorial that uses GUI.Button() to draw a button on the screen.
I am intrigued by how this function works. Looking through the documentation, the proper use of GUI.Button is to invoke the function in an if statement and put the code to be called when the button is pushed within the if statement's block.
What I want to know is, how does Unity3D "magically" delay the code in the if statement until after the button is clicked? If it was being passed in as a callback function or something, then I could understand what was going on. Perhaps Unity is using continuations under the hood to delay the execution of the code, but then I feel like it would cause code after the if statement to be executed multiple times. I just like to understand how my code is working, and this particular function continues to remain "magical" to me.
I don't know if it's the right term, but I usually refer to such system as immediate mode GUI.
how does Unity3D "magically" delay the code in the if statement until
after the button is clicked?
GUI.Button simply returns true if a click event happened inside the button bounds during last frame. Basically calling that function you are polling: every frame for every button asking the engine if an event which regards that button (screen area) is happened.
If it was being passed in as a callback function or something, then I
could understand what was going on
You are probably used to an MVC like pattern, where you pass a controller delegate that's called when an UI event is raised from the view. This is something really different.
Perhaps Unity is using continuations under the hood to delay the
execution of the code, but then I feel like it would cause code after
the if statement to be executed multiple times.
No. The function simply returns immediately and return true only if an event happened. If returns false the code after the if won't be executed at all.
Side notes:
That kind of system is hard to maintain, especially for complex structured GUI.
It has really serious performance implications (memory allocation, 1 drawcall for UI element)
Unless you are writing an editor extension or custom inspector code, I'd stay away from it. If you want to build a menu implement your own system or use an external plugin (there are several good ones NGUI, EZGUI,..).
Unity has already announced a new integrated UI System, it should be released soon.
Good question. The unity3d gui goes through several event phases, or in the documentation
Events correspond to user input (key presses, mouse actions), or are UnityGUI layout or rendering events.
For each event OnGUI is called in the scripts; so OnGUI is potentially called multiple times per frame. Event.current corresponds to "current" event inside OnGUI call."
In OnGUI you can find out which event is currently happening with >Event.current
The following events are processed link:
Types of UnityGUI input and processing events.
-MouseDown
-MouseUp,mouse button was released
-MouseMove,Mouse was moved (editor views only)
-MouseDrag,Mouse was dragged
-KeyDown, A keyboard key was pressed
-KeyUp A keyboard key was released.
-ScrollWheel The scroll wheel was moved.
-Repaint A repaint event. One is sent every frame.
-Layout A layout event.
-DragUpdated Editor only: drag & drop operation updated.
-DragPerform Editor only: drag & drop operation performed.
-DragExited Editor only: drag & drop operation exited.
-Ignore Event should be ignored.
-Used Already processed event.
-ValidateCommand Validates a special command (e.g. copy & paste).
-ExecuteCommand Execute a special command (eg. copy & paste).
-ContextClick User has right-clicked (or control-clicked on the mac).
Unity GUI has much improved lately and is quite usefull if you want to handle things programmatically. If you want to handle things visually, i recommend looking at the plugins heisenbug refers to.
If you decide to use unity gui, i recommend using only one object with ongui, and let this object handle all your gui.