3
dk                 @   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re  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
| _	| j
| x|D ]}| j
| qdW d| _tj|  f| dS )zI
        :type node:
        :type subtrees: list(CanvasWidgetI)
        r   
      Fc          	      s    g | ]} j d d d d ddqS )r   z#006060)fill)create_line).0c)canvas ./tmp/pip-build-v9q4h5k9/nltk/nltk/draw/tree.py
<listcomp>P   s    z.TreeSegmentWidget.__init__.<locals>.<listcomp> hiddenz#006060)r   stateoutlineN)_label	_subtrees_horizontal_roof_xspace_yspace_ordered_linesZcreate_polygon_polygon_add_child_widget	_managingr   __init__)selfr   labelsubtreesattribssubtreer   )r   r   r'   @   s    

zTreeSegmentWidget.__init__c             C   s  | j  }|dkr~|| _| jrLx| jD ]}|j|dd q$W |j| jdd n.x| jD ]}|j|dd qTW |j| jdd n\|dkr|dkrd| _n|dkrd	| _ntd
n(|dkrx| jD ]}|j||d qW |j| j|d nt|to|d	 dkr$| jt	|d  }|j||d n|dkr@|j| j|d n|dkr|j| j||i x|| jD ]}|j|||i qdW nZ|dkr|dkr|| _
n|dkr|| _| j| j n |dkr|| _ntj| || d S )Nroofr   )r   Znormalorientation
horizontal   verticalr   z*orientation must be horizontal or verticalcolor)r   )r   r   widthxspaceyspaceordered)r4   r5   )r   r   r#   Z
itemconfigr$   r   
ValueError
isinstancetupleintr    r!   updater   r"   r   __setitem__)r(   attrvaluer   lr   r   r   r<   _   sL    





zTreeSegmentWidget.__setitem__c             C   s   |dkr| j S |dkr(| j j| j|S |dkrB| j j| jdS t|trz|d dkrz| jt|d  }| j j|dS |dkr| jS |d	kr| j	S |d
kr| j
rdS dS n|dkr| jS tj| |S d S )Nr-   r3   r2   r   r   r0   r   r4   r5   r.   r/   r1   r6   )r   r   Zitemcgetr$   r8   r9   r#   r:   r    r!   r   r"   r   __getitem__)r(   r=   r?   r   r   r   r@      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 )N)r   )r(   r   r   r   r*      s    zTreeSegmentWidget.subtreesc             C   s,   | j | j | j| || _| j| j dS )z2
        Set the node label to ``label``.
        N)_remove_child_widgetr   r%   r;   )r(   r)   r   r   r   	set_label   s    
zTreeSegmentWidget.set_labelc             C   s8   | j j|}|| j |< | j| | j| | j| dS )zC
        Replace the child ``oldchild`` with ``newchild``.
        N)r   indexrA   r%   r;   )r(   ZoldchildZnewchildrC   r   r   r   replace_child   s
    


zTreeSegmentWidget.replace_childc             C   sB   | j j|}| j |= | j| | j j| jj  | j| j d S )N)	r   rC   rA   r   deleter#   popr;   r   )r(   childrC   r   r   r   remove_child   s
    
zTreeSegmentWidget.remove_childc             C   sL   | j  }| jj|| | j| | jj|jdddddd | j| j d S )Nr   z#006060)r   )	r   r   insertr%   r#   appendr   r;   r   )r(   rC   rG   r   r   r   r   insert_child   s
    
zTreeSegmentWidget.insert_childc             C   s   | j r| jgS | jS d S )N)r   r$   r#   )r(   r   r   r   _tags   s    zTreeSegmentWidget._tagsc             C   sb   t |tr|j j }n|j }| jrB|d |d |d  d fS |d |d  d |d fS d S )Nr   r0      g       @   )r8   r   r)   bboxr   )r(   rG   rO   r   r   r   _subtree_top   s    
zTreeSegmentWidget._subtree_topc             C   sL   | j j }| jr,|d |d |d  d fS |d |d  d |d fS d S )NrN   r0   rM   g       @r   )r   rO   r   )r(   rO   r   r   r   _node_bottom   s    
zTreeSegmentWidget._node_bottomc          
   C   sl  t | jdkrd S | jj d kr$d S || jkr6| j}n|g}| jrT| j rT| j|}| j \}}| jd j \}}}}xV| jdd  D ]D}	|	j }
t||
d }t||
d }t	||
d }t	||
d }qW | j
r| j j| j||||||||	 n | j j| j||||||||	 xP|D ]H}	| j \}}| j| jj|	 }| j|	\}}| j j||||| qW d S )Nr   r0   rN   rM   )lenr   r   rO   r"   r&   _maintain_orderrQ   minmaxr   r   Zcoordsr$   r#   rC   rP   )r(   rG   Zneed_updatenodexnodeyZxminZyminZxmaxymaxr,   rO   lineZsubtreexZsubtreeyr   r   r   _update   s6    


zTreeSegmentWidget._updatec             C   s   | j r| j|S | j|S d S )N)r   _maintain_order_horizontal_maintain_order_vertical)r(   rG   r   r   r   rS     s    
z!TreeSegmentWidget._maintain_orderc             C   s  |j  \}}}}|| jkrdxB| jD ]8}|j  \}}}	}
|| j |kr"|jd|| j |  q"W | jS |g}| jj|}|| j }xrt|d t| jD ]Z}| j| j  \}}}	}
||kr| j| j|| d ||	| | j 7 }|j	| j|  qW || j }xrt|d ddD ]^}| j| j  \}}}	}
||	k r| j| j||	 d ||	| | j 8 }|j	| j|  qW | jj  \}}}	}
|
|| j kr| jjd|| j |
  | j}|S )Nr   r0   r]   )
rO   r   r   r!   moverC   r    rangerR   rJ   )r(   rG   lefttoprightbotr,   x1y1x2y2movedrC   xir   r   r   r\     s8    



z*TreeSegmentWidget._maintain_order_verticalc             C   s  |j  \}}}}|| jkrbx@| jD ]6}|j  \}}}	}
|| j |kr"|j|| j |  q"W | jS |g}| jj|}|| j }xrt|d t| jD ]Z}| j| j  \}}}	}
||kr| j| jd||  ||
| | j 7 }|j	| j|  qW || j }xrt|d ddD ]^}| j| j  \}}}	}
||
k r| j| jd||
  ||
| | j 8 }|j	| j|  qW | jj  \}}}	}
|	|| j kr| jj|| j |	 d | j}|S )Nr0   r   r]   r]   )
rO   r   r   r    r^   rC   r!   r_   rR   rJ   )r(   rG   r`   ra   rb   rc   r,   rd   re   rf   rg   rh   rC   yrj   r   r   r   r[   <  s8    



z,TreeSegmentWidget._maintain_order_horizontalc       	      C   s   | j  \}}d}xZ| jD ]P}|j }||d  | j }||d  }|j|| ||d |d  | j 7 }qW d}x | jD ]}|| j|d 7 }qxW |t| j }x| jD ]}|jd||  qW d S )N   r   r0   rM   g        )rQ   r   rO   r    r^   r!   rP   rR   )	r(   rV   rW   rk   r,   subtree_bboxdxdycenterr   r   r   _manage_horizontalf  s    z$TreeSegmentWidget._manage_horizontalc       	      C   s   | j  \}}d}xZ| jD ]P}|j }||d  | j }||d  }|j|| ||d |d  | j 7 }qW d}x*| jD ] }|| j|d t| j 7 }qxW x| jD ]}|j|| d qW d S )Nr   r0   rN   g        )rQ   r   rO   r!   r^   r    rP   rR   )	r(   rV   rW   ri   r,   rm   ro   rn   rp   r   r   r   _manage_vertical|  s     z"TreeSegmentWidget._manage_verticalc             C   s`   d| _ | j \}}t| jdkr$d S | jr4| j  n| j  x| jD ]}| j| qDW d| _ d S )NTr   F)r&   rQ   rR   r   r   rq   rr   rZ   )r(   rV   rW   r,   r   r   r   _manage  s    
zTreeSegmentWidget._managec             C   s   d| j  d| j dS )Nz	[TreeSeg z: ])r   r   )r(   r   r   r   __repr__  s    zTreeSegmentWidget.__repr__N)__name__
__module____qualname____doc__r'   r<   r@   r)   r*   rB   rD   rH   rK   rL   rP   rQ   rZ   rS   r\   r[   rq   rr   rs   ru   r   r   r   r   r      s*    ,	
	
)**r   c       
         s\   t |trJ |j f} fdd|D }	t ||	fS  |fS d S )Nc                s"   g | ]}t  |qS r   )_tree_to_treeseg)r   rG   )r   leaf_attribsloc_attribs	make_leaf	make_nodenode_attribstree_attribsr   r   r     s   
z$_tree_to_treeseg.<locals>.<listcomp>)r8   r   r)   r   )
r   tr~   r}   r   r   r{   r|   r)   r*   r   )r   r{   r|   r}   r~   r   r   r   rz     s    


rz   c          	   K   s   i }i }i }i }xt |j D ]\}	}
|	dd dkrH|
||	dd < q|	dd dkrj|
||	dd < q|	dd dkr|
||	dd < q|	dd dkr|
||	dd < qtd|	 qW 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   Ztree_node_leaf_   loc_zBad attribute: %s)listitemsr7   rz   )r   r   r~   r}   r+   r   r   r{   r|   keyr>   r   r   r   tree_to_treesegment  s.    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 | _| j||f  | j||f | _| j| j tj| |f| d S )	Nr2   z#008000z#008080r0   z#c0c0c0Fr   r1   )
_make_node
_make_leafZ_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}   r+   r   r   r   r'   &  s0    
zTreeWidget.__init__c             G   s
   | 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  s    	zTreeWidget.expanded_treec             G   s
   | 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(   r   r   r   r   collapsed_treeT  s    	zTreeWidget.collapsed_treer0   c             C   sL   x"t | jj D ]}|j|| qW x"t | jj D ]}|j|| q4W 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_  s    zTreeWidget.bind_click_treesc             C   sL   x"t | jj D ]}|j|| qW x"t | jj D ]}|j|| q4W dS )z5
        Add a binding to all tree segments.
        N)r   r   r   	bind_dragr   )r(   r   r   r   r   r   r   bind_drag_treesh  s    zTreeWidget.bind_drag_treesc             C   s<   x| j D ]}|j|| qW x| j D ]}|j|| q$W dS )z.
        Add a binding to all leaves.
        N)r   r   )r(   r   r   leafr   r   r   bind_click_leavesq  s    zTreeWidget.bind_click_leavesc             C   s<   x| j D ]}|j|| qW x| j D ]}|j|| q$W dS )z.
        Add a binding to all leaves.
        N)r   r   )r(   r   r   r   r   r   r   bind_drag_leavesz  s    zTreeWidget.bind_drag_leavesc             C   s<   x| j D ]}|j|| qW x| j D ]}|j|| q$W dS )z-
        Add a binding to all nodes.
        N)r   r   )r(   r   r   noder   r   r   bind_click_nodes  s    zTreeWidget.bind_click_nodesc             C   s<   x| j D ]}|j|| qW x| j D ]}|j|| q$W dS )z-
        Add a binding to all nodes.
        N)r   r   )r(   r   r   r   r   r   r   bind_drag_nodes  s    zTreeWidget.bind_drag_nodesc       
   	      s   t |tsd S j}j| |j fj}jj|  fdd|j D } j	|7  _	t
 ||djjjd}|j|< |j|< |j  x0tt|D ] }|| }	j |	||f  qW d S )Nc                s   g | ]} |fj qS r   )r   )r   r?   )r   r}   r(   r   r   r     s    z4TreeWidget._make_collapsed_trees.<locals>.<listcomp>r0   )r-   r2   r   r3   )r8   r   r   r   r)   r   r   rJ   leavesr   r   r   r   r   r   r   hider_   rR   r   )
r(   r   r   r   r~   r   r   treesegrj   rG   r   )r   r}   r(   r   r     s,    



z TreeWidget._make_collapsed_treesc       
         s   j }j}t|tr| |j fj}jj| | fddtt	D }t
 ||jjd}|j< j|< |S | |fj}	jj|	 |	S d S )Nc                s$   g | ]}j  | |f qS r   )r   )r   rj   )r   childrenr   r(   r   r   r     s   z2TreeWidget._make_expanded_tree.<locals>.<listcomp>)r2   r3   )r   r   r8   r   r)   r   r   rJ   r_   rR   r   r   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   sN  |d d dkr4x| j D ]}|||dd  < qW n|d d dkrhx| jD ]}|||dd  < qLW n|dkr|| _xt| jj D ]}||d< qW n|dkr|| _xt| jj D ]}||d< qW xt| jj D ]}||d< qW n^|dkr"|| _x t| jj D ]}||d< qW n(|d	krX|| _	x t| jj D ]}||d
