Skip to content

Instantly share code, notes, and snippets.

@kohske
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohske/8ad5cb1d376581ff7335 to your computer and use it in GitHub Desktop.
Save kohske/8ad5cb1d376581ff7335 to your computer and use it in GitHub Desktop.
psychopyで日本語入力
#coding:utf-8
# http://www.s12600.net/psy/python/19-7.html
import wx
import psychopy.visual
import psychopy.core
questions = [
u'好きな食べ物はなんですか?',
u'嫌いな食べ物はなんですか?',
u'昨晩は何を食べましたか?',
]
appWidth = 300
appHeight = 100
class InputDlg(wx.App):
def OnInit(self):
self.frame = wx.Frame(None, style=wx.CAPTION)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.frame.SetSizer(self.sizer)
self.inputBox = wx.TextCtrl(self.frame)
self.sizer.Add(self.inputBox, 1, wx.EXPAND)
self.inputBox.Bind(wx.EVT_KEY_DOWN, self.OnKeyChar)
self.inputBox.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
self.buttonOK = wx.Button(self.frame)
self.buttonOK.SetLabel(u"OK")
self.buttonOK.Bind(wx.EVT_BUTTON, self.OnOKButton)
self.sizer.Add(self.buttonOK, 0, wx.ALIGN_RIGHT)
self.frame.SetTitle(u"回答入力ダイアログ")
w = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
h = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
self.frame.SetSize((appWidth, appHeight))
self.frame.Move((w/2-appWidth/2, h/2-appHeight/2+200))
return True
def OnOKButton(self, event):
self.ExitMainLoop()
def OnKeyUp(self, event):
mesg.setText(questions[iter]+u'\n\n'+dlg.inputBox.GetValue())
mesg.draw()
win.flip()
def OnKeyChar(self, event):
key = event.GetKeyCode()
if key in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):
self.ExitMainLoop()
else:
event.Skip()
win = psychopy.visual.Window(fullscr=True)
mesg = psychopy.visual.TextStim(win)
dlg = InputDlg(False)
dlg.frame.Show()
for iter in range(len(questions)):
mesg.setText(questions[iter]+'\n\n')
mesg.draw()
win.flip()
dlg.inputBox.SetValue('')
dlg.MainLoop()
mesg.setText(u'答えは「'+dlg.inputBox.GetValue()+u'」ですね\n記録しました')
mesg.draw()
win.flip()
#1.0秒待つ
psychopy.core.wait(1.0)
dlg.frame.Hide()
mesg.setText(u'終了しました。ご協力ありがとうございました。')
mesg.draw()
win.flip()
psychopy.core.wait(3.0)
win.close()
@kohske
Copy link
Author

kohske commented Feb 26, 2015

53行目を

        if event.ControlDown() and key in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):

にしとけば、一応長文入力も可能。Ctrl+Enterで入力終了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment