create and set default value of wx.Choice() using constructor

I'm sure I've seen this sort of thing when googling Python
ie
   some_object_variable = Class_a_constructor().Class_a_method()

which both creates an object & executes one of its methods
without needing a variable and having to do both in 2 statements
ie
some_object_variable = wx.Choice (
          parent=self,
          id=-1,
          choices=['fe','fi','fo']
         )
some_object_variable.SetStringSelection('fo')

I'd like to do the same sort of thing ie create a wx.Choice and set its
string all in one statement using SetStringSelection()
ie
      sz_main.Add(
         wx.Choice (
      parent=self,
      id=-1,
      choices=['fe','fi','fo']
       ).SetStringSelection('fo')
   ,0
         ,flag=wx.EXPAND
      )

but .SetStringSelection() is upsetting things.
Is there anyway I can set the default string whilst calling the
constructor?

I hope my question is clear

Use a ComboBox instead since wx.Choice doesn't have that as an option
in its constructor. Or you can do this:

some_object_variable = wx.Choice (
                                parent=panel,
                                id=-1,
                                choices=['fe','fi','fo']
                         ).SetStringSelection("fo")

···

On Mar 31, 5:43 am, me <mgbg25...@blueyonder.co.uk> wrote:

I'm sure I've seen this sort of thing when googling Python
ie
some_object_variable = Class_a_constructor().Class_a_method()

which both creates an object & executes one of its methods
without needing a variable and having to do both in 2 statements
ie
some_object_variable = wx.Choice (
parent=self,
id=-1,
choices=['fe','fi','fo']
)
some_object_variable.SetStringSelection('fo')

I'd like to do the same sort of thing ie create a wx.Choice and set its
string all in one statement using SetStringSelection()
ie
sz_main.Add(
wx.Choice (
parent=self,
id=-1,
choices=['fe','fi','fo']
).SetStringSelection('fo')
,0
,flag=wx.EXPAND
)

but .SetStringSelection() is upsetting things.
Is there anyway I can set the default string whilst calling the
constructor?

I hope my question is clear

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

after
some_object_variable = Class_a_constructor().Class_a_method()

some_object_variable will have the return of the method not the
object. In the case of SetStringSelection, a true or false.

My advice is let go of this idea and keep initialization and layout in
2 separate places. maybe even 2 separate methods that get called one
after the other in __init__

Alternatively, depending on the case, you could have an automatic
creation and layout.
This is accomplished by encoding the structure in a python object like
a list of lists or a list of dictionaries and having a small piece of
code parse the structure and create/layout the components.
To do this, use disposable variables within the algorithm and access
the active components by their name (which should be retrieved at
initialization from your custom structure)

To give you an example, I'll mutilate some sizers code from The Holy Demo.

This:
#---- orig----
    box3 = wx.BoxSizer(wx.VERTICAL)
    box3.AddMany([ (SampleWindow(win, "six"), 0, wx.EXPAND),
                   (SampleWindow(win, "seven"), 2, wx.EXPAND),
                   (SampleWindow(win, "eight"), 1, wx.EXPAND),
                   (SampleWindow(win, "nine"), 1, wx.EXPAND),
                   ])
#---- end orig----

becomes this:

#---- new1 ---
    custom_structure = [("six", 0), ("seven",2) , ("eight", 1), ("nine",1)]
    box3.AddMany([(SampleWindow(win, name), proportion , wx.EXPAND)
for (name, proportion) in custom_structure])
#--- end new1

or

#---- new 2
    custom_structure = [("six", 0), ("seven",2) , ("eight", 1), ("nine",1)]
    for name, prop in custom_structure:
        sample = SampleWindow(win, name)
        sample.SetBackground("pink")
        box3.Add(sample, prop, wx.EXPAND)
#---- end new 2

The second variant allows for customization of the controls that is
not possible in constructor.

You might also want to add a method like this one:

    def GetChildByName(self, name):
        for child in self.GetChildren():
            if child.GetName() == name:
                return child

This will help with accessing the objects by their name.

···

On Wed, Mar 31, 2010 at 1:43 PM, me <mgbg25171@blueyonder.co.uk> wrote:

