# Buggy code
def draw(self):
    """Draw the text to the back buffer"""
    # Border width
    self.box.setLineWidth(
        self.pallette["lineWidth"]
    )  # Use 1 as base if border width is none
    # self.borderWidth = self.box.lineWidth
    # Border colour
    self.box.setLineColor(self.pallette["lineColor"], colorSpace="rgb")
    # self.borderColor = self.box.lineColor
    # Background
    self.box.setFillColor(self.pallette["fillColor"], colorSpace="rgb")
    # self.fillColor = self.box.fillColor

    if self._needVertexUpdate:
        # print("Updating vertices...")
        self._updateVertices()
    if self.fillColor is not None or self.borderColor is not None:
        self.box.draw()

    # self.boundingBox.draw()  # could draw for debug purposes
    gl.glPushMatrix()
    self.win.setScale("pix")

    gl.glActiveTexture(gl.GL_TEXTURE0)
    gl.glBindTexture(gl.GL_TEXTURE_2D, self.glFont.textureID)
    gl.glEnable(gl.GL_TEXTURE_2D)
    gl.glDisable(gl.GL_DEPTH_TEST)

    gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
    gl.glEnableClientState(gl.GL_COLOR_ARRAY)
    gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
    gl.glEnableClientState(gl.GL_VERTEX_ARRAY)

    gl.glVertexPointer(2, gl.GL_DOUBLE, 0, self.verticesPix.ctypes)
    gl.glColorPointer(4, gl.GL_DOUBLE, 0, self._colors.ctypes)
    gl.glTexCoordPointer(2, gl.GL_DOUBLE, 0, self._texcoords.ctypes)

    self.shader.bind()
    self.shader.setInt("texture", 0)
    self.shader.setFloat("pixel", [1.0 / 512, 1.0 / 512])
    nVerts = len(self.text) * 4

    gl.glDrawArrays(gl.GL_QUADS, 0, nVerts)
    self.shader.unbind()

    # removed the colors and font texture
    gl.glDisableClientState(gl.GL_COLOR_ARRAY)
    gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY)
    gl.glDisableVertexAttribArray(1)
    gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

    gl.glActiveTexture(gl.GL_TEXTURE0)
    gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
    gl.glDisable(gl.GL_TEXTURE_2D)

    if self.hasFocus:  # draw caret line
        self.caret.draw()

    gl.glPopMatrix()


# Traceback (IndexError)
Traceback (most recent call last):
File "textbox_editable.py", line 67, in <module>
win.flip()
File "psychopy\psychopy\visual\window.py", line 1026, in flip
thisStim.draw()
File "psychopy\psychopy\visual\textbox2\textbox2.py", line 551, in draw
self.caret.draw()
File "psychopy\psychopy\visual\textbox2\textbox2.py", line 848, in draw
gl.glVertex2f(self.vertices[0, 0], self.vertices[0, 1])
File "psychopy\psychopy\visual\textbox2\textbox2.py", line 946, in vertices
chrVerts = textbox.vertices[range((ii-1) * 4, (ii-1) * 4 + 4)]
IndexError: index 1212 is out of bounds for axis 0 with size 1176

# Fixed code
def draw(self):
    """Draw the text to the back buffer"""
    # Border width
    self.box.setLineWidth(
        self.pallette["lineWidth"]
    )  # Use 1 as base if border width is none
    # self.borderWidth = self.box.lineWidth
    # Border colour
    self.box.setLineColor(self.pallette["lineColor"], colorSpace="rgb")
    # self.borderColor = self.box.lineColor
    # Background
    self.box.setFillColor(self.pallette["fillColor"], colorSpace="rgb")
    # self.fillColor = self.box.fillColor

    if self._needVertexUpdate:
        # print("Updating vertices...")
        self._updateVertices()
    if self.fillColor is not None or self.borderColor is not None:
        self.box.draw()

    # self.boundingBox.draw()  # could draw for debug purposes
    gl.glPushMatrix()
    self.win.setScale("pix")

    gl.glActiveTexture(gl.GL_TEXTURE0)
    gl.glBindTexture(gl.GL_TEXTURE_2D, self.glFont.textureID)
    gl.glEnable(gl.GL_TEXTURE_2D)
    gl.glDisable(gl.GL_DEPTH_TEST)

    gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
    gl.glEnableClientState(gl.GL_COLOR_ARRAY)
    gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
    gl.glEnableClientState(gl.GL_VERTEX_ARRAY)

    gl.glVertexPointer(2, gl.GL_DOUBLE, 0, self.verticesPix.ctypes)
    gl.glColorPointer(4, gl.GL_DOUBLE, 0, self._colors.ctypes)
    gl.glTexCoordPointer(2, gl.GL_DOUBLE, 0, self._texcoords.ctypes)

    self.shader.bind()
    self.shader.setInt("texture", 0)
    self.shader.setFloat("pixel", [1.0 / 512, 1.0 / 512])
    nVerts = len(self._text) * 4

    gl.glDrawArrays(gl.GL_QUADS, 0, nVerts)
    self.shader.unbind()

    # removed the colors and font texture
    gl.glDisableClientState(gl.GL_COLOR_ARRAY)
    gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY)
    gl.glDisableVertexAttribArray(1)
    gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

    gl.glActiveTexture(gl.GL_TEXTURE0)
    gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
    gl.glDisable(gl.GL_TEXTURE_2D)

    if self.hasFocus:  # draw caret line
        self.caret.draw()

    gl.glPopMatrix()