a
     ˜ëj°  ã                   @   s`   d Z ddlZddlmZmZmZmZmZ G dd„ deƒZG dd„ dƒZ	dd	„ Z
ed
kr\e
ƒ  dS )zC
Tkinter widgets for displaying multi-column listboxes and tables.
é    N)ÚFrameÚLabelÚListboxÚ	ScrollbarÚTkc                	   @   sÔ  e Zd ZdZedddd�Zeddddd	d
�Zedddddddd�Zdi fdd„Zdd„ Z	dd„ Z
dd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zd d!„ Zd"d#„ Zdbd$d%„Zi fd&d'„Zd(d)„ Zi fd*d+„Zi fd,d-„Zdcd.d/„Zd0d1„ Zddd2d3„Zd4d5„ Zd6d7„ Zd8d9„ Zded:d;„Zdfd<d=„Zdgd>d?„Z d@dA„ Z!dBdC„ Z"dDdE„ Z#dFdG„ Z$dHdI„ Z%dJdK„ Z&dLdM„ Z'dNdO„ Z(dPdQ„ Z)dRdS„ Z*dTdU„ Z+dVdW„ Z,dXdY„ Z-dZd[„ Z.d\d]„ Z/d^d_„ Z0d`da„ Z1eZ2eZ3eZ4e,Z5e-Z6e"Z7e.Z8dS )hÚMultiListboxa§  
    A multi-column listbox, where the current selection applies to an
    entire row.  Based on the MultiListbox Tkinter widget
    recipe from the Python Cookbook (https://code.activestate.com/recipes/52266/)

    For the most part, ``MultiListbox`` methods delegate to its
    contained listboxes.  For any methods that do not have docstrings,
    see ``Tkinter.Listbox`` for a description of what that method does.
    z#888Té   )Ú
backgroundÚ	takefocusÚhighlightthicknessZraisedzhelvetica -16 boldz#444Zwhite)ÚborderwidthZreliefÚfontr	   Ú
foregroundr   FÚnone)r   Zselectborderwidthr   ZexportselectionÚselectbackgroundZactivestyler
   Nc                    sX  t |tƒrtt|ƒƒ}d}nd}t|ƒdkr4tdƒ‚t|ƒˆ _g ˆ _g ˆ _	|du rbdgt|ƒ }nt|ƒt|ƒkrztdƒ‚|ˆ _
tjˆ |fi ˆ j¤Ž ˆ jddd� tˆ jƒD �]2\}}ˆ j||| d� |�rtˆ fd	|iˆ j¤Ž}	ˆ j	 |	¡ |	j|dd
ddd� ||	_tˆ fi ˆ j¤Ž}
ˆ j |
¡ |
j|dd
ddd� ||
_|
 dˆ j¡ |
 dˆ j¡ |
 d‡ fdd„¡ |
 d‡ fdd„¡ |
 d‡ fdd„¡ |
 d‡ fdd„¡ |
 d‡ fdd„¡ |
 ddd„ ¡ |
 dˆ j¡ q®ˆ  dˆ j¡ ˆ  d‡ fdd„¡ ˆ  d‡ fdd„¡ ˆ  d‡ fd d„¡ ˆ  d!‡ fd"d„¡ ˆ j|fi |¤Ž dS )#aŽ  
        Construct a new multi-column listbox widget.

        :param master: The widget that should contain the new
            multi-column listbox.

        :param columns: Specifies what columns should be included in
            the new multi-column listbox.  If ``columns`` is an integer,
            then it is the number of columns to include.  If it is
            a list, then its length indicates the number of columns
            to include; and each element of the list will be used as
            a label for the corresponding column.

        :param cnf, kw: Configuration parameters for this widget.
            Use ``label_*`` to configure all labels; and ``listbox_*``
            to configure all listboxes.  E.g.:
                >>> root = Tk()  # doctest: +SKIP
                >>> MultiListbox(root, ["Subject", "Sender", "Date"], label_foreground='red').pack()  # doctest: +SKIP
        FTr   zExpected at least one columnNr   z*Expected one column_weight for each column©ÚweightÚtextÚnews©ÚcolumnÚrowZstickyZpadxZpadyú
<Button-1>z<B1-Motion>z
<Button-4>c                    s
   ˆ   d¡S )Néÿÿÿÿ©Ú_scroll©Úe©Úself© úN/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/draw/table.pyÚ<lambda>   ó    z'MultiListbox.__init__.<locals>.<lambda>z
<Button-5>c                    s
   ˆ   d¡S )Nr   r   r   r   r    r!   r"   €   r#   z<MouseWheel>c                    s   ˆ   | j¡S ©N)r   Údeltar   r   r    r!   r"   �   r#   z
<Button-2>c                    s   ˆ   | j| j¡S r$   )Ú	scan_markÚxÚyr   r   r    r!   r"   ƒ   r#   z<B2-Motion>c                    s   ˆ   | j| j¡S r$   )Úscan_dragtor'   r(   r   r   r    r!   r"   „   r#   z
<B1-Leave>c                 S   s   dS )NÚbreakr    r   r    r    r!   r"   ‡   r#   z<Up>c                    s   ˆ j dd�S )Nr   ©r%   ©Úselectr   r   r    r!   r"   �   r#   z<Down>c                    s   ˆ j dd�S )Nr   r+   r,   r   r   r    r!   r"   ‘   r#   z<Prior>c                    s   ˆ j ˆ  ¡  d�S ©Nr+   ©r-   Ú	_pagesizer   r   r    r!   r"   ’   r#   z<Next>c                    s   ˆ j ˆ  ¡ d�S r.   r/   r   r   r    r!   r"   “   r#   )Ú
isinstanceÚintÚlistÚrangeÚlenÚ
ValueErrorÚtupleÚ_column_namesÚ
_listboxesÚ_labelsÚ_column_weightsr   Ú__init__ÚFRAME_CONFIGZgrid_rowconfigureÚ	enumerateÚgrid_columnconfigurer   ÚLABEL_CONFIGÚappendÚgridÚcolumn_indexr   ÚLISTBOX_CONFIGÚbindÚ_selectÚ_resize_columnÚ	configure)r   ÚmasterÚcolumnsÚcolumn_weightsÚcnfÚkwZinclude_labelsÚiÚlabelÚlÚlbr    r   r!   r<   >   sT    

zMultiListbox.__init__c                 C   sÚ   |j  d¡rdS d| _|j | u rZt| jƒD ],\}}t|j| ¡ | ¡   ƒdk r*|| _q*nD|j|j  ¡ d krz|j j	| _n$|jdk rž|j j	dkrž|j j	d | _| jdurÒ|j  d| j
¡ |j  d	|j | j¡ d
S dS dS )zô
        Callback used to resize a column of the table.  Return ``True``
        if the column is actually getting resized (if the user clicked
        on the far left or far right 5 pixels of a label); and
        ``False`` otherwies.
        z<ButtonRelease>FNé
   é   r   r   ú<Motion>ú<ButtonRelease-%d>T)ÚwidgetrE   Ú_resize_column_indexr>   r9   Úabsr'   Úwinfo_xÚwinfo_widthrC   Ú_resize_column_motion_cbÚnumÚ_resize_column_buttonrelease_cb)r   ÚeventrN   rQ   r    r    r!   rG   œ   s$    	


ÿzMultiListbox._resize_columnc                 C   sb   | j | j }| ¡ |d  }|j|j ¡  }| ¡ | ¡  }tdt|d || |  ƒƒ|d< d S )NÚwidthé   )r9   rW   rZ   r'   rV   rY   Úmaxr2   )r   r^   rQ   Z	charwidthÚx1Zx2r    r    r!   r[   ½   s
    z%MultiListbox._resize_column_motion_cbc                 C   s"   |j  d|j ¡ |j  d¡ d S )NrU   rT   )rV   Zunbindr\   )r   r^   r    r    r!   r]   Æ   s    z,MultiListbox._resize_column_buttonrelease_cbc                 C   s   | j S )zh
        A tuple containing the names of the columns used by this
        multi-column listbox.
        )r8   r   r    r    r!   Úcolumn_namesÎ   s    zMultiListbox.column_namesc                 C   s
   t | jƒS )aÉ  
        A tuple containing the ``Tkinter.Label`` widgets used to
        display the label of each column.  If this multi-column
        listbox was created without labels, then this will be an empty
        tuple.  These widgets will all be augmented with a
        ``column_index`` attribute, which can be used to determine
        which column they correspond to.  This can be convenient,
        e.g., when defining callbacks for bound events.
        )r7   r:   r   r    r    r!   Úcolumn_labelsÖ   s    zMultiListbox.column_labelsc                 C   s
   t | jƒS )aY  
        A tuple containing the ``Tkinter.Listbox`` widgets used to
        display individual columns.  These widgets will all be
        augmented with a ``column_index`` attribute, which can be used
        to determine which column they correspond to.  This can be
        convenient, e.g., when defining callbacks for bound events.
        )r7   r9   r   r    r    r!   Ú	listboxesã   s    	zMultiListbox.listboxesc                 C   s:   |j  |j¡}|  dd¡ |  |¡ |  |¡ |  ¡  d S )Nr   Úend)rV   Únearestr(   Úselection_clearÚselection_setÚactivateÚfocus)r   r   rN   r    r    r!   rF   ò   s
    

zMultiListbox._selectc                 C   s   | j D ]}| |d¡ qdS )NÚunitr*   ©r9   Úyview_scroll)r   r%   rQ   r    r    r!   r   ù   s    
zMultiListbox._scrollc                 C   s   t |  d¡ƒt |  d¡ƒ S )z2:return: The number of rows that makes up one pagez
@0,1000000z@0,0)r2   Úindexr   r    r    r!   r0   þ   s    zMultiListbox._pagesizec                 C   s–   |dur|durt dƒ‚|durNt|  ¡ ƒdkr:d| }nt|  ¡ d ƒ| }|  dd¡ |dur’tt|dƒ|  ¡ d ƒ}|  |¡ |r’|  	|¡ dS )aÒ  
        Set the selected row.  If ``index`` is specified, then select
        row ``index``.  Otherwise, if ``delta`` is specified, then move
        the current selection by ``delta`` (negative numbers for up,
        positive numbers for down).  This will not move the selection
        past the top or the bottom of the list.

        :param see: If true, then call ``self.see()`` with the newly
            selected index, to ensure that it is visible.
        Nz$specify index or delta, but not bothr   r   rf   r   )
r6   r5   Úcurselectionr2   rh   Úminra   Úsizeri   Úsee©r   ro   r%   rs   r    r    r!   r-     s    

zMultiListbox.selectc                 K   s¶   t t| ¡ ƒt| ¡ ƒ ƒ}t| ¡ ƒD ]ˆ\}}| d¡sD| d¡rh| jD ]}| |dd… |i¡ qJq(| d¡s|| d¡r | jD ]}| |dd… |i¡ q‚q(t | ||i¡ q(dS )a¢  
        Configure this widget.  Use ``label_*`` to configure all
        labels; and ``listbox_*`` to configure all listboxes.  E.g.:

                >>> master = Tk()  # doctest: +SKIP
                >>> mlb = MultiListbox(master, 5)  # doctest: +SKIP
                >>> mlb.configure(label_foreground='red')  # doctest: +SKIP
                >>> mlb.configure(listbox_foreground='red')  # doctest: +SKIP
        Zlabel_zlabel-é   NZlistbox_zlistbox-é   )Údictr3   ÚitemsÚ
startswithr:   rH   r9   r   )r   rL   rM   ÚkeyÚvalrO   Úlistboxr    r    r!   rH   *  s    


zMultiListbox.configurec                 C   s   |   ||i¡ dS )z|
        Configure this widget.  This is equivalent to
        ``self.configure({key,val``)}.  See ``configure()``.
        N)rH   )r   rz   r{   r    r    r!   Ú__setitem__?  s    zMultiListbox.__setitem__c                 K   s$   | j D ]}|j||fi |¤Ž qdS )zÇ
        Configure all table cells in the given row.  Valid keyword
        arguments are: ``background``, ``bg``, ``foreground``, ``fg``,
        ``selectbackground``, ``selectforeground``.
        N©r9   Úitemconfigure)r   Ú	row_indexrL   rM   rQ   r    r    r!   ÚrowconfigureF  s    
zMultiListbox.rowconfigurec                 K   sz   | j | }tt| ¡ ƒt| ¡ ƒ ƒ}t| ¡ ƒD ]B\}}|dv rft| ¡ ƒD ]}| |||i¡ qNq2| ||i¡ q2dS )zÊ
        Configure all table cells in the given column.  Valid keyword
        arguments are: ``background``, ``bg``, ``foreground``, ``fg``,
        ``selectbackground``, ``selectforeground``.
        )r	   Úbgr   Zfgr   ÚselectforegroundN)r9   rw   r3   rx   r4   rr   r   rH   )r   Ú	col_indexrL   rM   rQ   rz   r{   rN   r    r    r!   ÚcolumnconfigureO  s    
zMultiListbox.columnconfigurec                 K   s   | j | }|j||fi |¤ŽS )zÑ
        Configure the table cell at the given row and column.  Valid
        keyword arguments are: ``background``, ``bg``, ``foreground``,
        ``fg``, ``selectbackground``, ``selectforeground``.
        r~   )r   r€   r„   rL   rM   rQ   r    r    r!   r   f  s    
zMultiListbox.itemconfigurec                 G   sX   |D ]}t |ƒt | jƒkrtdƒ‚qt| jtt|Ž ƒƒD ]\}}|j|g|¢R Ž  q8dS )a0  
        Insert the given row or rows into the table, at the given
        index.  Each row value should be a tuple of cell values, one
        for each column in the row.  Index may be an integer or any of
        the special strings (such as ``'end'``) accepted by
        ``Tkinter.Listbox``.
        zDrows should be tuples whose length is equal to the number of columnsN)r5   r8   r6   Úzipr9   r3   Úinsert)r   ro   ÚrowsÚeltrQ   Úeltsr    r    r!   r‡   s  s    ÿzMultiListbox.insertc                    s8   ‡ ‡fdd„| j D ƒ}ˆr,dd„ t|Ž D ƒS t|ƒS dS )a  
        Return the value(s) of the specified row(s).  If ``last`` is
        not specified, then return a single row value; otherwise,
        return a list of row values.  Each row value is a tuple of
        cell values, one for each column in the row.
        c                    s   g | ]}|  ˆ ˆ¡‘qS r    )Úget)Ú.0rQ   ©ÚfirstÚlastr    r!   Ú
<listcomp>‹  r#   z$MultiListbox.get.<locals>.<listcomp>c                 S   s   g | ]}t |ƒ‘qS r    )r7   ©rŒ   r   r    r    r!   r�   �  r#   N)r9   r†   r7   )r   rŽ   r�   Úvaluesr    r�   r!   r‹   „  s    zMultiListbox.getc           
      C   sZ   | j d|d�\}}}}| j|  |¡\}}}}	t|ƒt|ƒ t|ƒt|ƒ t|ƒt|	ƒfS )zÇ
        Return the bounding box for the given table cell, relative to
        this widget's top-left corner.  The bounding box is a tuple
        of integers ``(left, top, width, height)``.
        r   )r   r   )Z	grid_bboxr9   Úbboxr2   )
r   r   ÚcolZdxZdyÚ_r'   r(   ÚwÚhr    r    r!   r“   ‘  s    zMultiListbox.bboxc                 C   s4   | j r| j |  ¡  | j|  ¡  | j|dd� dS )a1  
        Hide the given column.  The column's state is still
        maintained: its values will still be returned by ``get()``, and
        you must supply its values when calling ``insert()``.  It is
        safe to call this on a column that is already hidden.

        :see: ``show_column()``
        r   r   N)r:   Zgrid_forgetre   r?   )r   r„   r    r    r!   Úhide_columnŸ  s    	zMultiListbox.hide_columnc                 C   sV   | j | }| jr*| j| j|ddddd� | j| j|ddddd� | j||d� dS )z�
        Display a column that has been hidden using ``hide_column()``.
        It is safe to call this on a column that is not hidden.
        r   r   r   r   r   N)r;   r:   rB   r9   r?   )r   r„   r   r    r    r!   Úshow_column­  s    


ÿ

ÿzMultiListbox.show_columnc                    s   ‡ ‡‡fdd„| j D ƒS )aK  
        Add a binding to each ``Tkinter.Label`` widget in this
        mult-column listbox that will call ``func`` in response to the
        event sequence.

        :return: A list of the identifiers of replaced binding
            functions (if any), allowing for their deletion (to
            prevent a memory leak).
        c                    s   g | ]}|  ˆˆˆ ¡‘qS r    )rE   )rŒ   rO   ©ÚaddÚfuncÚsequencer    r!   r�   Ê  r#   z/MultiListbox.bind_to_labels.<locals>.<listcomp>)rd   ©r   r�   rœ   r›   r    rš   r!   Úbind_to_labelsÀ  s    
zMultiListbox.bind_to_labelsc                 C   s   | j D ]}| |||¡ qdS )aM  
        Add a binding to each ``Tkinter.Listbox`` widget in this
        mult-column listbox that will call ``func`` in response to the
        event sequence.

        :return: A list of the identifiers of replaced binding
            functions (if any), allowing for their deletion (to
            prevent a memory leak).
        N)re   rE   )r   r�   rœ   r›   r|   r    r    r!   Úbind_to_listboxesÌ  s    

zMultiListbox.bind_to_listboxesc                 C   s   |   |||¡|  |||¡ S )ac  
        Add a binding to each ``Tkinter.Label`` and ``Tkinter.Listbox``
        widget in this mult-column listbox that will call ``func`` in
        response to the event sequence.

        :return: A list of the identifiers of replaced binding
            functions (if any), allowing for their deletion (to
            prevent a memory leak).
        )rŸ   r    rž   r    r    r!   Úbind_to_columnsÙ  s    
ÿzMultiListbox.bind_to_columnsc                 O   s   | j d j|i |¤ŽS ©Nr   )r9   rp   ©r   ÚargsÚkwargsr    r    r!   rp   ì  s    zMultiListbox.curselectionc                 O   s   | j d j|i |¤ŽS r¢   )r9   Úselection_includesr£   r    r    r!   r¦   ï  s    zMultiListbox.selection_includesc                 O   s   | j d j|i |¤ŽS r¢   )r9   Úitemcgetr£   r    r    r!   r§   ò  s    zMultiListbox.itemcgetc                 O   s   | j d j|i |¤ŽS r¢   )r9   rr   r£   r    r    r!   rr   õ  s    zMultiListbox.sizec                 O   s   | j d j|i |¤ŽS r¢   )r9   ro   r£   r    r    r!   ro   ø  s    zMultiListbox.indexc                 O   s   | j d j|i |¤ŽS r¢   )r9   rg   r£   r    r    r!   rg   û  s    zMultiListbox.nearestc                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   rj   ©r   r¤   r¥   rQ   r    r    r!   rj   ÿ  s    
zMultiListbox.activatec                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   Údeleter¨   r    r    r!   r©     s    
zMultiListbox.deletec                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   r&   r¨   r    r    r!   r&     s    
zMultiListbox.scan_markc                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   r)   r¨   r    r    r!   r)     s    
zMultiListbox.scan_dragtoc                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   rs   r¨   r    r    r!   rs     s    
zMultiListbox.seec                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   Úselection_anchorr¨   r    r    r!   rª     s    
zMultiListbox.selection_anchorc                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   rh   r¨   r    r    r!   rh     s    
zMultiListbox.selection_clearc                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   ri   r¨   r    r    r!   ri     s    
zMultiListbox.selection_setc                 O   s    | j D ]}|j|i |¤Ž}q|S r$   )r9   Úyview)r   r¤   r¥   rQ   Úvr    r    r!   r«     s    
zMultiListbox.yviewc                 O   s    | j D ]}|j|i |¤Ž qd S r$   )r9   Úyview_movetor¨   r    r    r!   r­   $  s    
zMultiListbox.yview_movetoc                 O   s    | j D ]}|j|i |¤Ž qd S r$   rm   r¨   r    r    r!   rn   (  s    
zMultiListbox.yview_scroll)NNT)N)N)NNN)NNN)NNN)9Ú__name__Ú
__module__Ú__qualname__Ú__doc__rw   r=   r@   rD   r<   rG   r[   r]   Úpropertyrc   rd   re   rF   r   r0   r-   rH   r}   r�   r…   r   r‡   r‹   r“   r˜   r™   rŸ   r    r¡   rp   r¦   r§   rr   ro   rg   rj   r©   r&   r)   rs   rª   rh   ri   r«   r­   rn   Ú
itemconfigÚ	rowconfigÚcolumnconfigZselect_anchorZselect_clearZselect_includesZ
select_setr    r    r    r!   r      sŠ   û	ù^!	



$	




