wxPython Pheonix 4.0.2 - question regarding MultiChoiceProperty and setting the selection of the choices

Dear All,

I have a question regarding the MultiChoiceProperty which I hope you can help with. I have PropertyGrid with two properties 1) EnumProperty and 2) MultiChoiceProperty (MCP). I bind to the EVT_PG_CHANGED event. What I would like to happen is that when a new item is selected in the EnumProperty I would like to change the list of choices displayed in the MultiChoiceProperty and have those new choices in the ‘selected state’ i.e. so that they appear in the property grid as items which have been selected.

As I mention I handle EVT_PG_CHANGED event…in this event handler I am able to successfully change the ‘choices’ of the MCP in response to a change in the EnumProperty selection but am unable to set the state of the ‘new choices’ of the MCP to ‘selected’. I had thought .SetChoiceSelection() method of MCP would work but it doesn’t seem to do anything (selection state is unchanged after the call and items do not appear in ProperyGrid and only seems to take a single integer as parameter whereas for multi select I would have thought a list would have been required.

My code looks like this (see attached also). I question therefore is…is this the correct method or is there an alternative ? I’m running on a Windows7 laptop with wxPython 4.0.0a1 , Python 3

Any thoughts would be appreciated.

regards

mike

test8.py (3.13 KB)

···

import wx

from wx import propgrid

from wx import adv

import inspect

import yaml

class Page1(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

self.panel = panel = wx.Panel(self, wx.ID_ANY)

topsizer = wx.BoxSizer(wx.VERTICAL)

self.pg = pg = propgrid.PropertyGrid(self,

style=propgrid.PG_SPLITTER_AUTO_CENTER |

propgrid.PG_TOOLBAR)

self.Bind(propgrid.EVT_PG_CHANGED, self.OnGridChangeEvent)

vals1=[‘x’,‘y’,‘z’]

ix = range(len(vals1))

pg.Append( propgrid.EnumProperty(“Enum”,‘enum’,vals1, ix, 0) )

vals2=[‘a’,‘b’,‘c’]

self.pg_mcp = pg_mcp = propgrid.MultiChoiceProperty(label=‘MCP’,

name=‘measurments’,

choices=vals2, value=vals2)

pg.Append(pg_mcp)

topsizer.Add(pg,1,wx.EXPAND,5)

self.SetSizer(topsizer)

def OnGridChangeEvent(self, event):

#grid = event.EventObject

prop = event.GetProperty()

if prop:

print (prop.GetName())

if (prop.GetName() == ‘enum’):

new_values = [‘A’,‘B’,‘C’]

newChoices=propgrid.PGChoices(new_values)

self.pg_mcp.SetChoices(newChoices)

#Problem begins here —PROBLEM BEGINS HERE

#The following statements doesn’t seem to do anything

#ideally I want all the new_values to be set to the “selected”

#state and appear as a list of items in the property grid

#at the moment despite this call the text displayed

#in the propperty grid for this item is blank

#and when you click on the button of the MultiChoiceProperty

#the new items are also not selected. How to set the new items

#to the selected state and have them appear in the text field of

#the property grid corresponding to the MultiChoiceProperty?

self.pg_mcp.SetChoiceSelection(1)

return

class MainFrame(wx.Frame):

def init(self, cfg=None, products=None, measurements=None):

wx.Frame.init(self, None, title="")

Here we create a panel and a notebook on the panel

p = wx.Panel(self)

nb = wx.Notebook§

create the page windows as children of the notebook

page1 = Page1(nb)

add the pages to the notebook with the label to show on the tab

nb.AddPage(page1, “Input Datasets”)

sizer = wx.BoxSizer(wx.HORIZONTAL)

sizer.Add(nb, 1, wx.EXPAND)

p.SetSizer(sizer)

self.Center()

class MyApp(wx.App):

def OnInit(self):

self.frame = MainFrame()

self.frame.Show()

return True

if name == “main”:

app = MyApp(False)

app.MainLoop()

Hi Mike,

Did you ever find a solution for your question?

UPDATE:
So I took a closer look and here’s what I came up with. I get it, this is a 3-year-old post that I’m commenting on, but I wanted to leave some breadcrumbs behind for others. I took some liberties in reformatting some of your original work for readability.

import wx
from wx import adv
import inspect
import yaml
import wx
import wx.grid
import wx.propgrid as wxpg


class Page1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.panel = wx.Panel(self, wx.ID_ANY)
        topsizer = wx.BoxSizer(wx.VERTICAL)
        self.property_grid_1 = pg = wxpg.PropertyGrid(self, style=wxpg.PG_SPLITTER_AUTO_CENTER | wxpg.PG_TOOLBAR)
        self.Bind(wxpg.EVT_PG_CHANGED, self.OnGridChangeEvent)

        vals1 = ['x', 'y', 'z']
        vals2 = ['a', 'b', 'c']
        ix = range(len(vals1))
        pg.Append(wxpg.EnumProperty("Enum", 'enum', vals1, ix, 0))
        pg.Append(wxpg.MultiChoiceProperty(label='MCP', name='measurments', choices=vals2, value=vals2))

        topsizer.Add(pg, 1, wx.EXPAND, 5)
        self.SetSizer(topsizer)

    def OnGridChangeEvent(self, event):
        pg = self.property_grid_1
        prop = event.GetProperty()
        if prop:
            print(prop.GetName())
            if (prop.GetName() == 'enum'):
                new_values = ['A', 'B', 'C']
                pg.GetProperty('measurments').SetChoices(wxpg.PGChoices(new_values))
                pg.SetPropertyValue('measurments', [new_values[1], new_values[2]])
        return

So what’s going on here? I provide both an enum and multichoiceproperty example below with included comments.

pg = self.property_grid
pg.Append(wxpg.EnumProperty(label="X Data",         name="X Data", labels=['NaN'],  values=[0]))
pg.Append(wxpg.MultiChoiceProperty(label="Y Data",  name='Y Data', choices=['NaN'], value=['NaN']))

# set choices
choices = ['A', 'B', 'C']
pg.GetProperty('X Data').SetChoices(wxpg.PGChoices(choices))
pg.GetProperty('Y Data').SetChoices(wxpg.PGChoices(choices))

# set selection is assigned based on new string matching choice string list
pg.SetPropertyValue('X Data', choices[0])
pg.SetPropertyValue('Y Data', [choices[1]])

# retrieve data
x = pg.GetPropertyValueAsString('X Data')
y = pg.GetPropertyValue('Y Data') # includes multiple selections

# other approaches of assigning x:
# x = pg.GetProperty('X Data').GetValueAsString()  # returns the label
# x = pg.GetProperty('X Data').GetValue()  # returns integer instead of the label