o
    ¹i •  ã                   @   sº   d Z ddlmZmZmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ G dd„ deƒZdd	„ Zeefd
d„ZG dd„ deƒZG dd„ dƒZdd„ Zdd„ Zedkr[eƒ  dS dS )z
Graphically display a Tree.
é    )ÚIntVarÚMenuÚTk)Ú	BoxWidgetÚCanvasFrameÚCanvasWidgetÚ
OvalWidgetÚParenWidgetÚ
TextWidget)ÚTree)Úin_idlec                   @   s°   e Zd Z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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d$d%„ Zd&d'„ Zd(d)„ Zd*S )+ÚTreeSegmentWidgetaÃ  
    A canvas widget that displays a single segment of a hierarchical
    tree.  Each ``TreeSegmentWidget`` connects a single "node widget"
    to a sequence of zero or more "subtree widgets".  By default, the
    bottom of the node is connected to the top of each subtree by a
    single line.  However, if the ``roof`` attribute is set, then a
    single triangular "roof" will connect the node to all of its
    children.

    Attributes:
      - ``roof``: What sort of connection to draw between the node and
        its subtrees.  If ``roof`` is true, draw a single triangular
        "roof" over the subtrees.  If ``roof`` is false, draw a line
        between each subtree and the node.  Default value is false.
      - ``xspace``: The amount of horizontal space to leave between
        subtrees when managing this widget.  Default value is 10.
      - ``yspace``: The amount of space to place between the node and
        its children when managing this widget.  Default value is 15.
      - ``color``: The color of the lines connecting the node to its
        subtrees; and of the outline of the triangular roof.  Default
        value is ``'#006060'``.
      - ``fill``: The fill color for the triangular roof.  Default
        value is ``''`` (no fill).
      - ``width``: The width of the lines connecting the node to its
        subtrees; and of the outline of the triangular roof.  Default
        value is 1.
      - ``orientation``: Determines whether the tree branches downwards
        or rightwards.  Possible values are ``'horizontal'`` and
        ``'vertical'``.  The default value is ``'vertical'`` (i.e.,
        branch downwards).
      - ``draggable``: whether the widget can be dragged by the user.
    c                    s   || _ || _d| _d| _d| _d| _d| _‡ fdd„|D ƒ| _ˆ jddddd	d
| _	|  
|¡ |D ]}|  
|¡ q1d| _tj| ˆ fi |¤Ž dS )zI
        :type node:
        :type subtrees: list(CanvasWidgetI)
        r   é
   é   Fc              	      s    g | ]}ˆ j d d d d dd‘qS )r   ú#006060©Úfill)Úcreate_line)Ú.0Úc©Úcanvas© úI/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nltk/draw/tree.pyÚ
<listcomp>P   ó     z.TreeSegmentWidget.__init__.<locals>.<listcomp>Ú Úhiddenr   )r   ÚstateÚoutlineN)Ú_labelÚ	_subtreesÚ_horizontalÚ_roofÚ_xspaceÚ_yspaceÚ_orderedÚ_linesÚcreate_polygonÚ_polygonÚ_add_child_widgetÚ	_managingr   Ú__init__)Úselfr   ÚlabelÚsubtreesÚattribsÚsubtreer   r   r   r,   @   s    
ÿ
zTreeSegmentWidget.__init__c                 C   sÌ  |   ¡ }|dkr<|| _| jr%| jD ]	}|j|dd q|j| jdd d S | jD ]	}|j|dd q(|j| jdd d S |dkrV|dkrId| _d S |dkrRd	| _d S td
ƒ‚|dkrq| jD ]	}|j||d q]|j| j|d d S t|tƒrŽ|d	 dkrŽ| jt	|d ƒ }|j||d d S |dkrœ|j| j|d d S |dkr¹| | j||i¡ | jD ]
}| |||i¡ q¬d S |dv rÔ|dkrÅ|| _
n|dkrÌ|| _|  | j¡ d S |dkrÝ|| _d S t | ||¡ d S )NÚroofr   )r   ÚnormalÚorientationÚ
horizontalé   Úverticalr   z*orientation must be horizontal or verticalÚcolorr   )r   r   Úwidth)ÚxspaceÚyspacer:   r;   Úordered)r   r#   r'   Ú
itemconfigr)   r"   Ú
ValueErrorÚ
isinstanceÚtupleÚintr$   r%   Úupdater    r&   r   Ú__setitem__)r-   ÚattrÚvaluer   Úlr   r   r   rC   _   sN   





ÿ
zTreeSegmentWidget.__setitem__c                 C   sÆ   |dkr| j S |dkr|  ¡  | j|¡S |dkr!|  ¡  | jd¡S t|tƒr=|d dkr=| jt|d ƒ }|  ¡  |d¡S |dkrD| jS |d	krK| j	S |d
krV| j
rTdS dS |dkr]| jS t | |¡S )Nr2   r9   r8   r   r   r6   r   r:   r;   r4   r5   r7   r<   )r#   r   Úitemcgetr)   r?   r@   r'   rA   r$   r%   r"   r&   r   Ú__getitem__)r-   rD   rF   r   r   r   rH   ‹   s(   zTreeSegmentWidget.__getitem__c                 C   s   | j S ©N)r    ©r-   r   r   r   r.   £   s   zTreeSegmentWidget.labelc                 C   s   | j d d … S rI   )r!   rJ   r   r   r   r/   ¦   s   zTreeSegmentWidget.subtreesc                 C   s,   |   | j¡ |  |¡ || _|  | j¡ dS )z2
        Set the node label to ``label``.
        N)Ú_remove_child_widgetr    r*   rB   )r-   r.   r   r   r   Ú	set_label©   s   
zTreeSegmentWidget.set_labelc                 C   s8   | j  |¡}|| j |< |  |¡ |  |¡ |  |¡ dS )zC
        Replace the child ``oldchild`` with ``newchild``.
        N)r!   ÚindexrK   r*   rB   )r-   ÚoldchildÚnewchildrM   r   r   r   Úreplace_child²   s
   


zTreeSegmentWidget.replace_childc                 C   sB   | j  |¡}| j |= |  |¡ |  ¡  | j ¡ ¡ |  | j¡ d S rI   )	r!   rM   rK   r   Údeleter'   ÚpoprB   r    )r-   ÚchildrM   r   r   r   Úremove_child¼   s
   
zTreeSegmentWidget.remove_childc              	   C   sL   |   ¡ }| j ||¡ |  |¡ | j |jdddddd¡ |  | j¡ d S )Nr   r   r   )	r   r!   Úinsertr*   r'   Úappendr   rB   r    )r-   rM   rS   r   r   r   r   Úinsert_childÃ   s
   
zTreeSegmentWidget.insert_childc                 C   s   | j r| jgS | jS rI   )r#   r)   r'   rJ   r   r   r   Ú_tagsÌ   s   zTreeSegmentWidget._tagsc                 C   s^   t |tƒr| ¡  ¡ }n| ¡ }| jr!|d |d |d  d fS |d |d  d |d fS )Nr   r6   é   ç       @é   )r?   r   r.   Úbboxr"   )r-   rS   r\   r   r   r   Ú_subtree_topÒ   s   
zTreeSegmentWidget._subtree_topc                 C   sH   | j  ¡ }| jr|d |d |d  d fS |d |d  d |d fS )Nr[   r6   rY   rZ   r   )r    r\   r"   )r-   r\   r   r   r   Ú_node_bottomÜ   s   
zTreeSegmentWidget._node_bottomc                 C   s`  t | jƒdkr	d S | j ¡ d u rd S || ju r| j}n|g}| jr)| js)|  |¡}|  ¡ \}}| jd  ¡ \}}}}| jdd … D ]"}	|	 ¡ }
t||
d ƒ}t||
d ƒ}t	||
d ƒ}t	||
d ƒ}qA| j
rx|  ¡  | j||||||||¡	 n|  ¡  | j||||||||¡	 |D ]#}	|  ¡ \}}| j| j |	¡ }|  |	¡\}}|  ¡  |||||¡ qŠd S )Nr   r6   r[   rY   )Úlenr!   r    r\   r&   r+   Ú_maintain_orderr^   ÚminÚmaxr"   r   Úcoordsr)   r'   rM   r]   )r-   rS   Úneed_updateÚnodexÚnodeyÚxminÚyminÚxmaxÚymaxr1   r\   ÚlineÚsubtreexÚsubtreeyr   r   r   Ú_updateã   s<   

ÿÿüzTreeSegmentWidget._updatec                 C   s   | j r|  |¡S |  |¡S rI   )r"   Ú_maintain_order_horizontalÚ_maintain_order_vertical)r-   rS   r   r   r   r`     s   