r   c                   @   sV  e Zd ZdZdddddi fdd„Zdd„ Zdd	„ Zd
d„ ZdHdd„Zi fdd„Z	i fdd„Z
dIdd„ZdJdd„ZdKdd„ZdLdd„Ze	Ze
ZeZdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zed,d-„ ƒZd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Z dMd6d7„Z!dNd9d:„Z"d;d<„ Z#dOd=d>„Z$d?d@„ Z%dPdBdC„Z&dQdDdE„Z'dAZ(dFdG„ Z)dS )RÚTableað  
    A display widget for a table of values, based on a ``MultiListbox``
    widget.  For many purposes, ``Table`` can be treated as a
    list-of-lists.  E.g., table[i] is a list of the values for row i;
    and table.append(row) adds a new row with the given list of
    values.  Individual cells can be accessed using table[i,j], which
    refers to the j-th column of the i-th row.  This can be used to
    both read and write values from the table.  E.g.:

        >>> table[i,j] = 'hello'  # doctest: +SKIP

    The column (j) can be given either as an index number, or as a
    column name.  E.g., the following prints the value in the 3rd row
    for the 'First Name' column:

        >>> print(table[3, 'First Name'])  # doctest: +SKIP
        John

    You can configure the colors for individual rows, columns, or
    cells using ``rowconfig()``, ``columnconfig()``, and ``itemconfig()``.
    The color configuration for each row will be preserved if the
    table is modified; however, when new rows are added, any color
    configurations that have been made for *columns* will not be
    applied to the new row.

    Note: Although ``Table`` acts like a widget in some ways (e.g., it
    defines ``grid()``, ``pack()``, and ``bind()``), it is not itself a
    widget; it just contains one.  This is because widgets need to
    define ``__getitem__()``, ``__setitem__()``, and ``__nonzero__()`` in
    a way that's incompatible with the fact that ``Table`` behaves as a
    list-of-lists.

    :ivar _mlb: The multi-column listbox used to display this table's data.
    :ivar _rows: A list-of-lists used to hold the cell values of this
        table.  Each element of _rows is a row value, i.e., a list of
        cell values, one for each column in the row.
    NTc	                 K   s  t |ƒ| _|| _t|ƒ| _dd„ t|ƒD ƒ| _|du r>g | _ndd„ |D ƒ| _| jD ]}
|  |
¡ qTt	| j|||fi |	¤Ž| _
| j
jdddd	� |rÎt| jd
| j
jd�}|j| j
jd d< |jddd� || _d| _|rüt| j
jƒD ]\}}| d| j¡ qä|  ¡  dS )aæ  
        Construct a new Table widget.

        :type master: Tkinter.Widget
        :param master: The widget that should contain the new table.
        :type column_names: list(str)
        :param column_names: A list of names for the columns; these
            names will be used to create labels for each column;
            and can be used as an index when reading or writing
            cell values from the table.
        :type rows: list(list)
        :param rows: A list of row values used to initialize the table.
            Each row value should be a tuple of cell values, one for
            each column in the row.
        :type scrollbar: bool
        :param scrollbar: If true, then create a scrollbar for the
            new table widget.
        :type click_to_sort: bool
        :param click_to_sort: If true, then create bindings that will
            sort the table's rows by a given column's values if the
            user clicks on that colum's label.
        :type reprfunc: function
        :param reprfunc: If specified, then use this function to
            convert each table cell value to a string suitable for
            display.  ``reprfunc`` has the following signature:
            reprfunc(row_index, col_index, cell_value) -> str
            (Note that the column is specified by index, not by name.)
        :param cnf, kw: Configuration parameters for this widget's
            contained ``MultiListbox``.  See ``MultiListbox.__init__()``
            for details.
        c                 S   s   i | ]\}}||“qS r    r    )rŒ   rN   Úcr    r    r!   Ú
<dictcomp>›  r#   z"Table.__init__.<locals>.<dictcomp>Nc                 S   s   g | ]}d d„ |D ƒ‘qS )c                 S   s   g | ]}|‘qS r    r    )rŒ   r¬   r    r    r!   r�   ¡  r#   z-Table.__init__.<locals>.<listcomp>.<listcomp>r    r‘   r    r    r!   r�   ¡  r#   z"Table.__init__.<locals>.<listcomp>ÚleftTÚboth)ÚsideÚexpandÚfillZvertical)ZorientÚcommandr   ZyscrollcommandÚrightr(   )r»   r½   r   )r5   Ú_num_columnsÚ	_reprfuncr   Ú_framer>   Ú_column_name_to_indexÚ_rowsÚ	_checkrowr   Ú_mlbÚpackr   r«   Úsetre   Z
_scrollbarÚ_sortkeyrd   rE   Ú_sortÚ_fill_table)r   rI   rc   rˆ   rK   Z	scrollbarZclick_to_sortÚreprfuncrL   rM   r   ZsbrN   rP   r    r    r!   r<   l  s*    +


zTable.__init__c                 O   s   | j j|i |¤Ž dS )zrPosition this table's main frame widget in its parent
        widget.  See ``Tkinter.Frame.pack()`` for more info.N)rÂ   rÇ   r£   r    r    r!   rÇ   À  s    z
Table.packc                 O   s   | j j|i |¤Ž dS )zrPosition this table's main frame widget in its parent
        widget.  See ``Tkinter.Frame.grid()`` for more info.N)rÂ   rB   r£   r    r    r!   rB   Å  s    z
Table.gridc                 C   s   | j  ¡  dS )z-Direct (keyboard) input foxus to this widget.N)rÆ   rk   r   r    r    r!   rk   Ê  s    zTable.focusc                 C   s   | j  |||¡ dS )zkAdd a binding to this table's main frame that will call
        ``func`` in response to the event sequence.N)rÆ   rE   rž   r    r    r!   rE   Î  s    z