< qBW n|dkr|| _
x t| jj D ]}||d< qxW x t| jj D ]}||d< qW x| jD ]}||d< qW n|dkr*|| _x t| jj D ]}||d< qW x t| jj D ]}||d< qW | j  n |dkr|| _x t| jj D ]}||d< qJW x t| jj D ]}||d< qlW | j  n|dkr|| _x t| jj D ]}||d< qW x t| jj D ]}||d< qW | j  nd|dkr<|| _x t| jj D ]}||d< qW x0t| jj D ]}||d< q(W ntj| || d S )Nr   r   r   
line_colorr2   
line_widthr3   
roof_color	roof_fillr   	shapeable	draggabler4   r5   r.   r6   )r   r   r   r   r   r   r   r   r   r   r   r    manager!   r   r"   r   r<   )r(   r=   r>   r   r   r   r   r   r   r<     sx    








zTreeWidget.__setitem__c             C   s   |d d dkr&| j j|dd  d S |d d dkrL| jj|dd  d S |d d dkrr| jj|dd  d S |dkr| jS |dkr| jS |dkr| jS |d	kr| jS |d
kr| jS |dkr| j	S |dkr| j
S |dkr| jS tj| |S d S )Nr   r   r   r   r   r   r   r   r   r   r4   r5   r.   )r   getr   r   r   r   r   r   r   r    r!   r   r   r@   )r(   r=   r   r   r   r@   	  s.    zTreeWidget.__getitem__c             C   s   g S )Nr   )r(   r   r   r   rL   #  s    zTreeWidget._tagsc             C   sN   t | jj t | jj  }x,|D ]$}|j r"|j  |j  |j  q"W d S )N)r   r   r   r   r   showr   r   )r(   Zsegsr   r   r   r   rs   &  s    
zTreeWidget._managec             C   s   |}|d r| j | j|  }n| j| j|  }|j | krV| j| | j| || _n|j j|| |j  |j	 j
 dd \}}|j	 j
 dd \}}|j|| ||  |j  |j j| dS )z)
        Collapse/expand a tree.
        r-   NrN   )r   r   r   parentrA   r%   r   rD   r   r)   rO   r^   r   r;   )r(   r   Zold_treesegZnew_treesegZnewxZnewyoldxoldyr   r   r   toggle_collapsed0  s    

zTreeWidget.toggle_collapsedN)r0   )r0   )r0   )r0   )r0   )r0   )rv   rw   rx   ry   r
   r'   r   r   r   r   r   r   r   r   r   r   r<   r@   rL   rs   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   sF  ddl m}m} || _t | _| jjd | jjd| j | jjd| j t	| j }| _
| jjd| j
j t| j| _| jjd d| jj  d	f}d| jj  f}t||t|| _g | _x^tt|D ]N}t|j || |d
ddddd|d
}|j|j | jj| |j|dd qW | j  | j
jddd | j  d S )Nr   )ceilsqrtZNLTKz<Control-x>z<Control-q>z<Control-p>   	helveticaboldz#008040z#004080z#004040whiter0   )	node_font
leaf_color
node_colorr   r   r   r   	leaf_fontZboth)expandr   )mathr   r   Z_treesr   _toptitlebinddestroyr   _cframeprint_to_filer   _sizesetr   r:   rR   _width_widgetsr_   r   r   r   r   rJ   
add_widget_layoutpack_init_menubar)	r(   treesr   r   cfr   helvrj   widgetr   r   r   r'   V  s@    zTreeView.__init__c       	      C   s   d } } }}| j }xtt| jD ]p}| j| }|j d d \}}|| dkr\|}d}|j|| ||  |j d d }t||j d d }q&W d S )Nr   rN   r   rM   )r   r_   rR   r   rO   r^   rU   )	r(   rj   ri   rk   rX   r3   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   )ZtearoffzPrint to PostscriptzCtrl-p)r)   	underlinecommandZacceleratorZExitr0   zCtrl-xZFile)r)   r   menuZTinyr   )r)   variabler   r>   r   ZSmallr   ZMedium   ZLarge   ZHuge2   ZZoom)r   )r   r   Zadd_commandr   r   r   Zadd_cascadeZadd_radiobuttonr   resizeconfig)r(   ZmenubarZfilemenuZzoommenur   r   r   r     sV    





zTreeView._init_menubarc             G   s   d| j j  df}d| j j  f}| j j }| j j }xf| jD ]\}||d< ||d< ||d< ||d< | j j dk rzd|d	< q>| j j d
k rd|d	< q>d|d	< q>W | j  d S )Nr   r   r   r   r4   r5   rl   r0   r      rN   rM   )r   r   r   r   )r(   er   r   r4   r5   r   r   r   r   r     s    



zTreeView.resizec             G   s"   | j d krd S | j j  d | _ d S )N)r   r   )r(   r   r   r   r   r     s    

zTreeView.destroyc             O   s   t  r
dS | jj|| 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)	rv   rw   rx   r'   r   r   r   r   r   r   r   r   r   r   U  s   +7r   c              G   s   t |  j  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jd}t|j |d	d-d/ddddd	}|j|dd dd }dd }tjd}t|j |||d	d} fdd} fdd}	|j|j |j|j |j	|d |j
d	j|	d |j
 j|	d t|j |}
|j|
|j d d d tjd }t|j |dddd!}d	|d"< |j|d|j d d  d#d$ }d%}t|j |j }t|j |dd	d&}|j||j d d |j d d  tjd'}t|j |d	d(d(d1d)d*d+}|j  |j||j d d |j d d  |j| |j|jd |j  d S )2Nr   c                s   d j dd | d< d S )Nz#%06dr   i?B r   )randint)Zcw)randomr   r   r     s    zdemo.<locals>.filli&  i  rN   )r3   heightZcloseenoughzU
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))r0   r   r   r   r   italicr   ZblackZgreen4Zblue2)r   r   r   r   r   r   r   r   c             S   s   d}t | t| ||dddS )	Nr      r   )fontZgreen)r   )r   r   r   )r   r
   )r   textbigr   r   r   boxit  s    zdemo.<locals>.boxitc             S   s   t | t| |ddS )NZcyan)r   )r   r
   )r   r   r   r   r   ovalit  s    zdemo.<locals>.ovalitz/(S (NP this tree) (VP (V is) (AdjP shapeable))))r   c                s   d j dd | d< d S )Nz#%04d00r   i'  r2   )r   )r   )r   r   r   r2     s    zdemo.<locals>.colorc                s,   d j dd | j d< d| j j d< d S )Nz#%06dr   i'  r   r   r2   )r   r)   rG   )r   )r   r   r   color2!  s    zdemo.<locals>.color2rM   zb
    (S (NP this tree) (AUX was)
       (VP (V built) (PP (P with) (NP (N tree_to_treesegment))))))Z
tree_colorZtree_xspaceZ
tree_widthr   c             S   s   | d dkrx| j ddj d jd | jddj d jd | jdj d jd | j j d jd d| d< nj| j ddj d jd | jddj d jd | jdj d jd | j j d jd d| d< d S )Nr.   r/   r0   r   r1   rM   )r   r*   Zset_textr   )Z
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)))Zbrown2Zbrown4r/   )r   r   r   r   r   r.   )r   r   r   )r   r   r   )r   r   r   )r   r   r   
fromstringr   r   r   r   r   r   r   r   r	   rO   r   r
   stripr   r   r   )r   r   r   Ztcr   r   ZtreetokZtc2r2   r   ZparenZtree3Ztc3r   r   ZtwidgetZtextboxZtree4Ztc4r   )r   r   demo  sn    
&
&
r  __main__N)ry   tkinterr   r   r   Znltk.draw.utilr   r   r   r   r	   r
   Z	nltk.treer   Z	nltk.utilr   r   rz   r   r   r   r   r  rv   r   r   r   r   <module>
   s&       8  Z o