Hello,

I have this problem. I build a list of elements, and I never know in
advance how many or what will be the elements.

I then wish to populate an OptionMenu with this list of elements.

How can I do that?

The reason I'm asking is I'm building a little GUI to enter records in
a SQL table. I want to give the user the choice of tables (with the
OptionMenu), however we never know in advance the menus there will be
in a given database.


My current code below. It all starts with the last function, enterAsset().


# -----------------------------------------------------
# Table filling
# -----------------------------------------------------


def getTables():
        
        """ Get the list of tables from the database. """
        
        oConnection = connect2db()
        oCursor = oConnection.cursor()
        
        oResult = oCursor.execute( "SHOW TABLES" )
        aRows = oCursor.fetchall()
        aTables = [ tRow[0] for tRow in aRows ]
        
        oCursor.close()
        oConnection.close()

        return aTables


        
        
def getTableColumns( sTableName ):
        
        """ Lists and returns the list of columns from a given table. """
        
        oConnection = connect2db()
        oCursor = oConnection.cursor()
        
        oResult = oCursor.execute( "SHOW COLUMNS FROM " + sTableName )
        aRows = oCursor.fetchall()
        aColumns = [ aRow[0] for aRow in aRows if aRow[0] != 'ID' ]
        
        oCursor.close()
        oConnection.close()
        
        return aColumns




# ------------------------
# Tkinter functions

        

def fetchAndDestroy():
        
        """
        Retrieves the values entered in the input fields.
        Maps them in-place to the label keys dictionary.
        Terminates the input field gui.
        """

        for oEntry in aEntries:
                sEntry = oEntry.get()
                if sEntry == '': sEntry = 'null'
                dEntryValues.setdefault( str(oEntry), [] ).append( sEntry )
        
        oRoot.destroy()




def makeformAsset( oRoot, aColumns ):
        
        """ Generates the input form, based on the provided list of table 
columns. """
        
        for sColumn in aColumns:
                
                oRow = Frame( oRoot )
                
                oLabel = Label( oRow, width = 25, text = sColumn, anchor = E )
                oEntry = Entry( oRow, width = 50, relief = RIDGE )
                dEntryValues.setdefault( str(oEntry), [] ).append( sColumn )
                
                oRow.pack( side = TOP, fill = X )
                oLabel.pack( side = LEFT )
                oEntry.pack( side = RIGHT, expand = YES, fill = X )
                
                aEntries.append( oEntry )


def enterAsset():
        
        """ Creates a form to fill in the info to create a new asset. """

        global dEntryValues
        global oRoot
        global aEntries
        
        dEntryValues = {}
        oRoot = Tk()
        aEntries = []
        
        # Get available tables
        aTables = getTables()
        
        var1 = StringVar()
        for sTable in aTables: oOption = OptionMenu( oRoot, var1, sTable )
        oOption.pack( fill = X )
        var1.set( aTables[0] )
        sTableName = var1.get()
        
        # Get columns for this table
        aColumns = getTableColumns( sTableName )

        makeformAsset( oRoot, aColumns )
        Button( oRoot, text = 'OK', command = ( fetchAndDestroy ) ).pack(
side = LEFT, expand = YES, fill = X )
        oRoot.mainloop()



Thanks
Bernard
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to