I'm sure I've seen this sort of thing when googling Python
ie
some_object_variable = Class_a_constructor().Class_a_method()

which both creates an object & executes one of its methods
without needing a variable and having to do both in 2 statements
ie
some_object_variable = wx.Choice (
parent=self,
id=-1,
choices=['fe','fi','fo']
)
some_object_variable.SetStringSelection('fo')

I'd like to do the same sort of thing ie create a wx.Choice and set its
string all in one statement using SetStringSelection()
ie
sz_main.Add(
wx.Choice (
parent=self,
id=-1,
choices=['fe','fi','fo']
).SetStringSelection('fo')
,0
,flag=wx.EXPAND
)

but .SetStringSelection() is upsetting things.
Is there anyway I can set the default string whilst calling the
constructor?

I hope my question is clear

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, reply using "remove me" as the subject.

--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Thank you both very much.
I have quite a few options to explore above.
Rgds Dean

···

On Mar 31, 4:11 pm, Peter Damoc <pda...@gmail.com> wrote:

after
some_object_variable = Class_a_constructor().Class_a_method()

some_object_variable will have the return of the method not the
object. In the case of SetStringSelection, a true or false.

My advice is let go of this idea and keep initialization and layout in
2 separate places. maybe even 2 separate methods that get called one
after the other in __init__

Alternatively, depending on the case, you could have an automatic
creation and layout.
This is accomplished by encoding the structure in a python object like
a list of lists or a list of dictionaries and having a small piece of
code parse the structure and create/layout the components.
To do this, use disposable variables within the algorithm and access
the active components by their name (which should be retrieved at
initialization from your custom structure)

To give you an example, I'll mutilate some sizers code from The Holy Demo.

This:
#---- orig----
box3 = wx.BoxSizer(wx.VERTICAL)
box3.AddMany([ (SampleWindow(win, "six"), 0, wx.EXPAND),
(SampleWindow(win, "seven"), 2, wx.EXPAND),
(SampleWindow(win, "eight"), 1, wx.EXPAND),
(SampleWindow(win, "nine"), 1, wx.EXPAND),
])
#---- end orig----

becomes this:

#---- new1 ---
custom_structure = [("six", 0), ("seven",2) , ("eight", 1), ("nine",1)]
box3.AddMany([(SampleWindow(win, name), proportion , wx.EXPAND)
for (name, proportion) in custom_structure])
#--- end new1

or

#---- new 2
custom_structure = [("six", 0), ("seven",2) , ("eight", 1), ("nine",1)]
for name, prop in custom_structure:
sample = SampleWindow(win, name)
sample.SetBackground("pink")
box3.Add(sample, prop, wx.EXPAND)
#---- end new 2

The second variant allows for customization of the controls that is
not possible in constructor.

You might also want to add a method like this one:

def GetChildByName\(self, name\):
    for child in self\.GetChildren\(\):
        if child\.GetName\(\) == name:
            return child

This will help with accessing the objects by their name.

On Wed, Mar 31, 2010 at 1:43 PM, me <mgbg25...@blueyonder.co.uk> wrote:
> I'm sure I've seen this sort of thing when googling Python
> ie
> some_object_variable = Class_a_constructor().Class_a_method()

> which both creates an object & executes one of its methods
> without needing a variable and having to do both in 2 statements
> ie
> some_object_variable = wx.Choice (
> parent=self,
> id=-1,
> choices=['fe','fi','fo']
> )
> some_object_variable.SetStringSelection('fo')

> I'd like to do the same sort of thing ie create a wx.Choice and set its
> string all in one statement using SetStringSelection()
> ie
> sz_main.Add(
> wx.Choice (
> parent=self,
> id=-1,
> choices=['fe','fi','fo']
> ).SetStringSelection('fo')
> ,0
> ,flag=wx.EXPAND
> )

> but .SetStringSelection() is upsetting things.
> Is there anyway I can set the default string whilst calling the
> constructor?

> I hope my question is clear

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
> or visithttp://groups.google.com/group/wxPython-users?hl=en

> To unsubscribe, reply using "remove me" as the subject.

--
There is NO FATE, we are the creators.
blog:http://damoc.ro/