兎の真似をする烏

全力で"楽"である為に・人生が"面白く"ある為に

wxPythonでコンボボックスを使うには

やりたいこと

wxPythonでコンボボックスを使用したい。
画面生成にはwxGladeをりようする。

試してみる

さくっと簡単な例を作成してみる。

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.9.9pre on Thu Oct 22 16:07:45 2020
#

import wx

# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class MainPanel(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MainPanel.__init__
        kwds["style"] = kwds.get("style", 0)
        wx.Panel.__init__(self, *args, **kwds)
        
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        
        self.combo_box_1 = wx.ComboBox(self, wx.ID_ANY, choices=[], style=wx.CB_DROPDOWN | wx.CB_READONLY)
        self.combo_box_1.SetFont(wx.Font(18, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_2.Add(self.combo_box_1, 1, wx.ALL, 10)

        self.combo_box_1.Append('First', 1)
        self.combo_box_1.Append('Second', 2)
        self.combo_box_1.Append('Third', 3)

        self.SetSizer(sizer_1)
        
        self.Layout()

        self.Bind(wx.EVT_COMBOBOX, self.combobox_event, self.combo_box_1)
        # end wxGlade

    def combobox_event(self, event):  # wxGlade: MainPanel.<event_handler>
        print('表示データ:{}'.format(self.combo_box_1.GetValue()))
        print('内部データ:{}'.format(
            self.combo_box_1.GetClientData(self.combo_box_1.GetSelection())))

# end of class MainPanel

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MainFrame.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((400, 300))
        self.SetTitle("frame")
        
        self.main_panel = MainPanel(self, wx.ID_ANY)
        self.Layout()
        # end wxGlade

# end of class MainFrame

class MyApp(wx.App):
    def OnInit(self):
        self.main_frame = MainFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.main_frame)
        self.main_frame.Show()
        return True

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

要素設置方法

コンボボックスに要素を設置するには Append を利用する。この時、表示データと内部データとを分けて設置することができる。ユーザに見せたい表示データを第1引数に、処理で使いたい内部データを第2引数にそれぞれ設定したら良い。

        self.combo_box_1.Append('First', 1)
        self.combo_box_1.Append('Second', 2)
        self.combo_box_1.Append('Third',3)

要素取得方法

ユーザがコンボボックスで選択した情報を取得する方法について。
表示データを取得するには GetValue() を使用する。
内部データを取得するには GetSelection() で選択位置を取得した後に GetClientData() を使用する。

        print('表示データ:{}'.format(self.combo_box_1.GetValue()))
        print('内部データ:{}'.format(self.combo_box_1.GetClientData(self.combo_box_1.GetSelection())))

読み込み専用の設定方法

wx.ComboBoxwx.Choicewx.TextCtrl の両方の機能を兼ね備えている。つまりはユーザがテキストを変更することができる。ユーザにテキスト変更をさせたくないのであれば、コンボボックスの生成時に wx.CB_READONLY を指定したらOK。

        self.combo_box_1 = wx.ComboBox(self, wx.ID_ANY, choices=[], style=wx.CB_DROPDOWN | wx.CB_READONLY)