Saturday, June 09, 2012

Sample Python Code: Showing Combobox Selected Text

The following code shows how to retrieve the selected text from wx.ComboBox when a mouse click event is triggered.

------------------------------------------------------------------------------------------------------------------------------
import wx

class MyFrame(wx.Frame):
    def __init__(self, parent=None, id=-1, title=''):
        wx.Frame.__init__(self, parent, id, title,size=(200, 240))
        top = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        font = wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD)
        lb = wx.StaticText(top, -1,'static text')
        sizer.Add(lb)

        c1 = wx.StaticText(top, -1, 'Number:')
    self.s1 = c1.Label
        c1.SetFont(font)

        ct = wx.SpinCtrl(top, -1, '1', min=1, max=12)
        sizer.Add(c1)
        sizer.Add(ct)

        c2 = wx.StaticText(top, -1, 'Type:')
        c2.SetFont(font)
        self.cb = wx.ComboBox(top, -1, '',choices=('A', 'B', 'C','D', 'E', 'F'))

        sizer.Add(c2)
        sizer.Add(self.cb)

        showbtn = wx.Button(top)
        showbtn.SetLabel('Show Info')
        self.Bind(wx.EVT_BUTTON, self.ShowValue, showbtn)
        sizer.Add(showbtn)

        qb = wx.Button(top, -1, "QUIT")
        self.Bind(wx.EVT_BUTTON,lambda e: self.Close(True), qb)
        sizer.Add(qb)

        top.SetSizer(sizer)
        self.Layout()

    def TestMe(val, self, e):
        print val
   
    def ShowValue(self, e):
        wx.MessageBox('Show Value ...' + self.cb.GetStringSelection())
   
class MyApp(wx.App):
   def OnInit(self):
       frame = MyFrame(title="wxWidgets")
       frame.Show(True)
       self.SetTopWindow(frame)
       return True

app = MyApp()
app.MainLoop()

--------------------------------------------------------------------------------------------------------------------------
Based on code above, the key part is to declare a self.cb variable to hold a reference to the combobox. This is the easiest way to refer to the combobox.

Of course, this is something I found after I have searched around and tried a few ways ... this is something I figure out when I don't find any specific example after googling :)

Converting a Physical Linux to Virtual

Hmm ... I have done a lot of work on my Linux Lubuntu 15.10 with PHP and PostgreSQL and a few other things ... it is quite time-consuming to...