Problem with exam project

Hi, making exam project and keep running into the same error messages, my psychopy experiment stops after the dialog box, and cannot find the problem. i post my code and the error message that i get, hope someone can help :))

##import packages
from psychopy import visual, event, core, gui, data
import pandas as pd
import random
import glob

##create popup
dialog = gui.Dlg(title = “First Semester Exam”)
dialog.addField("Participant ID: ")
dialog.addField("Age: ")
dialog.addField("Gender: ", choices=[“Female”, “Male”, “Other”])
dialog.addField("Mood today (1 = in a very bad mood, 5 = in a very good mood): ", choices=[“1”, “2”, “3”, “4”, “5”]) #mood
dialog.addField(“Do you have English as your second language?”, choices=[“Yes”, “No”]) #language
dialog.show()

if dialog.OK:
ID = dialog.data[0]
age = dialog.data[1]
gender = dialog.data[2]
mood = dialog.data[3]
language = dialog.data[4]
elif dialog.Cancel:
core.quit()

##creating csv file
date = data.getDateStr()
logfile = pd.DataFrame(columns = [“id”, “age”, “gender”, “mood”, “language”, “happy/disgust”, “label/laden”, “word”, “primed/satiated”, “congruent/incongruent”, “response_time”]) #setting all columns for csv, are they good?

##setting up stopwatch and window and fixation cross
win = visual.Window(color=‘black’, size=(1920, 1080))
stopwatch = core.Clock()
fix_cross = visual.TextStim(win, text=‘+’, color=‘white’, height=40)

##getting in the pictures
happy_images = glob.glob(“stimuli/happy*.JPG”)
random.shuffle(happy_images)
disgust_images = glob.glob(“stimuli/disgust*.JPG”)
random.shuffle(disgust_images)

##function for showing words
def stimulus(word, repetition, images):
msg = visual.TextStim(win, text = word)
msg.draw()
win.flip()
core.wait(0.5)
win.flip()
core.wait(0.2)
for _ in range(repetition):
msg = visual.TextStim(win, text = word)
msg.draw()
win.flip()
core.wait(0.5)
win.flip()
core.wait(0.2)
fix_cross.draw()
win.flip()
core.wait(0.5)
pic = visual.ImageStim(win, image = images[0])
images.pop(0)
pic.draw()
key = event.waitKeys(keyList = [“1”, “9”])
response = stopwatch.getTime()
win.flip()
core.wait(0.8)
return response

##tryout
tryout = [“happyness”,30,“happy_images”]
stimulus(tryout[0], tryout[1], tryout[1])

#setting variables
emotion = “”
connection = “”
repetition = “”
matching = “”
if (tryout[2] == happy_images):
emotion = “happy”
else:
emotion = “disgust”
if (tryout[0] == “joy” or tryout[0] == “happyness” or tryout[0] == “disgust” or tryout[0] == “repulse”):
connection = “label”
else:
connection = “laden”
if (tryout[1] == 3):
repetition = “primed”
else:
repetition = “satiated”
if (((tryout[0] == “joy” or tryout[0] == “happyness” or tryout[0] == “celebration” or tryout[0] == “sunshine”) and tryout[2] == happy_images) or ((tryout[0] == “stink” or tryout[0] == “infection” or tryout[0] == “disgust” or tryout[0] == “repulse”) and tryout[2] == disgust_images)):
matching = “congruent”
else:
matching = “incongruent”

logfile = logfile.append({
“id”: ID,
“age”: age,
“gender”: gender,
“mood”: mood,
“language”: language,
“happy/disgust”: emotion,
“label/laden”: connection,
“word” : tryout[0],
“primed/satiated”: repetition,
“congruent/incongruent”: matching,
“response_time”: response}, ignore_index = True)

##ending
core.wait(2)
win.close()

##saving csv
logfile_name = “logfiles/logfile_{}_{}.csv”.format(ID, date)
logfile.to_csv(logfile_name)

and error message:

Welcome to PsychoPy3!
v2023.2.2
######## Running: C:\Users\sari\Documents\aarhus\cogcom\exam\chatgpt.py ########
pygame 2.1.0 (SDL 2.0.16, Python 3.8.10)
C:\Program Files\PsychoPy\lib\site-packages\scipy_init_.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.24.4
warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
Traceback (most recent call last):
Hello from the pygame community. Contribute - pygame wiki
8.3340 WARNING Monitor specification not found. Creating a temporary one…
File “C:\Users\sari\Documents\aarhus\cogcom\exam\chatgpt.py”, line 34, in
fix_cross = visual.TextStim(win, text=‘+’, color=‘white’, height=40)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\text.py”, line 230, in init
self.setText(text, log=False)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\text.py”, line 390, in setText
setAttribute(self, ‘text’, text, log)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\tools\attributetools.py”, line 134, in setAttribute
setattr(self, attrib, value)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\tools\attributetools.py”, line 27, in set
newValue = self.func(obj, value)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\text.py”, line 381, in text
self._setTextShaders(text)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\text.py”, line 399, in _setTextShaders
self.pygletTextObj = pyglet.text.Label(
File "C:\Program Files\PsychoPy\lib\site-packages\pyglet\text_init
.py", line 456, in init
self.document.set_style(0, len(self.document.text), {
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\text\document.py”, line 603, in set_style
return super(UnformattedDocument, self).set_style(
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\text\document.py”, line 512, in set_style
self.dispatch_event(‘on_style_text’, start, end, attributes)
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\event.py”, line 418, in dispatch_event
if handler(*args):
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\text\layout.py”, line 1073, in on_style_text
self._init_document()
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\text\layout.py”, line 1043, in _init_document
self._update()
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\text\layout.py”, line 966, in _update
lines = self._get_lines()
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\text\layout.py”, line 942, in _get_lines
glyphs = self._get_glyphs()
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\text\layout.py”, line 1085, in _get_glyphs
glyphs.extend(font.get_glyphs(text[start:end]))
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\font\base.py”, line 394, in get_glyphs
self.glyphs[c] = glyph_renderer.render(c)
File “C:\Program Files\PsychoPy\lib\site-packages\pyglet\font\win32.py”, line 444, in render
gdiplus.GdipGraphicsClear(self._graphics, 0x00000000)
OSError: exception: access violation writing 0x000001744631C340
################# Experiment ended with exit code 1 [pid:5996] #################

Hi. I am not an expert on PsychoPy, so bear with me.

So, it looks like you’re encountering an access violation error while trying to write to a specific memory address, which is indicated by the 0x000001744631C340 part of the error message. This could be caused by a number of different issues, such as trying to write to a read-only section of memory or attempting to access memory that has already been freed.

Are you just using the coding screen and not the builder? If so, this would explain your problem. Coder is not recommended as you can encounter serious issues, which are hard to solve. The advice I often see on here, and that has helped me, even for people who use PsychoPy often, is to use the builder screen and insert a code snippet into the code component at the right place (so, before the experiment, begin the experiment, begin routine, every frame, end routine, end experiment).

I recommend first trying that and redoing your experiment with the builder and inserting ‘special code’ via the code component and see how you are. If you are still having issues, come back again. You can still use the coding thread to talk about coding issues, as these code snippets are still code.

If you are new, I recommend checking PsychoPy YouTube channel with demos and the support documentation page to get you started with the builder.

Good Luck :slight_smile: