How do I use two mouse press functions in python? - python

First scene with first mouse press
That first onMousePress function makes the background change.
Second scene with second mouse press
This second onMousePress function is planned to make the barrels disappear but it just kind of replaces the first onMousePress and makes it so that's the only one that works. Also ignore the 2 and the fact that it actually makes a rectangle, the 2 made it not work so I can click on the future tp and the rect is for testing purposes

You can set flag of first click and some small wait time. If the event is triggered second time it is double click, otherwise it is single click when time runs out. You need to set wait time in parallel thread or use something like timer. And don't forget to reset your flag everytime.

Related

Moving on to another stimulus whenever a key is pressed

I'm setting up an experiment on psychopy in which a stimulus is displayed on the screen for a set period of time (say 0.5s), after which a blank screen is presented with a fixation. I'm using event.waitKeys() in order to get Keyboard input.
I have written the following code.
for i in range (1, 21):
answer = cf.Stimulus() #This is a function for generating the stimulus
img = visual.ImageStim(
win=win,
image="temp.jpg",
units="pix"
)
img.draw() #This is the first screen
fixation.draw()
win.flip()
core.wait(0.5)
fixation.draw() #This is the second screen
win.flip()
keysarray = event.waitKeys()
os.remove('temp.jpg')
The problem which I'm running into here is that, if the keyboard input has been received before the wait time of 0.5 ends in the first screen, events.waitKeys() doesn't register this key entry and still waits on the second screen for the keyboard input. The program only moves forward if a key entry is received for the second screen.
Instead, I want the program to go to the next stimulus whenever a keyboard input is presented between the start of screen 1 to the end of screen 2. That is, if the keyboard input is received in screen 1 itself (before the end of 0.5s), I want the input to be registered and the program to move on to the next stimulus (either by moving to screen 2 for a very short duration of time, or by skipping screen 2 all together). I can't seem to figure out how this can be achieved.
The short answer here is that event.waitKeys() defaults to clearing the event queue, so that only new keypresses are detected. You can get the behaviour you want by overriding that:
keysarray = event.waitKeys(clearEvents=False)
But I think that keys pressed prior to calling the function would not have useful reaction times recorded (although keyboard handling has changed a lot in version 3.1).
Having said that, there are a bunch of other issues with this code that could be improved to fit a more optimal PsychoPy style. I'd suggest posting it at the user forum at https://discourse.psychopy.org. That forum is better suited to back-and-forth discussions than the single question/answer format here at SO.

Python Tkinter Command Click

I need to bind command button-click to a function in python. I have already bound the flag function to right click, but this only work when right-clicking with a mouse. As I write the majority of my code on a laptop, this is horribly inconvenient. Here is what I currently have:
# set up the mouse click listeners
self.bind('<Button-1>',self.expose)
self.bind('<Button-2>',self.flag)
#this is where I want to bind self.flag to command click
I'd like to use self.bind if possible, and simply bind command click to self.flag. Is it possible to do this?
You can just use:
self.bind("<Command-Button-1>",self.flag)
This can be done in addition to <Button-2> and you may want to also bind <Control-Button-1> for compatibility.
I would normally link to http://infohost.nmt.edu/tcc/help/pubs/tkinter/event-modifiers.html but it seems to be down at the moment, basically there are a few modifiers that can be used in combination with Button or Key:
Alt True when the user is holding the alt key down.
Any This modifier generalizes an event type. For example, the event pattern '<Any-KeyPress>' applies to the pressing of any key.
Control True when the user is holding the control key down.
Double Specifies two events happening close together in time. For example, <Double-Button-1> describes two presses of button 1 in rapid succession.
Lock True when the user has pressed shift lock.
Shift True when the user is holding down the shift key.
Triple Like Double, but specifies three events in rapid succession.
So you could for example bind <Control-Shift-Double-Button-1> if your program needed to be that elaborate.

Python Notebook run automatically periodically

I have a script in a notebook browser that does some live data analysis, that I would like to run periodically by itself once every few seconds/minutes, rather than I having to do Ctrl+Enter all the time.
Is there any way to achieve this? Do I need any additional plug-ins, and which ones
Simplest way to do it:
import time
def periodic_work(interval):
while True:
#change this to the function you want to call, or paste in the code you want to run
your_function()
#interval should be an integer, the number of seconds to wait
time.sleep(interval)
To run once per minute, run a new cell with periodic_work(60) -- you should see a closed circle in the top right of the IPython Notebook that indicates the Kernal is busy. If/when you want to stop the live update, hit the Stop button (labeled Interrupt) on the menu bar and wait a second or so. To start it up again, run the cell that calls periodic_work again.
One way would be to make your cell a function. In another cell, just call time.sleep () in an appropriate loop. You could also have it check for the existence of some external resource to quit, if you don't want to loop an a priori known finite number of times.

Python- run a while loop in background of script

So I have this script for controlling an LCD plate where I want the backlight to change every time you press a button, but also want to do other things as well. Currently this doesn't work and it doesn't display the messages I want it to.
This is the change backlight bit of code:
while True:
for b in btn:
if lcd.buttonPressed(b):
lcd.backlight(col[randint(0,5)])
And then I want to go on and run a bit of code that prints a string to the lcd and stuff, in this way:
lcd.message("This is a string")
but the script doesn't ever print the string, it just stays on the backlight change-y bit.
Basically I'm creating an index of letters that you can move through using the LCD buttons, and want the backlight to change each time you press a button.
The standard way is to have a single loop containing both. In the loop you first handles any input or state changes. Then at the end of your loop you update display. This is a fairly common paradigm in game programming see pygame for tons of examples. You would then only break once your program is terminating. One thing worth pointing out is that you would not want to block on checking for key press (or any input), otherwise you would hold up your display waiting for input.
sudo code would be something like:
while True:
for event in key_presses():
handle_event(event) # stuff that happens as a result of input
update_state() # stuff that happens regardless of input
update_display() # everything that changes the display (backlight, text, anything)

Wait for button click

I'm trying to create a GUI that will have some preset test to run. Depending on what the user's selection is a secession of test will run. I'm trying to figure out what is the best way to run a test in a thread and then wait until the user presses the next button to continue.
The current way the program knows what test to run is by create a dictionary like so
A = {0:[0,0,0],1:[1,0,1],2:[0,1,1]}
the key would represent the index of a combo box and the list represent whether the test runs or not, so 0 mean don't run that particular test and 1 mean do. So, I would have a for loop that would run through the list and if it's 0 it goes to the next list element, and if it's 1 it configures the test runs it and then I would like it to wait until the user presses the next button in the GUI.
EDIT: Instead I implemented a state machine method, using Qtimer. So the GUI will stay in the wait state until the GUI send sends a signal to move from the wait sate to the next state after a button has been pressed
You can do it simple like this: first, disable next button in your GUI using yourNextButton.setDisabled(1); second, enable it at the end of your test yourNextButton.setDisabled(0) (I assume it is one method); with the button enabled,which means your test is done, you can click it and connect it with next operation you need to be done (next test or whatever), but do not forget to disable it again when it's clicked.
If you need different behavior feel free to ask.

Categories

Resources