z!TreeSegmentWidget._maintain_orderc                 C   s¤  |  ¡ \}}}}|| ju r0| jD ]}|  ¡ \}}}	}
|| j |kr,| d|| j | ¡ q| jS |g}| j |¡}|| j }t|d t| jƒƒD ]-}| j|   ¡ \}}}	}
||kru| j|  || d¡ ||	| | j 7 }| 	| j| ¡ qH|| j }t|d ddƒD ]-}| j|   ¡ \}}}	}
||	k r°| j|  ||	 d¡ ||	| | j 8 }| 	| j| ¡ qƒ| j  ¡ \}}}	}
|
|| j krÐ| j d|| j |
 ¡ | j}|S )Nr   r6   éÿÿÿÿ)
r\   r    r!   r%   ÚmoverM   r$   Úranger_   rV   )r-   rS   ÚleftÚtopÚrightÚbotr1   Úx1Úy1Úx2Úy2ÚmovedrM   ÚxÚir   r   r   rp     s>   

€
€
€z*TreeSegmentWidget._maintain_order_verticalc                 C   s¢  |  ¡ \}}}}|| ju r/| jD ]}|  ¡ \}}}	}
|| j |kr+| || j | ¡ q| jS |g}| j |¡}|| j }t|d t| jƒƒD ]-}| j|   ¡ \}}}	}
||krt| j|  d|| ¡ ||
| | j 7 }| 	| j| ¡ qG|| j }t|d ddƒD ]-}| j|   ¡ \}}}	}
||
k r¯| j|  d||
 ¡ ||
| | j 8 }| 	| j| ¡ q‚| j  ¡ \}}}	}
|	|| j krÏ| j || j |	 d¡ | j}|S )Nr6   r   rq   )
r\   r    r!   r$   rr   rM   r%   rs   r_   rV   )r-   rS   rt   ru   rv   rw   r1   rx   ry   rz   r{   r|   rM   Úyr~   r   r   r   ro   <  s>   

€
€
€z,TreeSegmentWidget._maintain_order_horizontalc           	      C   s¸   |   ¡ \}}d}| jD ](}| ¡ }||d  | j }||d  }| ||¡ ||d |d  | j 7 }qd}| jD ]}||  |¡d 7 }q9|t| jƒ }| jD ]
}| d|| ¡ qOd S )Né   r   r6   rY   ç        )r^   r!   r\   r$   rr   r%   r]   r_   )	r-   re   rf   r   r1   Úsubtree_bboxÚdxÚdyÚcenterr   r   r   Ú_manage_horizontalf  s   


ÿz$TreeSegmentWidget._manage_horizontalc           	      C   s´   |   ¡ \}}d}| jD ](}| ¡ }||d  | j }||d  }| ||¡ ||d |d  | j 7 }qd}| jD ]}||  |¡d t| jƒ 7 }q9| jD ]
}| || d¡ qMd S )Nr   r6   r[   r   )r^   r!   r\   r%   rr   r$   r]   r_   )	r-   re   rf   r}   r1   r‚   r„   rƒ   r…   r   r   r   Ú_manage_vertical|  s   


ÿz"TreeSegmentWidget._manage_verticalc                 C   s\   d| _ |  ¡ \}}t| jƒdkrd S | jr|  ¡  n|  ¡  | jD ]}|  |¡ q!d| _ d S )NTr   F)r+   r^   r_   r!   r"   r†   r‡   rn   )r-   re   rf   r1   r   r   r   Ú_manage‘  s   


zTreeSegmentWidget._managec                 C   s   d| j › d| j› dS )Nz	[TreeSeg z: ú])r    r!   rJ   r   r   r   Ú__repr__¢  s   zTreeSegmentWidget.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   rC   rH   r.   r/   rL   rP   rT   rW   rX   r]   r^   rn   r`   rp   ro   r†   r‡   rˆ   rŠ   r   r   r   r   r      s,    !,	
	
)**r   c           
         sd   t |tƒr)ˆˆ | ¡ fi ˆ¤Ž}‡ ‡‡‡‡‡‡fdd„|D ƒ}	tˆ ||	fi ˆ¤ŽS ˆˆ |fi ˆ¤ŽS )Nc                    s"   g | ]}t ˆ |ˆˆˆˆˆˆƒ‘qS r   )Ú_tree_to_treeseg)r   rS   ©r   Úleaf_attribsÚloc_attribsÚ	make_leafÚ	make_nodeÚnode_attribsÚtree_attribsr   r   r   ²  s    öøÿz$_tree_to_treeseg.<locals>.<listcomp>)r?   r   r.   r   )
r   Útr”   r“   r–   r•   r‘   r’   r.   r/   r   r   r   r   ¦  s   

õr   c              	   K   sÎ   i }i }i }i }t | ¡ ƒD ]M\}	}
|	dd… dkr#|
||	dd… < q|	dd… dkr4|
||	dd… < q|	dd… dkrE|
||	dd… < q|	dd… dkrV|
||	dd… < qtd|	 ƒ‚t| |||||||ƒS )	aS  
    Convert a Tree into a ``TreeSegmentWidget``.

    :param make_node: A ``CanvasWidget`` constructor or a function that
        creates ``CanvasWidgets``.  ``make_node`` is used to convert
        the Tree's nodes into ``CanvasWidgets``.  If no constructor
        is specified, then ``TextWidget`` will be used.
    :param make_leaf: A ``CanvasWidget`` constructor or a function that
        creates ``CanvasWidgets``.  ``make_leaf`` is used to convert
        the Tree's leafs into ``CanvasWidgets``.  If no constructor
        is specified, then ``TextWidget`` will be used.
    :param attribs: Attributes for the canvas widgets that make up the
        returned ``TreeSegmentWidget``.  Any attribute beginning with
        ``'tree_'`` will be passed to all ``TreeSegmentWidgets`` (with
        the ``'tree_'`` prefix removed.  Any attribute beginning with
        ``'node_'`` will be passed to all nodes.  Any attribute
        beginning with ``'leaf_'`` will be passed to all leaves.  And
        any attribute beginning with ``'loc_'`` will be passed to all
        text locations (for Trees).
    Né   Útree_Únode_Úleaf_é   Úloc_zBad attribute: %s)ÚlistÚitemsr>   r   )r   r—   r”   r“   r0   r–   r•   r‘   r’   ÚkeyrE   r   r   r   Útree_to_treesegmentÄ  s0   ør¡   c                   @   s¢   e Zd ZdZeefdd„Zdd„ Zdd„ Zd$d	d
„Zd$dd„Z	d$dd„Z
d$dd„Zd$dd„Zd$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#S )%Ú
TreeWidgeta   
    A canvas widget that displays a single Tree.
    ``TreeWidget`` manages a group of ``TreeSegmentWidgets`` that are
    used to display a Tree.

    Attributes:

      - ``node_attr``: Sets the attribute ``attr`` on all of the
        node widgets for this ``TreeWidget``.
      - ``node_attr``: Sets the attribute ``attr`` on all of the
        leaf widgets for this ``TreeWidget``.
      - ``loc_attr``: Sets the attribute ``attr`` on all of the
        location widgets for this ``TreeWidget`` (if it was built from
        a Tree).  Note that a location widget is a ``TextWidget``.

      - ``xspace``: The amount of horizontal space to leave between
        subtrees when managing this widget.  Default value is 10.
      - ``yspace``: The amount of space to place between the node and
        its children when managing this widget.  Default value is 15.

      - ``line_color``: The color of the lines connecting each expanded
        node to its subtrees.
      - ``roof_color``: The color of the outline of the triangular roof
        for collapsed trees.
      - ``roof_fill``: The fill color for the triangular roof for
        collapsed trees.
      - ``width``

      - ``orientation``: Determines whether the tree branches downwards
        or rightwards.  Possible values are ``'horizontal'`` and
        ``'vertical'``.  The default value is ``'vertical'`` (i.e.,
        branch downwards).

      - ``shapeable``: whether the subtrees can be independently
        dragged by the user.  THIS property simply sets the
        ``DRAGGABLE`` property on all of the ``TreeWidget``'s tree
        segments.
      - ``draggable``: whether the widget can be dragged by the user.
    c                 K   s¾   || _ || _|| _i | _i | _ddi| _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _i | _i | _i | _g | _g | _|  ||d	¡ |  ||d	¡| _|  | j¡ tj| |fi |¤Ž d S )
Nr8   z#008000z#008080r6   z#c0c0c0Fr   r7   r   )Ú
_make_nodeÚ
_make_leafÚ_treeÚ_nodeattribsÚ_leafattribsÚ_locattribsÚ_line_colorÚ_line_widthÚ_roof_colorÚ
_roof_fillÚ
_shapeabler$   r%   Ú_orientationr&   Ú_keysÚ_expanded_treesÚ_collapsed_treesÚ_nodesÚ_leavesÚ_make_collapsed_treesÚ_make_expanded_treeÚ_treesegr*   r   r,   )r-   r   r—   r”   r“   r0   r   r   r   r,   &  s0   
zTreeWidget.__init__c                 G   ó
   | j | S ©aF  
        Return the ``TreeSegmentWidget`` for the specified subtree.

        :param path_to_tree: A list of indices i1, i2, ..., in, where
            the desired widget is the widget corresponding to
            ``tree.children()[i1].children()[i2]....children()[in]``.
            For the root, the path is ``()``.
        )r°   ©r-   Úpath_to_treer   r   r   Úexpanded_treeI  ó   
	zTreeWidget.expanded_treec                 G   r·   r¸   )r±   r¹   r   r   r   Úcollapsed_treeT  r¼   zTreeWidget.collapsed_treer6   c                 C   óD   t | j ¡ ƒD ]}| ||¡ qt | j ¡ ƒD ]}| ||¡ qdS ©z5
        Add a binding to all tree segments.
        N)rž   r°   ÚvaluesÚ
bind_clickr±   ©r-   ÚcallbackÚbuttonÚtsegr   r   r   Úbind_click_trees_  ó
   ÿzTreeWidget.bind_click_treesc                 C   r¾   r¿   )rž   r°   rÀ   Ú	bind_dragr±   rÂ   r   r   r   Úbind_drag_treesh  rÇ   zTreeWidget.bind_drag_treesc                 C   ó4   | j D ]}| ||¡ q| j D ]}| ||¡ qdS ©z.
        Add a binding to all leaves.
        N)r³   rÁ   ©r-   rÃ   rÄ   Úleafr   r   r   Úbind_click_leavesq  ó
   

ÿzTreeWidget.bind_click_leavesc                 C   rÊ   rË   )r³   rÈ   rÌ   r   r   r   Úbind_drag_leavesz  rÏ   zTreeWidget.bind_drag_leavesc                 C   rÊ   ©z-
        Add a binding to all nodes.
        N)r²   rÁ   ©r-   rÃ   rÄ   Únoder   r   r   Úbind_click_nodesƒ  rÏ   zTreeWidget.bind_click_nodesc                 C   rÊ   rÑ   )r²   rÈ   rÒ   r   r   r   Úbind_drag_nodesŒ  rÏ   zTreeWidget.bind_drag_nodesc           
   	      sÐ   t |tƒsd S ˆj}ˆj‰|ˆ | ¡ fi ˆj¤Ž}ˆj |¡ ‡ ‡‡fdd„| ¡ D ƒ}ˆ j	|7  _	t
ˆ ||dˆjˆjˆjd}|ˆj|< |ˆj|< | ¡  tt|ƒƒD ]}|| }	ˆ ˆ |	||f ¡ qUd S )Nc                    s    g | ]}ˆˆ |fi ˆj ¤Ž‘qS r   )r§   )r   rF   ©r   r“   r-   r   r   r     r   z4TreeWidget._make_collapsed_trees.<locals>.<listcomp>r6   )r2   r8   r   r9   )r?   r   r£   r¤   r.   r¦   r²   rV   Úleavesr³   r   r«   r¬   rª   r±   r¯   Úhiders   r_   r´   )
r-   r   r—   r    r”   rÓ   r×   Útreesegr~   rS   r   rÖ   r   r´   •  s0   
ù


þz TreeWidget._make_collapsed_treesc           
         s°   ˆj }ˆj}t|tƒrF|ˆ | ¡ fi ˆj¤Ž}ˆj |¡ |‰‡ ‡‡‡fdd„tt	ˆƒƒD ƒ}t
ˆ ||ˆjˆjd}|ˆjˆ< ˆˆj|< |S |ˆ |fi ˆj¤Ž}	ˆj |	¡ |	S )Nc                    s$   g | ]}ˆ  ˆ ˆ| ˆ|f ¡‘qS r   )rµ   )r   r~   ©r   Úchildrenr    r-   r   r   r   »  s    ÿÿz2TreeWidget._make_expanded_tree.<locals>.<listcomp>)r8   r9   )r£   r¤   r?   r   r.   r¦   r²   rV   rs   r_   r   r©   rª   r°   r¯   r§   r³   )
r-   r   r—   r    r”   r“   rÓ   r/   rÙ   rÍ   r   rÚ   r   rµ   ³  s$   

þÿ

zTreeWidget._make_expanded_treec                 C   sö  |d d… dkr| j D ]
}|||dd … < qd S |d d… dkr0| jD ]
}|||dd … < q#d S |dkrG|| _t| j ¡ ƒD ]}||d< q>d S |dkrl|| _t| j ¡ ƒD ]}||d< qUt| j ¡ ƒD ]}||d< qcd S |dkrƒ|| _t| j ¡ ƒD ]}||d< qzd S |d	krš|| _	t| j ¡ ƒD ]}||d
< q‘d S |dkrÉ|| _
t| j ¡ ƒD ]}||d< q¨t| j ¡ ƒD ]}||d< q¶| jD ]}||d< qÀd S |dkrò|| _t| j ¡ ƒD ]}||d< q×t| j ¡ ƒD ]}||d< qå|  ¡  d S |dkr|| _t| j ¡ ƒD ]}||d< qt| j ¡ ƒD ]}||d< q|  ¡  d S |dkrJ|| _t| j ¡ ƒD ]}||d< q-t| j ¡ ƒD ]}||d< q<|  ¡  d S |dkrr|| _t| j ¡ ƒD ]}||d< qYt| j ¡ ƒD ]}||d< qhd S t | ||¡ d S )Nr˜   rš   r›   Ú
line_colorr8   Ú
line_widthr9   Ú
roof_colorÚ	roof_fillr   Ú	shapeableÚ	draggabler:   r;   r4   r<   )r²   r³   r©   rž   r°   rÀ   rª   r±   r«   r¬   r­   r$   Úmanager%   r®   r&   r   rC   )r-   rD   rE   rÓ   rÍ   rÅ   r   r   r   rC   Ê  sˆ   
ÿ
ÿ
ÿ

ÿ
ÿ
ÿ



ÿ




ÿzTreeWidget.__setitem__c                 C   sî   |d d… dkr| j  |dd … d ¡S |d d… dkr&| j |dd … d ¡S |d d… dkr9| j |dd … d ¡S |dkr@| jS |dkrG| jS |dkrN| jS |d	krU| jS |d
kr\| jS |dkrc| j	S |dkrj| j
S |dkrq| jS t | |¡S )Nr˜   rš   r›   rœ   r   rÜ   rÝ   rÞ   rß   rà   r:   r;   r4   )r¦   Úgetr§   r¨   r©   rª   r«   r¬   r­   r$   r%   r®   r   rH   )r-   rD   r   r   r   rH   	  s.   zTreeWidget.__getitem__c                 C   s   g S rI   r   rJ   r   r   r   rX   #  s   zTreeWidget._tagsc                 C   sJ   t | j ¡ ƒt | j ¡ ƒ }|D ]}| ¡ r"| ¡  | ¡  | ¡  qd S rI   )rž   r°   rÀ   r±   r   Úshowrâ   rØ   )r-   ÚsegsrÅ   r   r   r   rˆ   &  s   ÿ€üzTreeWidget._managec                 C   sÌ   |}|d r| j | j|  }n| j| j|  }| ¡ | u r+|  |¡ |  |¡ || _n| ¡  ||¡ | ¡  | 	¡  
¡ dd… \}}| 	¡  
¡ dd… \}}| || || ¡ | ¡  | ¡  |¡ dS )z)
        Collapse/expand a tree.
        r2   Nr[   )r°   r¯   r±   ÚparentrK   r*   r¶   rP   rä   r.   r\   rr   rØ   rB   )r-   rÙ   Úold_treesegÚnew_treesegÚnewxÚnewyÚoldxÚoldyr   r   r   Útoggle_collapsed0  s   

zTreeWidget.toggle_collapsedN)r6   )r‹   rŒ   r   rŽ   r
   r,   r»   r½   rÆ   rÉ   rÎ   rÐ   rÔ   rÕ   r´   rµ   rC   rH   rX   rˆ   rí   r   r   r   r   r¢   ý  s&    )
ÿ#

	
	
	
	
		?
r¢   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚTreeViewc           	      G   sB  ddl m}m} || _tƒ | _| j d¡ | j d| j¡ | j d| j¡ t	| jƒ }| _
| j d| j
j¡ t| jƒ| _| j d¡ d| j ¡  d	f}d| j ¡  f}t||t|ƒƒƒƒ| _g | _tt|ƒƒD ]'}t| ¡ || |d
ddddd|d
}| |j¡ | j |¡ | |dd¡ qg|  ¡  | j
jddd |  ¡  d S )Nr   )ÚceilÚsqrtÚNLTKz<Control-x>z<Control-q>z<Control-p>é   Ú	helveticaÚboldz#008040z#004080z#004040Úwhiter6   )Ú	node_fontÚ
leaf_colorÚ
node_colorrÞ   rß   rÜ   rá   Ú	leaf_fontÚboth)Úexpandr   )Úmathrï   rð   Ú_treesr   Ú_topÚtitleÚbindÚdestroyr   Ú_cframeÚprint_to_filer   Ú_sizeÚsetrã   rA   r_   Ú_widthÚ_widgetsrs   r¢   r   rÆ   rí   rV   Ú
add_widgetÚ_layoutÚpackÚ_init_menubar)	r-   Útreesrï   rð   Úcfrô   Úhelvr~   Úwidgetr   r   r   r,   V  sB   özTreeView.__init__c           	      C   sš   d } } }}| j }tt| jƒƒD ]8}| j| }| ¡ d d… \}}|| dkr-|}d}| || || ¡ | ¡ d d }t|| ¡ d d ƒ}qd S )Nr   r[   r   rY   )r  rs   r_   r  r\   rr   rb   )	r-   r~   r}   r   rj   r9   r  rë   rì   r   r   r   r	    s   
øzTreeView._layoutc                 C   sö   t | jƒ}t |dd}|jdd| jjdd |jdd| jdd |jd	d|d
 t |dd}|jd| jdd| j	d |jd| jdd| j	d |jd| jdd| j	d |jd| jdd| j	d |jd| jdd| j	d |jdd|d
 | jj
|d d S )Nr   )ÚtearoffzPrint to PostscriptzCtrl-p)r.   Ú	underlineÚcommandÚacceleratorÚExitr6   zCtrl-xÚFile)r.   r  ÚmenuÚTinyr   )r.   Úvariabler  rE   r  ÚSmallrò   ÚMediumé   ÚLargeé   ÚHugeé2   ÚZoom)r  )r   rþ   Úadd_commandr  r  r  Úadd_cascadeÚadd_radiobuttonr  ÚresizeÚconfig)r-   ÚmenubarÚfilemenuÚzoommenur   r   r   r  Ž  sd   
ü
ÿûûûûûzTreeView._init_menubarc                 G   s¦   d| j  ¡  df}d| j  ¡  f}| j  ¡ }| j  ¡ }| jD ].}||d< ||d< ||d< ||d< | j  ¡ dk r<d|d	< q| j  ¡ d
k rHd|d	< qd|d	< q|  ¡  d S )Nró   rô   rö   rù   r:   r;   r€   r6   rÝ   é   r[   rY   )r  rã   r  r	  )r-   Úerô   r  r:   r;   r  r   r   r   r$  Å  s   





zTreeView.resizec                 G   s"   | j d u rd S | j  ¡  d | _ d S rI   )rþ   r  )r-   r*  r   r   r   r  ×  s   


zTreeView.destroyc                 O   s    t ƒ rdS | jj|i |¤Ž dS )zñ
        Enter the Tkinter mainloop.  This function must be called if
        this demo is created from a non-interactive program (e.g.
        from a secript); otherwise, the demo will close as soon as
        the script completes.
        N)r   rþ   Úmainloop)r-   ÚargsÚkwargsr   r   r   r+  Ý  s   zTreeView.mainloopN)	r‹   rŒ   r   r,   r	  r  r$  r  r+  r   r   r   r   rî   U  s    +7rî   c                  G   s   t | Ž  ¡  dS )zd
    Open a new window containing a graphical diagram of the given
    trees.

    :rtype: None
    N)rî   r+  )r  r   r   r   Ú
draw_treesé  s   r.  c                     s   dd l ‰ ‡ fdd„} tdddd}t d¡}t| ¡ |d	d
dddddd	}| |dd¡ dd„ }dd„ }t d¡}t| ¡ |||d	d}‡ fdd„}‡ fdd„}	| |j¡ | |j¡ | 	|d¡ | 
d	¡ |	d¡ | 
¡  |	d¡ t| ¡ |ƒ}
| |
| ¡ d d d¡ t d¡}t| ¡ |dddd}d	|d< | |d| ¡ d d ¡ d d!„ }d"}t| ¡ | ¡ ƒ}t| ¡ |dd	d#}| || ¡ d d | ¡ d d ¡ t d$¡}t| ¡ |d	d%d%d&d'd(d)}| ¡  | || ¡ d d | ¡ d d ¡ | |¡ | |jd¡ | ¡  d S )*Nr   c                    ó   dˆ   dd¡ | d< d S )Nú#%06dr   i?B r   ©Úrandint)Úcw©Úrandomr   r   r   ü  ó   zdemo.<locals>.filli&  iÂ  r[   )r9   ÚheightÚcloseenoughzU
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))r6   )ró   iòÿÿÿrô   )ró   éôÿÿÿÚitalicrõ   ÚblackÚgreen4Úblue2)rá   rö   rù   rß   rÞ   r÷   rø   r   c                 S   s   d}t | t| ||dddS )N)ró   iðÿÿÿrô   )ÚfontÚgreenr   )r   r
   )r   ÚtextÚbigr   r   r   Úboxit  s   zdemo.<locals>.boxitc                 S   s   t | t| |ƒddS )NÚcyanr   )r   r
   )r   r@  r   r   r   Úovalit  s   zdemo.<locals>.ovalitz/(S (NP this tree) (VP (V is) (AdjP shapeable))))rà   c                    r/  )Nz#%04d00r   é'  r8   r1  )rÓ   r4  r   r   r8     r6  zdemo.<locals>.colorc                    s,   dˆ   dd¡ |  ¡ d< d|  ¡  ¡ d< d S )Nr0  r   rE  r   rõ   r8   )r2  r.   rS   )rÙ   r4  r   r   Úcolor2!  s   zdemo.<locals>.color2rY   zb
    (S (NP this tree) (AUX was)
       (VP (V built) (PP (P with) (NP (N tree_to_treesegment))))))Ú
tree_colorÚtree_xspaceÚ
tree_widthrá   c                 S   sè   | d dkr=|   dd¡ ¡ d  d¡ |  dd¡ ¡ d  d¡ |  d¡ ¡ d  d¡ |  ¡  ¡ d  d¡ d| d< d S |   dd¡ ¡ d  d¡ |  dd¡ ¡ d  d¡ |  d¡ ¡ d  d¡ |  ¡  ¡ d  d¡ d| d< d S )Nr4   r5   r6   r   r7   rY   )r»   r/   Úset_textr½   )Ú
treewidgetr   r   r   Úorientswitch9  s   zdemo.<locals>.orientswitchaM  
Try clicking, right clicking, and dragging
different elements of each of the trees.
The top-left tree is a TreeWidget built from
a Tree.  The top-right is a TreeWidget built
from a Tree, using non-default widget
constructors for the nodes & leaves (BoxWidget
and OvalWidget).  The bottom-left tree is
built from tree_to_treesegment.)r   rá   z/(S (NP this tree) (VP (V is) (Adj horizontal)))Úbrown2)ró   r9  rô   Úbrown4r5   )rá   rÜ   rÞ   rö   rø   r4   )r5  r   r   Ú
fromstringr¢   r   r  rÆ   rí   rÔ   r»   rÁ   r	   r\   r¡   r
   Ústripr   râ   r+  )r   r  r—   ÚtcrB  rD  ÚtreetokÚtc2r8   rF  ÚparenÚtree3Útc3rL  r@  ÚtwidgetÚtextboxÚtree4Útc4r   r4  r   Údemoù  sx   ÿ÷
ÿÿ	&
ø
&
r[  Ú__main__N)rŽ   Útkinterr   r   r   Únltk.draw.utilr   r   r   r   r	   r
   Ú	nltk.treer   Ú	nltk.utilr   r   r   r¡   r¢   rî   r.  r[  r‹   r   r   r   r   Ú<module>   s,       
ÿ9  Z o
ÿ