vendredi 5 août 2016

PythonMegaWidget (Pmw) ComboBox restreind à une liste de valeur

Bonjour a tous, j'essaye de restreindre la sélection de valeur d'un Pmw.ComboBox aux seules valeurs disponibles dans la liste. Voici comment j'ai utilisé extravalidators pour y arriver.
Code en beta mais fonctionnant correctement

Hello there, I'm trying to restrict the selected value of a Pmw.Combox to the only possible values in the list. Here how I did use the extravalidators to reach that goal.
Roughly beta code but worked fine

from tkinter import *
import Pmw

root = Tk()
root.option_readfile('optionDB')

...
...

    combovalues = ['v1','v2','v3'] 
    f = Frame( root  )
    l = Label( f, text="Combo", anchor=W, width=15 ).pack( side=LEFT, expand=N )
    def strictselect_validate(avalue):
        print( 'validate: %s' % avalue )
        if avalue in combovalues:
            return Pmw.OK
        elif any( [avalue in v for v in combovalues] ):
            return Pmw.PARTIAL
        else:
            return Pmw.ERROR
    def strictselect_stringtovalue(avalue): # Not used in this case
        print( 'stringtovalue: %s' % avalue )
        return None
    combobox2 = Pmw.ComboBox( f, labelpos='wn', listbox_width=24, dropdown=1, 
                        entry_bg='white', entry_fg='black',
                        scrolledlist_items=combovalues,
                        entryfield_extravalidators = { 'strictselect' : (strictselect_validate, strictselect_stringtovalue) }, 
                        entryfield_validate = { 'validator' : 'strictselect' } )
    combobox2.pack( side=LEFT, expand=N )
    fields.append( (combobox2, None) ) # the Field and the string variable
    f.pack( side=TOP, expand=N, fill=X )


Le truc: le Pmw.ComboBox utilise un Pmw.EntryField, il est donc possible d'utiliser les fonctionnalités de Pmw.EntryField

The Tip: the Pmw.ComboBox use a Pmw.EntryField, it is then possible to use the features of Pmw.EntryField

Aucun commentaire: