2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

wxpythonでGUIで入力した値を取得する

Last updated at Posted at 2021-02-16

psychopy で刺激を出すときに,刺激パラメータをGUIで記述できるようにしたのでメモ.
python は GUIの選択肢がいろいろあるが,psychopy を入れる際,wxpython がデフォルトでインストールされるはずなので利用する.

レイアウトについては最初理解できなくて苦しんだが,wx.GridSizer を使うことで Psychopy を使う人にはなじみ深い MATLAB/matplotlib の subplot と同様の感覚で使える.python の作法に明るくないのでコードは汚いが,下記コードで動作を確認した.
各GUIをリストに入れることであとでパラメータの追加などをやりやすくしたつもり.

import wx
class GUIinput:
    def __init__(self):
        self.frame = wx.Frame(None, -1, "textbox")
        self.frame.SetTitle('Exp. Params.')
        panel_ui = wx.Panel(self.frame, wx.ID_ANY, size=(400, 100)) 
        
        layout = wx.GridSizer(rows=6, cols=2, gap=(5, 5))
        labels = ['NI-dev ID', 'Durations[s]', 'ISI [s]', 'Init Wait [s]', 'RGB Channels']
        defaults = ['Dev2', '5,5,5,5', '0', '5', '0,0,1']
        
        self.frame.labels = []
        self.frame.tboxes = []
        for k, i in enumerate(labels):
            self.frame.labels.append(wx.StaticText(panel_ui, wx.ID_ANY, labels[k]))
            self.frame.tboxes.append(wx.TextCtrl(panel_ui, wx.ID_ANY, defaults[k]))
        
        # button        
        self.frame.btn = wx.Button(panel_ui, -1, 'Submit')
        self.frame.btn.Bind(wx.EVT_BUTTON, self.Clicked)
        
        # layout
        for k, i in enumerate(self.frame.labels):
            layout.Add(i)
            layout.Add(self.frame.tboxes[k])
        layout.Add(self.frame.btn, flag=wx.SHAPED)
        panel_ui.SetSizer(layout)

        self.frame.Show(True)
        self.frame.datas = []
    def Clicked(self,event):
        for i in self.frame.tboxes:
            self.frame.datas.append(i.GetValue())
        self.frame.Close(True)

app = wx.App()
guiobj = GUIinput()
app.SetTopWindow(guiobj.frame)
app.MainLoop()
print(guiobj.frame.datas)

image.png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?