Table.bindc                 K   s   | j j||fi |¤Ž dS )z%:see: ``MultiListbox.rowconfigure()``N)rÆ   r�   )r   r€   rL   rM   r    r    r!   r�   Ó  s    zTable.rowconfigurec                 K   s$   |   |¡}| jj||fi |¤Ž dS )z(:see: ``MultiListbox.columnconfigure()``N)rC   rÆ   r…   )r   r„   rL   rM   r    r    r!   r…   ×  s    
zTable.columnconfigurec                 K   s"   |   |¡}| jj|||fi |¤ŽS )z&:see: ``MultiListbox.itemconfigure()``)rC   rÆ   r   )r   r€   r„   rL   rM   r    r    r!   r   Ü  s    
zTable.itemconfigurec                 C   s   | j  |||¡S )z':see: ``MultiListbox.bind_to_labels()``)rÆ   rŸ   rž   r    r    r!   rŸ   á  s    zTable.bind_to_labelsc                 C   s   | j  |||¡S )z*:see: ``MultiListbox.bind_to_listboxes()``)rÆ   r    rž   r    r    r!   r    å  s    zTable.bind_to_listboxesc                 C   s   | j  |||¡S )z(:see: ``MultiListbox.bind_to_columns()``)rÆ   r¡   rž   r    r    r!   r¡   é  s    zTable.bind_to_columnsc                    sZ   ˆ  |¡ ˆj ˆ |¡ ˆjdur:‡ ‡fdd„t|ƒD ƒ}ˆj ˆ |¡ ˆjrVˆ ¡  dS )aT  
        Insert a new row into the table, so that its row index will be
        ``row_index``.  If the table contains any rows whose row index
        is greater than or equal to ``row_index``, then they will be
        shifted down.

        :param rowvalue: A tuple of cell values, one for each column
            in the new row.
        Nc                    s   g | ]\}}ˆ  ˆ ||¡‘qS r    ©rÁ   ©rŒ   Újr¬   ©r€   r   r    r!   r�     s   z Table.insert.<locals>.<listcomp>)rÅ   rÄ   r‡   rÁ   r>   rÆ   Ú_DEBUGÚ_check_table_vs_mlb)r   r€   Úrowvaluer    rÐ   r!   r‡   õ  s    


ÿzTable.insertc                 C   s&   |D ]}|   |¡ q| jr"|  ¡  dS )zï
        Add new rows at the end of the table.

        :param rowvalues: A list of row values used to initialize the
            table.  Each row value should be a tuple of cell values,
            one for each column in the row.
        N)rA   rÑ   rÒ   )r   Z	rowvaluesrÓ   r    r    r!   Úextend	  s    zTable.extendc                 C   s$   |   t| jƒ|¡ | jr |  ¡  dS )zš
        Add a new row to the end of the table.

        :param rowvalue: A tuple of cell values, one for each column
            in the new row.
        N)r‡   r5   rÄ   rÑ   rÒ   ©r   rÓ   r    r    r!   rA     s    zTable.appendc                 C   s&   g | _ | j dd¡ | jr"|  ¡  dS )z0
        Delete all rows in this table.
        r   rf   N)rÄ   rÆ   r©   rÑ   rÒ   r   r    r    r!   Úclear!  s    zTable.clearc                 C   sX   t |tƒrtdƒ‚n@t |tƒrFt|ƒdkrF| j|d  |  |d ¡ S t| j| ƒS dS )a‹  
        Return the value of a row or a cell in this table.  If
        ``index`` is an integer, then the row value for the ``index``th
        row.  This row value consists of a tuple of cell values, one
        for each column in the row.  If ``index`` is a tuple of two
        integers, ``(i,j)``, then return the value of the cell in the
        ``i``th row and the ``j``th column.
        úSlicing not supportedé   r   r   N)r1   Úslicer6   r7   r5   rÄ   rC   )r   ro   r    r    r!   Ú__getitem__*  s
    	

zTable.__getitem__c                    s  t ˆ tƒrtdƒ‚�nt ˆ tƒr¬tˆ ƒdkr¬ˆ d ˆ ˆ d ¡ }}ˆ |g¡}|ˆj| |< ˆjdurvˆ |||¡}ˆj	j
|  ||¡ ˆj	j
|  |d ¡ ˆ |¡ nnˆ ˆ g¡}ˆ |¡ t|ƒˆjˆ < ˆjdurò‡ ‡fdd„t|ƒD ƒ}ˆj	 ˆ |¡ ˆj	 ˆ d ¡ ˆ |¡ dS )aú  
        Replace the value of a row or a cell in this table with
        ``val``.

        If ``index`` is an integer, then ``val`` should be a row value
        (i.e., a tuple of cell values, one for each column).  In this
        case, the values of the ``index``th row of the table will be
        replaced with the values in ``val``.

        If ``index`` is a tuple of integers, ``(i,j)``, then replace the
        value of the cell in the ``i``th row and ``j``th column with
        ``val``.
        r×   rØ   r   r   Nc                    s   g | ]\}}ˆ  ˆ ||¡‘qS r    rÍ   rÎ   ©ro   r   r    r!   r�   \  r#   z%Table.__setitem__.<locals>.<listcomp>)r1   rÙ   r6   r7   r5   rC   Ú_save_config_inforÄ   rÁ   rÆ   re   r‡   r©   Ú_restore_config_inforÅ   r3   r>   )r   ro   r{   rN   rÏ   Úconfig_cookier    rÛ   r!   r}   :  s&    



zTable.__setitem__c                 C   sV   t |tƒrtdƒ‚t |tƒr0t|ƒdkr0tdƒ‚| j|= | j |¡ | jrR|  	¡  dS )zA
        Delete the ``row_index``th row from this table.
        r×   rØ   zCannot delete a single cell!N)
