Re: how does wxpython dynamically changes the content of Choice?

Tim Roberts <[email protected]>
Newsgroups gmane.comp.python.wxpython
Organization Providenza & Boekelheide, Inc.
Message-ID <[email protected]>
李进强 wrote:
> /       The problem is that there are two Choice drop-down boxes in 
> the UI, the content of the second box is initially empty, and when the 
> user selects an option in the first box, the corresponding option 
> appears in the second box。/
> /        For example, if the first box is month, the second box is 
> day, and the user selects January, then the second box has 1... . 31, 
> the user chooses February, the second frame is 1... 28。 /

Think about your problem in words.  When the user make a choice in the 
first box, you want to fill the second box.  So, you need to handle the 
EVT_CHOICE event, and in that event, you fetch the month they choice, 
clear out the days box, and fill it appropriately.

You might leave the "day" box disabled until they pick a month. Also, 
remember that you can't really fill in February unless you know the 
year.  Also remember that range(1,31) runs from 1 to 30.

So, like this:

import wx

days = [31,28,31,30,31,30,31,31,30,31,30,31]

class myFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__ ( self, None, id = wx.ID_ANY, size = wx.Size( 
500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

         self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )

         bSizer9 = wx.BoxSizer( wx.VERTICAL )

         m_staticText14 = wx.StaticText( self, wx.ID_ANY, u"select 
Month", wx.DefaultPosition, wx.DefaultSize, 0 )
         m_staticText14.Wrap( -1 )

         bSizer9.Add( m_staticText14, 0, 
wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

         # select month
         m_choice1Choices = 
["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
         self.m_choice1 = wx.Choice( self, wx.ID_ANY, 
wx.DefaultPosition, wx.DefaultSize, m_choice1Choices, 0 )
         self.m_choice1.SetSelection( 0 )
         self.m_choice1.Bind( wx.EVT_CHOICE, self.onChooseMonth )
         bSizer9.Add( self.m_choice1, 0, 
wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

         m_staticText15 = wx.StaticText( self, wx.ID_ANY, u"select Day", 
wx.DefaultPosition, wx.DefaultSize, 0 )
         m_staticText15.Wrap( -1 )

         bSizer9.Add( m_staticText15, 0, 
wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

         # select day
         self.m_choice2 = wx.Choice( self, wx.ID_ANY, 
wx.DefaultPosition, wx.DefaultSize, ['--'], 0 )
         bSizer9.Add( self.m_choice2, 0, 
wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

         self.SetSizer( bSizer9 )
         self.Layout()

         self.Centre( wx.BOTH )

     def onChooseMonth(self,evt):
         evt.Skip()
         month = self.m_choice1.GetSelection()
         self.m_choice2.Clear()
         for i in range(days[month]):
             self.m_choice2.Append( str(i+1) )

app = wx.App()
myFrame().Show()
app.MainLoop()

-- 
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.

-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/30e55aad-0d01-5695-d6dc-7bcf26fc47ca%40probo.com.
smime.p7s (application/pkcs7-signature, 3.3 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.