psychopy.org | Reference | Downloads | Github

Retrieving window's size

Hello,

Is there a way to get the window’s size ?
psychopy.visual.Window ( size=800, 600,…)

How can I retrieve the size? There’s no standard getter functions such as getSize function?

The more pressing question is , if i do this on the JS side to retrieve the with and height, how can I access it on the python side using builder?
For example,
If a ‘Begin Experiment’ has the following:
[screenWidth,screenHeight]=psychoJS.window.size;

How can I access the screenWidth,screenHeight on the python side?

thanks

OS (e.g. Win10): mac sierra
PsychoPy version (e.g. 1.84.x): 2020.2.3
Standard Standalone? (y/n) If not then what?: built from scratch

Hi @Irfa_Nisar, this thread and this thread should help you.

psychoJS.window.size returns an array [w, h].

You can add a code component in the"Begin Experiment" tab:
JS code:

w = psychoJS.window.size[0];
h = psychoJS.window.size[1];

where ‘w = psychoJS.window.size[0]’ stores the first item in the array (which is width) as w, and ‘h = psychoJS.window.size[1]’ stores the second item in the array (height) as h, which are now variables that can be used elsewhere. Looks like this may return size in pixels though.

If the goal is to present consistently sized images across different monitor dimensions, you can incorporate @wakecarter’s credit card demo found here. This demo should be helpful for seeing how to store online screen size and access in python.

Cheers!

1 Like

Hello,

The second part of my question is saying precisely what you are suggesting i.e.
[screenWidth,screenHeight]=psychoJS.window.size;

The question is : can i use screenWidth,screenHeight in later routines on the python side ? Seems like i cannot because i do not see it in the *_lastrun.py file.

thanks

@Irfa_Nisar, just make variables in both py & js that record window size using the same name– that’ll allow you to call the information in Builder whether you’re running locally or online. See the window documentation to get the window size in py.

You can create variables, say screenWidth=0; before the experiment.
Then you can have this in the ‘Begin Experiment’,
screenWidth=psychoJS.window.size[0];

However, *_lastrun.py still only reports screenWidth=0; //what we set in the ‘Before Experiment’ section closer to the header section of the file.

It seems to me that perhaps JS and python segments are not linked? Or do I need to take a leap of faith and hope that python knows about it without ever having it documented out anywhere in the python segments?

thanks!

Hi @Irfa_Nisar, so when you run the study online, the only script that is used is the js script (py doesn’t get read). Similarly, when you run locally, the only script that is read is the python script (js doesn’t get looked at).

So to use js-created variables in Builder, what that really means is that you are saving a new variable in the js script, and when the js script is compiled it auto-translate all of the Builder components into js, and then your js code components are also incorporated into it.

It sounds like the goal is to call variables within Builder that refer to the online-window size-- so if you want this to work both offline (py script) and online (js script), you need to create a variable in both languages using the same name. I think the problem here is likely that in the “py” code, you’re using js syntax that py doesn’t know how to use: psychoJS.window.size.

You’ll want to refer to the window doc linked above & test, but for the psychopy code component you’ll need something like:

screenWidth=psychopy.visual.window.size[0]

or potentially:

win = psychopy.visual.Window
screenWidth=win.size[0]

+1. That’s what i figured too. You almost have to repeat the same step twice if the builder doesn’t not replicate for both py and js. python code components replicate for both , but this is not true for js.

Look at the code_JS section of my crib sheet (see pinned post in online category) for recommendations of how to get auto translated code components working in both Python and JavaScript.