r1   rÙ   r6   r7   r5   rÄ   rÆ   r©   rÑ   rÒ   )r   r€   r    r    r!   Ú__delitem__a  s    
zTable.__delitem__c                 C   s
   t | jƒS )z<
        :return: the number of rows in this table.
        )r5   rÄ   r   r    r    r!   Ú__len__n  s    zTable.__len__c                 C   s*   t |ƒ| jkr&td|t |ƒ| jf ƒ‚dS )z‹
        Helper function: check that a given row value has the correct
        number of elements; and if not, raise an exception.
        z"Row %r has %d columns; expected %dN)r5   rÀ   r6   rÕ   r    r    r!   rÅ   t  s    ÿÿzTable._checkrowc                 C   s   | j jS )z1A list of the names of the columns in this table.)rÆ   rc   r   r    r    r!   rc   ƒ  s    zTable.column_namesc                 C   s6   t |tƒr(d|  kr | jk r(n n|S | j| S dS )zö
        If ``i`` is a valid column index integer, then return it as is.
        Otherwise, check if ``i`` is used as the name for any column;
        if so, return that column's index.  Otherwise, raise a
        ``KeyError`` exception.
        r   N)r1   r2   rÀ   rÃ   )r   rN   r    r    r!   rC   ˆ  s    $zTable.column_indexc                 C   s   | j  |  |¡¡ dS )z$:see: ``MultiListbox.hide_column()``N)rÆ   r˜   rC   ©r   rC   r    r    r!   r˜   •  s    zTable.hide_columnc                 C   s   | j  |  |¡¡ dS )z$:see: ``MultiListbox.show_column()``N)rÆ   r™   rC   rá   r    r    r!   r™   ™  s    zTable.show_columnc                 C   s"   | j  ¡ }|rt|d ƒS dS dS )z³
        Return the index of the currently selected row, or None if
        no row is selected.  To get the row value itself, use
        ``table[table.selected_row()]``.
        r   N)rÆ   rp   r2   )r   Úselr    r    r!   Úselected_row¡  s    
zTable.selected_rowc                 C   s   | j  |||¡ dS )z:see: ``MultiListbox.select()``N)rÆ   r-   rt   r    r    r!   r-   ­  s    zTable.selectÚtogglec                 C   sŽ   |dvrt dƒ‚|  |¡}| jdd�}|dkrD|| jkrD| j ¡  n | jjt |¡|dkd� || _|  	¡  | j
|ddd� | jrŠ|  ¡  d	S )
aÒ  
        Sort the rows in this table, using the specified column's
        values as a sort key.

        :param column_index: Specifies which column to sort, using
            either a column index (int) or a column's label name
            (str).

        :param order: Specifies whether to sort the values in
            ascending or descending order:

              - ``'ascending'``: Sort from least to greatest.
              - ``'descending'``: Sort from greatest to least.
              - ``'toggle'``: If the most recent call to ``sort_by()``
                sorted the table by the same column (``column_index``),
                then reverse the rows; otherwise sort in ascending
                order.
        )Z	ascendingÚ
descendingrä   zBsort_by(): order should be "ascending", "descending", or "toggle".T)Úindex_by_idrä   rå   )rz   Úreverse)ræ   rs   N)r6   rC   rÜ   rÉ   rÄ   rç   ÚsortÚoperatorÚ
itemgetterrË   rÝ   rÑ   rÒ   )r   rC   ÚorderrÞ   r    r    r!   Úsort_byµ  s     ÿ
ÿzTable.sort_byc                 C   s*   |j j}| j |¡rdS |  |¡ dS dS )zLEvent handler for clicking on a column label -- sort by
        that column.ÚcontinueN)rV   rC   rÆ   rG   rì   )r   r^   rC   r    r    r!   rÊ   Þ  s
    
zTable._sortc                    sV   ˆj  dd¡ tˆjƒD ]8\‰ }ˆjdurB‡ ‡fdd„t|ƒD ƒ}ˆj  d|¡ qdS )a„  
        Re-draw the table from scratch, by clearing out the table's
        multi-column listbox; and then filling it in with values from
        ``self._rows``.  Note that any cell-, row-, or column-specific
        color configuration that has been done will be lost.  The
        selection will also be lost -- i.e., no row will be selected
        after this call completes.
        r   rf   Nc                    s   g | ]\}}ˆ  ˆ ||¡‘qS r    rÍ   rÎ   ©rN   r   r    r!   r�   ý  r#   z%Table._fill_table.<locals>.<listcomp>)rÆ   r©   r>   rÄ   rÁ   r‡   )r   Zsave_configr   r    rî   r!   rË   ñ  s
    	
