Chinese input error of TextBox2

Hi,

I’m trying to conduct an online experiment and I have to create procedures that can be run on both Windows and MacOS system computers. I use TextBox2 component to enable participants to write essays. The component works on a Windows system computer, even if I input multiple Chinese characters at the same time. But when I input multiple Chinese characters on a MacOS system computer, some errors happen.

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/pyglet/libs/darwin/cocoapy/runtime.py", line 1128, in objc_method
    result = f(py_self, *args)
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_textview.py", line 78, in insertText_
    self._window.dispatch_event("on_text", text)
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/pyglet/window/__init__.py", line 1333, in dispatch_event
    if EventDispatcher.dispatch_event(self, *args) != False:
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/pyglet/event.py", line 415, in dispatch_event
    if getattr(self, event_type)(*args):
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/visual/backends/pygletbackend.py", line 429, in onText
    currentEditable._onText(evt)
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/visual/textbox2/textbox2.py", line 966, in _onText
    self.addCharAtCaret(chr)
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/visual/textbox2/textbox2.py", line 410, in addCharAtCaret
    self._layout()
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/visual/textbox2/textbox2.py", line 483, in _layout
    if self._styles[i] == NONE:
IndexError: list index out of range
Traceback (most recent call last):
  File "/Users/weiziqian/OneDrive/Documents/py_project/exp1/writing_control/exp_MacOS/main.py", line 52, in <module>
    run_writing_task()
  File "/Users/weiziqian/OneDrive/Documents/py_project/exp1/writing_control/exp_MacOS/writing_task.py", line 155, in run_writing_task
    words = removePunctuation(textbox.text)
  File "/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/visual/textbox2/textbox2.py", line 360, in text
    if styles[i] == ITALIC and lastFormatter != styles[i]:
IndexError: list index out of range

In addition, it seems TextBox2 cannot insert line break automatically.

Any help is appreciated.

Computer system: MacOS Monterey 12.0.1
PsychoPy version: StandalonePsychoPy-2021.2.3-macOS

您好~请问怎样设置在textbox控件中可以显示输入的中文呀

Hi, you can change font to Hiragino Sans GB or Arial Unicode MS. You might find useful information on [chinese mac] Fonts

We made some substantial changes to TextBox2 in v2022.1.0, particularly in the _layout method where your error is happening, so it may be worth upgrading to the latest version. If you do so, does this still occur?

Thanks for the information. I have downloaded the latest version but the same error is still occurring in the MacOS system. Fortunately, my scripts work fine in the Windows system. So I think this error might just be a computer system issue.

1 Like

Interesting - I’m not surprised that the call to removePunctuation causes problems on Mac as Mac’s do some smart substitution which can mess with that sort of thing, but it’s strange that the ._styles array is a different length between Mac and Windows. Could you share the specific characters you’re using which cause the error? We have a Mac in the office so I’d like to test this out and see what’s going on

Hi, I’m glad to help. It looks like whenever you try to type multiple Chinese characters the issue will occur. For example, you can try to type 计算机 (the input code is jisuanji). Before trying this, you need to set up a Chinese input source on your Mac.