zTable._fill_tablec                    s   ‡ ‡‡fdd„dD ƒS )Nc                    s"   i | ]}|ˆj  ˆˆ |¡d  “qS )r   )rÆ   r³   )rŒ   Úk©r·   Úrr   r    r!   r¸     s   ÿz)Table._get_itemconfig.<locals>.<dictcomp>)r   rƒ   r	   r   r    )r   rñ   r·   r    rð   r!   Ú_get_itemconfig   s    þzTable._get_itemconfigFc                    sn   |du rt ttˆ jƒƒƒ}ˆ  ¡ }|r<|dur<tˆ j| ƒ}|rT‡ fdd„|D ƒ}n‡ fdd„|D ƒ}||fS )a*  
        Return a 'cookie' containing information about which row is
        selected, and what color configurations have been applied.
        this information can the be re-applied to the table (after
        making modifications) using ``_restore_config_info()``.  Color
        configuration information will be saved for any rows in
        ``row_indices``, or in the entire table, if
        ``row_indices=None``.  If ``index_by_id=True``, the the cookie
        will associate rows with their configuration information based
        on the rows' python id.  This is useful when performing
        operations that re-arrange the rows (e.g. ``sort``).  If
        ``index_by_id=False``, then it is assumed that all rows will be
        in the same order when ``_restore_config_info()`` is called.
        Nc                    s2   i | ]*‰ t ˆjˆ  ƒ‡ ‡fd d„tˆjƒD ƒ“qS )c                    s   g | ]}ˆ  ˆ |¡‘qS r    ©rò   ©rŒ   r·   ©rñ   r   r    r!   r�   &  s   ú6Table._save_config_info.<locals>.<dictcomp>.<listcomp>)ÚidrÄ   r4   rÀ   ©rŒ   r   ©rñ   r!   r¸   %  s   ýÿz+Table._save_config_info.<locals>.<dictcomp>c                    s(   i | ] ‰ ˆ ‡ ‡fd d„t ˆjƒD ƒ“qS )c                    s   g | ]}ˆ  ˆ |¡‘qS r    ró   rô   rõ   r    r!   r�   -  r#   rö   )r4   rÀ   rø   r   rù   r!   r¸   ,  s   ÿ)r3   r4   r5   rÄ   rã   r÷   )r   Zrow_indicesræ   Ú	selectionÚconfigr    r   r!   rÜ     s    
ü
þzTable._save_config_infoc           	   	   C   sÚ   |\}}|du r| j  dd¡ |rŒt| jƒD ]\\}}t|ƒ|v rlt| jƒD ] }| j  |||t|ƒ | ¡ qJt|ƒ|kr,| j j||d� q,nJ|dur¤| j j||d� |D ],}t| jƒD ]}| j  |||| | ¡ q¶q¨dS )zy
        Restore selection & color configuration information that was
        saved using ``_save_config_info``.
        Nr   rf   )rs   )	rÆ   rh   r>   rÄ   r÷   r4   rÀ   r   r-   )	r   Úcookieræ   rs   rú   rû   rñ   r   r·   r    r    r!   rÝ   3  s    zTable._restore_config_infoc                 C   s¬   | j jD ]}t| ƒ| ¡ ksJ ‚q| D ]}t|ƒ| jks&J ‚q&| jt| j jƒksTJ ‚t| ƒD ]J\}}t|ƒD ]8\}}| jdurŒ|  |||¡}| j  |¡| |kslJ ‚qlq\dS )a  
        Verify that the contents of the table's ``_rows`` variable match
        the contents of its multi-listbox (``_mlb``).  This is just
        included for debugging purposes, to make sure that the
        list-modifying operations are working correctly.
        N)	rÆ   re   r5   rr   rÀ   rc   r>   rÁ   r‹   )r   r”   r   rN   rÏ   Úcellr    r    r!   rÒ   U  s    
zTable._check_table_vs_mlb)NNN)N)NNN)NNN)NNN)NNT)rä   )T)NF)FF)*r®   r¯   r°   r±   r<   rÇ   rB   rk   rE   r�   r…   r   rŸ   r    r¡   r´   rµ   r³   r‡   rÔ   rA   rÖ   rÚ   r}   rß   rà   rÅ   r²   rc   rC   r˜   r™   rã   r-   rì   rÊ   rË   rò   rÜ   rÝ   rÑ   rÒ   r    r    r    r!   r¶   E  sZ   *÷
T




	'


)

(
r¶   c            
         s†  t ƒ ‰ ˆ  d‡ fdd„¡ tˆ d ¡ g d¢dd„ d�} | jdd	d
� ddlm}m} tt	| 
¡ d d… ƒƒD ]�\}}|d dkr‚ql| ¡ }| |¡D ]f}z| ¡ d  ¡ }W n   d}Y n0 z| ¡ d  ¡ }W n   d}Y n0 |  || ¡ ||g¡ q”ql| jddd� | jddd� | jddd� | jddd� tt| ƒƒD ]6}dD ]*}	| ||	f dk�rJ| j||	ddd� �qJ�qBˆ  ¡  d S )Nz<Control-q>c                    s   ˆ   ¡ S r$   )Údestroyr   ©Úrootr    r!   r"   q  r#   zdemo.<locals>.<lambda>zWord Synset Hypernym Hyponym)r   r   r   r   c                 S   s   d| S )Nz  %sr    )rN   rÏ   Úsr    r    r!   r"   w  r#   )rK   rÌ   Trº   )r¼   r½   r   )ÚbrownÚwordnetiô  ÚNz*none*ÚWordz#afa)r	   ZSynsetz#efeÚHypernymz#feeÚHyponymz#ffe)r  r  z#666)r   rƒ   )r   rE   r¶   ÚsplitrÇ   Znltk.corpusr  r  ÚsortedrÈ   Ztagged_wordsÚlowerZsynsetsZ	hypernymsZ
definitionrA   rµ   r4   r5   r³   Zmainloop)
Útabler  r  ÚwordÚposZsynsetZ	hyper_defZhypo_defr   r   r    rÿ   r!   Údemoo  sF    ü 

ÿr  Ú__main__)r±   ré   Útkinterr   r   r   r   r   r   r¶   r  r®   r    r    r    r!   Ú<module>   s       5    .*