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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
# Natural Language Toolkit: Drawing utilities
#
# Copyright (C) 2001-2023 NLTK Project
# Author: Edward Loper <[email protected]>
# URL: <https://www.nltk.org/>
# For license information, see LICENSE.TXT

"""

Tools for graphically displaying and interacting with the objects and

processing classes defined by the Toolkit.  These tools are primarily

intended to help students visualize the objects that they create.



The graphical tools are typically built using "canvas widgets", each

of which encapsulates the graphical elements and bindings used to

display a complex object on a Tkinter ``Canvas``.  For example, NLTK

defines canvas widgets for displaying trees and directed graphs, as

well as a number of simpler widgets.  These canvas widgets make it

easier to build new graphical tools and demos.  See the class

documentation for ``CanvasWidget`` for more information.



The ``nltk.draw`` module defines the abstract ``CanvasWidget`` base

class, and a number of simple canvas widgets.  The remaining canvas

widgets are defined by submodules, such as ``nltk.draw.tree``.



The ``nltk.draw`` module also defines ``CanvasFrame``, which

encapsulates a ``Canvas`` and its scrollbars.  It uses a

``ScrollWatcherWidget`` to ensure that all canvas widgets contained on

its canvas are within the scroll region.



Acknowledgements: Many of the ideas behind the canvas widget system

are derived from ``CLIG``, a Tk-based grapher for linguistic data

structures.  For more information, see the CLIG

homepage (http://www.ags.uni-sb.de/~konrad/clig.html).



"""
from abc import ABCMeta, abstractmethod
from tkinter import (
    RAISED,
    Button,
    Canvas,
    Entry,
    Frame,
    Label,
    Menu,
    Menubutton,
    Scrollbar,
    StringVar,
    Text,
    Tk,
    Toplevel,
    Widget,
)
from tkinter.filedialog import asksaveasfilename

from nltk.util import in_idle

##//////////////////////////////////////////////////////
##  CanvasWidget
##//////////////////////////////////////////////////////


class CanvasWidget(metaclass=ABCMeta):
    """

    A collection of graphical elements and bindings used to display a

    complex object on a Tkinter ``Canvas``.  A canvas widget is

    responsible for managing the ``Canvas`` tags and callback bindings

    necessary to display and interact with the object.  Canvas widgets

    are often organized into hierarchies, where parent canvas widgets

    control aspects of their child widgets.



    Each canvas widget is bound to a single ``Canvas``.  This ``Canvas``

    is specified as the first argument to the ``CanvasWidget``'s

    constructor.



    Attributes.  Each canvas widget can support a variety of

    "attributes", which control how the canvas widget is displayed.

    Some typical examples attributes are ``color``, ``font``, and

    ``radius``.  Each attribute has a default value.  This default

    value can be overridden in the constructor, using keyword

    arguments of the form ``attribute=value``:



        >>> from nltk.draw.util import TextWidget

        >>> cn = TextWidget(Canvas(), 'test', color='red')  # doctest: +SKIP



    Attribute values can also be changed after a canvas widget has

    been constructed, using the ``__setitem__`` operator:



        >>> cn['font'] = 'times'  # doctest: +SKIP



    The current value of an attribute value can be queried using the

    ``__getitem__`` operator:



        >>> cn['color']  # doctest: +SKIP

        'red'



    For a list of the attributes supported by a type of canvas widget,

    see its class documentation.



    Interaction.  The attribute ``'draggable'`` controls whether the

    user can drag a canvas widget around the canvas.  By default,

    canvas widgets are not draggable.



    ``CanvasWidget`` provides callback support for two types of user

    interaction: clicking and dragging.  The method ``bind_click``

    registers a callback function that is called whenever the canvas

    widget is clicked.  The method ``bind_drag`` registers a callback

    function that is called after the canvas widget is dragged.  If

    the user clicks or drags a canvas widget with no registered

    callback function, then the interaction event will propagate to

    its parent.  For each canvas widget, only one callback function

    may be registered for an interaction event.  Callback functions

    can be deregistered with the ``unbind_click`` and ``unbind_drag``

    methods.



    Subclassing.  ``CanvasWidget`` is an abstract class.  Subclasses

    are required to implement the following methods:



      - ``__init__``: Builds a new canvas widget.  It must perform the

        following three tasks (in order):



          - Create any new graphical elements.

          - Call ``_add_child_widget`` on each child widget.

          - Call the ``CanvasWidget`` constructor.

      - ``_tags``: Returns a list of the canvas tags for all graphical

        elements managed by this canvas widget, not including

        graphical elements managed by its child widgets.

      - ``_manage``: Arranges the child widgets of this canvas widget.

        This is typically only called when the canvas widget is

        created.

      - ``_update``: Update this canvas widget in response to a

        change in a single child.



    For a ``CanvasWidget`` with no child widgets, the default

    definitions for ``_manage`` and ``_update`` may be used.



    If a subclass defines any attributes, then it should implement

    ``__getitem__`` and ``__setitem__``.  If either of these methods is

    called with an unknown attribute, then they should propagate the

    request to ``CanvasWidget``.



    Most subclasses implement a number of additional methods that

    modify the ``CanvasWidget`` in some way.  These methods must call

    ``parent.update(self)`` after making any changes to the canvas

    widget's graphical elements.  The canvas widget must also call

    ``parent.update(self)`` after changing any attribute value that

    affects the shape or position of the canvas widget's graphical

    elements.



    :type __canvas: Tkinter.Canvas

    :ivar __canvas: This ``CanvasWidget``'s canvas.



    :type __parent: CanvasWidget or None

    :ivar __parent: This ``CanvasWidget``'s hierarchical parent widget.

    :type __children: list(CanvasWidget)

    :ivar __children: This ``CanvasWidget``'s hierarchical child widgets.



    :type __updating: bool

    :ivar __updating: Is this canvas widget currently performing an

        update?  If it is, then it will ignore any new update requests

        from child widgets.



    :type __draggable: bool

    :ivar __draggable: Is this canvas widget draggable?

    :type __press: event

    :ivar __press: The ButtonPress event that we're currently handling.

    :type __drag_x: int

    :ivar __drag_x: Where it's been moved to (to find dx)

    :type __drag_y: int

    :ivar __drag_y: Where it's been moved to (to find dy)

    :type __callbacks: dictionary

    :ivar __callbacks: Registered callbacks.  Currently, four keys are

        used: ``1``, ``2``, ``3``, and ``'drag'``.  The values are

        callback functions.  Each callback function takes a single

        argument, which is the ``CanvasWidget`` that triggered the

        callback.

    """

    def __init__(self, canvas, parent=None, **attribs):
        """

        Create a new canvas widget.  This constructor should only be

        called by subclass constructors; and it should be called only

        "after" the subclass has constructed all graphical canvas

        objects and registered all child widgets.



        :param canvas: This canvas widget's canvas.

        :type canvas: Tkinter.Canvas

        :param parent: This canvas widget's hierarchical parent.

        :type parent: CanvasWidget

        :param attribs: The new canvas widget's attributes.

        """
        if self.__class__ == CanvasWidget:
            raise TypeError("CanvasWidget is an abstract base class")

        if not isinstance(canvas, Canvas):
            raise TypeError("Expected a canvas!")

        self.__canvas = canvas
        self.__parent = parent

        # If the subclass constructor called _add_child_widget, then
        # self.__children will already exist.
        if not hasattr(self, "_CanvasWidget__children"):
            self.__children = []

        # Is this widget hidden?
        self.__hidden = 0

        # Update control (prevents infinite loops)
        self.__updating = 0

        # Button-press and drag callback handling.
        self.__press = None
        self.__drag_x = self.__drag_y = 0
        self.__callbacks = {}
        self.__draggable = 0

        # Set up attributes.
        for (attr, value) in list(attribs.items()):
            self[attr] = value

        # Manage this canvas widget
        self._manage()

        # Register any new bindings
        for tag in self._tags():
            self.__canvas.tag_bind(tag, "<ButtonPress-1>", self.__press_cb)
            self.__canvas.tag_bind(tag, "<ButtonPress-2>", self.__press_cb)
            self.__canvas.tag_bind(tag, "<ButtonPress-3>", self.__press_cb)

    ##//////////////////////////////////////////////////////
    ##  Inherited methods.
    ##//////////////////////////////////////////////////////

    def bbox(self):
        """

        :return: A bounding box for this ``CanvasWidget``. The bounding

            box is a tuple of four coordinates, *(xmin, ymin, xmax, ymax)*,

            for a rectangle which encloses all of the canvas

            widget's graphical elements.  Bounding box coordinates are

            specified with respect to the coordinate space of the ``Canvas``.

        :rtype: tuple(int, int, int, int)

        """
        if self.__hidden:
            return (0, 0, 0, 0)
        if len(self.tags()) == 0:
            raise ValueError("No tags")
        return self.__canvas.bbox(*self.tags())

    def width(self):
        """

        :return: The width of this canvas widget's bounding box, in

            its ``Canvas``'s coordinate space.

        :rtype: int

        """
        if len(self.tags()) == 0:
            raise ValueError("No tags")
        bbox = self.__canvas.bbox(*self.tags())
        return bbox[2] - bbox[0]

    def height(self):
        """

        :return: The height of this canvas widget's bounding box, in

            its ``Canvas``'s coordinate space.

        :rtype: int

        """
        if len(self.tags()) == 0:
            raise ValueError("No tags")
        bbox = self.__canvas.bbox(*self.tags())
        return bbox[3] - bbox[1]

    def parent(self):
        """

        :return: The hierarchical parent of this canvas widget.

            ``self`` is considered a subpart of its parent for

            purposes of user interaction.

        :rtype: CanvasWidget or None

        """
        return self.__parent

    def child_widgets(self):
        """

        :return: A list of the hierarchical children of this canvas

            widget.  These children are considered part of ``self``

            for purposes of user interaction.

        :rtype: list of CanvasWidget

        """
        return self.__children

    def canvas(self):
        """

        :return: The canvas that this canvas widget is bound to.

        :rtype: Tkinter.Canvas

        """
        return self.__canvas

    def move(self, dx, dy):
        """

        Move this canvas widget by a given distance.  In particular,

        shift the canvas widget right by ``dx`` pixels, and down by

        ``dy`` pixels.  Both ``dx`` and ``dy`` may be negative, resulting

        in leftward or upward movement.



        :type dx: int

        :param dx: The number of pixels to move this canvas widget

            rightwards.

        :type dy: int

        :param dy: The number of pixels to move this canvas widget

            downwards.

        :rtype: None

        """
        if dx == dy == 0:
            return
        for tag in self.tags():
            self.__canvas.move(tag, dx, dy)
        if self.__parent:
            self.__parent.update(self)

    def moveto(self, x, y, anchor="NW"):
        """

        Move this canvas widget to the given location.  In particular,

        shift the canvas widget such that the corner or side of the

        bounding box specified by ``anchor`` is at location (``x``,

        ``y``).



        :param x,y: The location that the canvas widget should be moved

            to.

        :param anchor: The corner or side of the canvas widget that

            should be moved to the specified location.  ``'N'``

            specifies the top center; ``'NE'`` specifies the top right

            corner; etc.

        """
        x1, y1, x2, y2 = self.bbox()
        if anchor == "NW":
            self.move(x - x1, y - y1)
        if anchor == "N":
            self.move(x - x1 / 2 - x2 / 2, y - y1)
        if anchor == "NE":
            self.move(x - x2, y - y1)
        if anchor == "E":
            self.move(x - x2, y - y1 / 2 - y2 / 2)
        if anchor == "SE":
            self.move(x - x2, y - y2)
        if anchor == "S":
            self.move(x - x1 / 2 - x2 / 2, y - y2)
        if anchor == "SW":
            self.move(x - x1, y - y2)
        if anchor == "W":
            self.move(x - x1, y - y1 / 2 - y2 / 2)

    def destroy(self):
        """

        Remove this ``CanvasWidget`` from its ``Canvas``.  After a

        ``CanvasWidget`` has been destroyed, it should not be accessed.



        Note that you only need to destroy a top-level

        ``CanvasWidget``; its child widgets will be destroyed

        automatically.  If you destroy a non-top-level

        ``CanvasWidget``, then the entire top-level widget will be

        destroyed.



        :raise ValueError: if this ``CanvasWidget`` has a parent.

        :rtype: None

        """
        if self.__parent is not None:
            self.__parent.destroy()
            return

        for tag in self.tags():
            self.__canvas.tag_unbind(tag, "<ButtonPress-1>")
            self.__canvas.tag_unbind(tag, "<ButtonPress-2>")
            self.__canvas.tag_unbind(tag, "<ButtonPress-3>")
        self.__canvas.delete(*self.tags())
        self.__canvas = None

    def update(self, child):
        """

        Update the graphical display of this canvas widget, and all of

        its ancestors, in response to a change in one of this canvas

        widget's children.



        :param child: The child widget that changed.

        :type child: CanvasWidget

        """
        if self.__hidden or child.__hidden:
            return
        # If we're already updating, then do nothing.  This prevents
        # infinite loops when _update modifies its children.
        if self.__updating:
            return
        self.__updating = 1

        # Update this CanvasWidget.
        self._update(child)

        # Propagate update request to the parent.
        if self.__parent:
            self.__parent.update(self)

        # We're done updating.
        self.__updating = 0

    def manage(self):
        """

        Arrange this canvas widget and all of its descendants.



        :rtype: None

        """
        if self.__hidden:
            return
        for child in self.__children:
            child.manage()
        self._manage()

    def tags(self):
        """

        :return: a list of the canvas tags for all graphical

            elements managed by this canvas widget, including

            graphical elements managed by its child widgets.

        :rtype: list of int

        """
        if self.__canvas is None:
            raise ValueError("Attempt to access a destroyed canvas widget")
        tags = []
        tags += self._tags()
        for child in self.__children:
            tags += child.tags()
        return tags

    def __setitem__(self, attr, value):
        """

        Set the value of the attribute ``attr`` to ``value``.  See the

        class documentation for a list of attributes supported by this

        canvas widget.



        :rtype: None

        """
        if attr == "draggable":
            self.__draggable = value
        else:
            raise ValueError("Unknown attribute %r" % attr)

    def __getitem__(self, attr):
        """

        :return: the value of the attribute ``attr``.  See the class

            documentation for a list of attributes supported by this

            canvas widget.

        :rtype: (any)

        """
        if attr == "draggable":
            return self.__draggable
        else:
            raise ValueError("Unknown attribute %r" % attr)

    def __repr__(self):
        """

        :return: a string representation of this canvas widget.

        :rtype: str

        """
        return "<%s>" % self.__class__.__name__

    def hide(self):
        """

        Temporarily hide this canvas widget.



        :rtype: None

        """
        self.__hidden = 1
        for tag in self.tags():
            self.__canvas.itemconfig(tag, state="hidden")

    def show(self):
        """

        Show a hidden canvas widget.



        :rtype: None

        """
        self.__hidden = 0
        for tag in self.tags():
            self.__canvas.itemconfig(tag, state="normal")

    def hidden(self):
        """

        :return: True if this canvas widget is hidden.

        :rtype: bool

        """
        return self.__hidden

    ##//////////////////////////////////////////////////////
    ##  Callback interface
    ##//////////////////////////////////////////////////////

    def bind_click(self, callback, button=1):
        """

        Register a new callback that will be called whenever this

        ``CanvasWidget`` is clicked on.



        :type callback: function

        :param callback: The callback function that will be called

            whenever this ``CanvasWidget`` is clicked.  This function

            will be called with this ``CanvasWidget`` as its argument.

        :type button: int

        :param button: Which button the user should use to click on

            this ``CanvasWidget``.  Typically, this should be 1 (left

            button), 3 (right button), or 2 (middle button).

        """
        self.__callbacks[button] = callback

    def bind_drag(self, callback):
        """

        Register a new callback that will be called after this

        ``CanvasWidget`` is dragged.  This implicitly makes this

        ``CanvasWidget`` draggable.



        :type callback: function

        :param callback: The callback function that will be called

            whenever this ``CanvasWidget`` is clicked.  This function

            will be called with this ``CanvasWidget`` as its argument.

        """
        self.__draggable = 1
        self.__callbacks["drag"] = callback

    def unbind_click(self, button=1):
        """

        Remove a callback that was registered with ``bind_click``.



        :type button: int

        :param button: Which button the user should use to click on

            this ``CanvasWidget``.  Typically, this should be 1 (left

            button), 3 (right button), or 2 (middle button).

        """
        try:
            del self.__callbacks[button]
        except:
            pass

    def unbind_drag(self):
        """

        Remove a callback that was registered with ``bind_drag``.

        """
        try:
            del self.__callbacks["drag"]
        except:
            pass

    ##//////////////////////////////////////////////////////
    ##  Callback internals
    ##//////////////////////////////////////////////////////

    def __press_cb(self, event):
        """

        Handle a button-press event:

          - record the button press event in ``self.__press``

          - register a button-release callback.

          - if this CanvasWidget or any of its ancestors are

            draggable, then register the appropriate motion callback.

        """
        # If we're already waiting for a button release, then ignore
        # this new button press.
        if (
            self.__canvas.bind("<ButtonRelease-1>")
            or self.__canvas.bind("<ButtonRelease-2>")
            or self.__canvas.bind("<ButtonRelease-3>")
        ):
            return

        # Unbind motion (just in case; this shouldn't be necessary)
        self.__canvas.unbind("<Motion>")

        # Record the button press event.
        self.__press = event

        # If any ancestor is draggable, set up a motion callback.
        # (Only if they pressed button number 1)
        if event.num == 1:
            widget = self
            while widget is not None:
                if widget["draggable"]:
                    widget.__start_drag(event)
                    break
                widget = widget.parent()

        # Set up the button release callback.
        self.__canvas.bind("<ButtonRelease-%d>" % event.num, self.__release_cb)

    def __start_drag(self, event):
        """

        Begin dragging this object:

          - register a motion callback

          - record the drag coordinates

        """
        self.__canvas.bind("<Motion>", self.__motion_cb)
        self.__drag_x = event.x
        self.__drag_y = event.y

    def __motion_cb(self, event):
        """

        Handle a motion event:

          - move this object to the new location

          - record the new drag coordinates

        """
        self.move(event.x - self.__drag_x, event.y - self.__drag_y)
        self.__drag_x = event.x
        self.__drag_y = event.y

    def __release_cb(self, event):
        """

        Handle a release callback:

          - unregister motion & button release callbacks.

          - decide whether they clicked, dragged, or cancelled

          - call the appropriate handler.

        """
        # Unbind the button release & motion callbacks.
        self.__canvas.unbind("<ButtonRelease-%d>" % event.num)
        self.__canvas.unbind("<Motion>")

        # Is it a click or a drag?
        if (
            event.time - self.__press.time < 100
            and abs(event.x - self.__press.x) + abs(event.y - self.__press.y) < 5
        ):
            # Move it back, if we were dragging.
            if self.__draggable and event.num == 1:
                self.move(
                    self.__press.x - self.__drag_x, self.__press.y - self.__drag_y
                )
            self.__click(event.num)
        elif event.num == 1:
            self.__drag()

        self.__press = None

    def __drag(self):
        """

        If this ``CanvasWidget`` has a drag callback, then call it;

        otherwise, find the closest ancestor with a drag callback, and

        call it.  If no ancestors have a drag callback, do nothing.

        """
        if self.__draggable:
            if "drag" in self.__callbacks:
                cb = self.__callbacks["drag"]
                try:
                    cb(self)
                except:
                    print("Error in drag callback for %r" % self)
        elif self.__parent is not None:
            self.__parent.__drag()

    def __click(self, button):
        """

        If this ``CanvasWidget`` has a drag callback, then call it;

        otherwise, find the closest ancestor with a click callback, and

        call it.  If no ancestors have a click callback, do nothing.

        """
        if button in self.__callbacks:
            cb = self.__callbacks[button]
            # try:
            cb(self)
            # except:
            #    print('Error in click callback for %r' % self)
            #    raise
        elif self.__parent is not None:
            self.__parent.__click(button)

    ##//////////////////////////////////////////////////////
    ##  Child/parent Handling
    ##//////////////////////////////////////////////////////

    def _add_child_widget(self, child):
        """

        Register a hierarchical child widget.  The child will be

        considered part of this canvas widget for purposes of user

        interaction.  ``_add_child_widget`` has two direct effects:

          - It sets ``child``'s parent to this canvas widget.

          - It adds ``child`` to the list of canvas widgets returned by

            the ``child_widgets`` member function.



        :param child: The new child widget.  ``child`` must not already

            have a parent.

        :type child: CanvasWidget

        """
        if not hasattr(self, "_CanvasWidget__children"):
            self.__children = []
        if child.__parent is not None:
            raise ValueError(f"{child} already has a parent")
        child.__parent = self
        self.__children.append(child)

    def _remove_child_widget(self, child):
        """

        Remove a hierarchical child widget.  This child will no longer

        be considered part of this canvas widget for purposes of user

        interaction.  ``_add_child_widget`` has two direct effects:

          - It sets ``child``'s parent to None.

          - It removes ``child`` from the list of canvas widgets

            returned by the ``child_widgets`` member function.



        :param child: The child widget to remove.  ``child`` must be a

            child of this canvas widget.

        :type child: CanvasWidget

        """
        self.__children.remove(child)
        child.__parent = None

    ##//////////////////////////////////////////////////////
    ##  Defined by subclass
    ##//////////////////////////////////////////////////////

    @abstractmethod
    def _tags(self):
        """

        :return: a list of canvas tags for all graphical elements

            managed by this canvas widget, not including graphical

            elements managed by its child widgets.

        :rtype: list of int

        """

    def _manage(self):
        """

        Arrange the child widgets of this canvas widget.  This method

        is called when the canvas widget is initially created.  It is

        also called if the user calls the ``manage`` method on this

        canvas widget or any of its ancestors.



        :rtype: None

        """

    def _update(self, child):
        """

        Update this canvas widget in response to a change in one of

        its children.



        :param child: The child that changed.

        :type child: CanvasWidget

        :rtype: None

        """


##//////////////////////////////////////////////////////
##  Basic widgets.
##//////////////////////////////////////////////////////


class TextWidget(CanvasWidget):
    """

    A canvas widget that displays a single string of text.



    Attributes:

      - ``color``: the color of the text.

      - ``font``: the font used to display the text.

      - ``justify``: justification for multi-line texts.  Valid values

        are ``left``, ``center``, and ``right``.

      - ``width``: the width of the text.  If the text is wider than

        this width, it will be line-wrapped at whitespace.

      - ``draggable``: whether the text can be dragged by the user.

    """

    def __init__(self, canvas, text, **attribs):
        """

        Create a new text widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :type text: str

        :param text: The string of text to display.

        :param attribs: The new canvas widget's attributes.

        """
        self._text = text
        self._tag = canvas.create_text(1, 1, text=text)
        CanvasWidget.__init__(self, canvas, **attribs)

    def __setitem__(self, attr, value):
        if attr in ("color", "font", "justify", "width"):
            if attr == "color":
                attr = "fill"
            self.canvas().itemconfig(self._tag, {attr: value})
        else:
            CanvasWidget.__setitem__(self, attr, value)

    def __getitem__(self, attr):
        if attr == "width":
            return int(self.canvas().itemcget(self._tag, attr))
        elif attr in ("color", "font", "justify"):
            if attr == "color":
                attr = "fill"
            return self.canvas().itemcget(self._tag, attr)
        else:
            return CanvasWidget.__getitem__(self, attr)

    def _tags(self):
        return [self._tag]

    def text(self):
        """

        :return: The text displayed by this text widget.

        :rtype: str

        """
        return self.canvas().itemcget(self._tag, "TEXT")

    def set_text(self, text):
        """

        Change the text that is displayed by this text widget.



        :type text: str

        :param text: The string of text to display.

        :rtype: None

        """
        self.canvas().itemconfig(self._tag, text=text)
        if self.parent() is not None:
            self.parent().update(self)

    def __repr__(self):
        return "[Text: %r]" % self._text


class SymbolWidget(TextWidget):
    """

    A canvas widget that displays special symbols, such as the

    negation sign and the exists operator.  Symbols are specified by

    name.  Currently, the following symbol names are defined: ``neg``,

    ``disj``, ``conj``, ``lambda``, ``merge``, ``forall``, ``exists``,

    ``subseteq``, ``subset``, ``notsubset``, ``emptyset``, ``imp``,

    ``rightarrow``, ``equal``, ``notequal``, ``epsilon``.



    Attributes:



    - ``color``: the color of the text.

    - ``draggable``: whether the text can be dragged by the user.



    :cvar SYMBOLS: A dictionary mapping from symbols to the character

        in the ``symbol`` font used to render them.

    """

    SYMBOLS = {
        "neg": "\330",
        "disj": "\332",
        "conj": "\331",
        "lambda": "\154",
        "merge": "\304",
        "forall": "\042",
        "exists": "\044",
        "subseteq": "\315",
        "subset": "\314",
        "notsubset": "\313",
        "emptyset": "\306",
        "imp": "\336",
        "rightarrow": chr(222),  #'\256',
        "equal": "\75",
        "notequal": "\271",
        "intersection": "\307",
        "union": "\310",
        "epsilon": "e",
    }

    def __init__(self, canvas, symbol, **attribs):
        """

        Create a new symbol widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :type symbol: str

        :param symbol: The name of the symbol to display.

        :param attribs: The new canvas widget's attributes.

        """
        attribs["font"] = "symbol"
        TextWidget.__init__(self, canvas, "", **attribs)
        self.set_symbol(symbol)

    def symbol(self):
        """

        :return: the name of the symbol that is displayed by this

            symbol widget.

        :rtype: str

        """
        return self._symbol

    def set_symbol(self, symbol):
        """

        Change the symbol that is displayed by this symbol widget.



        :type symbol: str

        :param symbol: The name of the symbol to display.

        """
        if symbol not in SymbolWidget.SYMBOLS:
            raise ValueError("Unknown symbol: %s" % symbol)
        self._symbol = symbol
        self.set_text(SymbolWidget.SYMBOLS[symbol])

    def __repr__(self):
        return "[Symbol: %r]" % self._symbol

    @staticmethod
    def symbolsheet(size=20):
        """

        Open a new Tkinter window that displays the entire alphabet

        for the symbol font.  This is useful for constructing the

        ``SymbolWidget.SYMBOLS`` dictionary.

        """
        top = Tk()

        def destroy(e, top=top):
            top.destroy()

        top.bind("q", destroy)
        Button(top, text="Quit", command=top.destroy).pack(side="bottom")
        text = Text(top, font=("helvetica", -size), width=20, height=30)
        text.pack(side="left")
        sb = Scrollbar(top, command=text.yview)
        text["yscrollcommand"] = sb.set
        sb.pack(side="right", fill="y")
        text.tag_config("symbol", font=("symbol", -size))
        for i in range(256):
            if i in (0, 10):
                continue  # null and newline
            for k, v in list(SymbolWidget.SYMBOLS.items()):
                if v == chr(i):
                    text.insert("end", "%-10s\t" % k)
                    break
            else:
                text.insert("end", "%-10d  \t" % i)
            text.insert("end", "[%s]\n" % chr(i), "symbol")
        top.mainloop()


class AbstractContainerWidget(CanvasWidget):
    """

    An abstract class for canvas widgets that contain a single child,

    such as ``BoxWidget`` and ``OvalWidget``.  Subclasses must define

    a constructor, which should create any new graphical elements and

    then call the ``AbstractCanvasContainer`` constructor.  Subclasses

    must also define the ``_update`` method and the ``_tags`` method;

    and any subclasses that define attributes should define

    ``__setitem__`` and ``__getitem__``.

    """

    def __init__(self, canvas, child, **attribs):
        """

        Create a new container widget.  This constructor should only

        be called by subclass constructors.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :param child: The container's child widget.  ``child`` must not

            have a parent.

        :type child: CanvasWidget

        :param attribs: The new canvas widget's attributes.

        """
        self._child = child
        self._add_child_widget(child)
        CanvasWidget.__init__(self, canvas, **attribs)

    def _manage(self):
        self._update(self._child)

    def child(self):
        """

        :return: The child widget contained by this container widget.

        :rtype: CanvasWidget

        """
        return self._child

    def set_child(self, child):
        """

        Change the child widget contained by this container widget.



        :param child: The new child widget.  ``child`` must not have a

            parent.

        :type child: CanvasWidget

        :rtype: None

        """
        self._remove_child_widget(self._child)
        self._add_child_widget(child)
        self._child = child
        self.update(child)

    def __repr__(self):
        name = self.__class__.__name__
        if name[-6:] == "Widget":
            name = name[:-6]
        return f"[{name}: {self._child!r}]"


class BoxWidget(AbstractContainerWidget):
    """

    A canvas widget that places a box around a child widget.



    Attributes:

      - ``fill``: The color used to fill the interior of the box.

      - ``outline``: The color used to draw the outline of the box.

      - ``width``: The width of the outline of the box.

      - ``margin``: The number of pixels space left between the child

        and the box.

      - ``draggable``: whether the text can be dragged by the user.

    """

    def __init__(self, canvas, child, **attribs):
        """

        Create a new box widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :param child: The child widget.  ``child`` must not have a

            parent.

        :type child: CanvasWidget

        :param attribs: The new canvas widget's attributes.

        """
        self._child = child
        self._margin = 1
        self._box = canvas.create_rectangle(1, 1, 1, 1)
        canvas.tag_lower(self._box)
        AbstractContainerWidget.__init__(self, canvas, child, **attribs)

    def __setitem__(self, attr, value):
        if attr == "margin":
            self._margin = value
        elif attr in ("outline", "fill", "width"):
            self.canvas().itemconfig(self._box, {attr: value})
        else:
            CanvasWidget.__setitem__(self, attr, value)

    def __getitem__(self, attr):
        if attr == "margin":
            return self._margin
        elif attr == "width":
            return float(self.canvas().itemcget(self._box, attr))
        elif attr in ("outline", "fill", "width"):
            return self.canvas().itemcget(self._box, attr)
        else:
            return CanvasWidget.__getitem__(self, attr)

    def _update(self, child):
        (x1, y1, x2, y2) = child.bbox()
        margin = self._margin + self["width"] / 2
        self.canvas().coords(
            self._box, x1 - margin, y1 - margin, x2 + margin, y2 + margin
        )

    def _tags(self):
        return [self._box]


class OvalWidget(AbstractContainerWidget):
    """

    A canvas widget that places a oval around a child widget.



    Attributes:

      - ``fill``: The color used to fill the interior of the oval.

      - ``outline``: The color used to draw the outline of the oval.

      - ``width``: The width of the outline of the oval.

      - ``margin``: The number of pixels space left between the child

        and the oval.

      - ``draggable``: whether the text can be dragged by the user.

      - ``double``: If true, then a double-oval is drawn.

    """

    def __init__(self, canvas, child, **attribs):
        """

        Create a new oval widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :param child: The child widget.  ``child`` must not have a

            parent.

        :type child: CanvasWidget

        :param attribs: The new canvas widget's attributes.

        """
        self._child = child
        self._margin = 1
        self._oval = canvas.create_oval(1, 1, 1, 1)
        self._circle = attribs.pop("circle", False)
        self._double = attribs.pop("double", False)
        if self._double:
            self._oval2 = canvas.create_oval(1, 1, 1, 1)
        else:
            self._oval2 = None
        canvas.tag_lower(self._oval)
        AbstractContainerWidget.__init__(self, canvas, child, **attribs)

    def __setitem__(self, attr, value):
        c = self.canvas()
        if attr == "margin":
            self._margin = value
        elif attr == "double":
            if value == True and self._oval2 is None:
                # Copy attributes & position from self._oval.
                x1, y1, x2, y2 = c.bbox(self._oval)
                w = self["width"] * 2
                self._oval2 = c.create_oval(
                    x1 - w,
                    y1 - w,
                    x2 + w,
                    y2 + w,
                    outline=c.itemcget(self._oval, "outline"),
                    width=c.itemcget(self._oval, "width"),
                )
                c.tag_lower(self._oval2)
            if value == False and self._oval2 is not None:
                c.delete(self._oval2)
                self._oval2 = None
        elif attr in ("outline", "fill", "width"):
            c.itemconfig(self._oval, {attr: value})
            if self._oval2 is not None and attr != "fill":
                c.itemconfig(self._oval2, {attr: value})
            if self._oval2 is not None and attr != "fill":
                self.canvas().itemconfig(self._oval2, {attr: value})
        else:
            CanvasWidget.__setitem__(self, attr, value)

    def __getitem__(self, attr):
        if attr == "margin":
            return self._margin
        elif attr == "double":
            return self._double is not None
        elif attr == "width":
            return float(self.canvas().itemcget(self._oval, attr))
        elif attr in ("outline", "fill", "width"):
            return self.canvas().itemcget(self._oval, attr)
        else:
            return CanvasWidget.__getitem__(self, attr)

    # The ratio between inscribed & circumscribed ovals
    RATIO = 1.4142135623730949

    def _update(self, child):
        R = OvalWidget.RATIO
        (x1, y1, x2, y2) = child.bbox()
        margin = self._margin

        # If we're a circle, pretend our contents are square.
        if self._circle:
            dx, dy = abs(x1 - x2), abs(y1 - y2)
            if dx > dy:
                y = (y1 + y2) / 2
                y1, y2 = y - dx / 2, y + dx / 2
            elif dy > dx:
                x = (x1 + x2) / 2
                x1, x2 = x - dy / 2, x + dy / 2

        # Find the four corners.
        left = int((x1 * (1 + R) + x2 * (1 - R)) / 2)
        right = left + int((x2 - x1) * R)
        top = int((y1 * (1 + R) + y2 * (1 - R)) / 2)
        bot = top + int((y2 - y1) * R)
        self.canvas().coords(
            self._oval, left - margin, top - margin, right + margin, bot + margin
        )
        if self._oval2 is not None:
            self.canvas().coords(
                self._oval2,
                left - margin + 2,
                top - margin + 2,
                right + margin - 2,
                bot + margin - 2,
            )

    def _tags(self):
        if self._oval2 is None:
            return [self._oval]
        else:
            return [self._oval, self._oval2]


class ParenWidget(AbstractContainerWidget):
    """

    A canvas widget that places a pair of parenthases around a child

    widget.



    Attributes:

      - ``color``: The color used to draw the parenthases.

      - ``width``: The width of the parenthases.

      - ``draggable``: whether the text can be dragged by the user.

    """

    def __init__(self, canvas, child, **attribs):
        """

        Create a new parenthasis widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :param child: The child widget.  ``child`` must not have a

            parent.

        :type child: CanvasWidget

        :param attribs: The new canvas widget's attributes.

        """
        self._child = child
        self._oparen = canvas.create_arc(1, 1, 1, 1, style="arc", start=90, extent=180)
        self._cparen = canvas.create_arc(1, 1, 1, 1, style="arc", start=-90, extent=180)
        AbstractContainerWidget.__init__(self, canvas, child, **attribs)

    def __setitem__(self, attr, value):
        if attr == "color":
            self.canvas().itemconfig(self._oparen, outline=value)
            self.canvas().itemconfig(self._cparen, outline=value)
        elif attr == "width":
            self.canvas().itemconfig(self._oparen, width=value)
            self.canvas().itemconfig(self._cparen, width=value)
        else:
            CanvasWidget.__setitem__(self, attr, value)

    def __getitem__(self, attr):
        if attr == "color":
            return self.canvas().itemcget(self._oparen, "outline")
        elif attr == "width":
            return self.canvas().itemcget(self._oparen, "width")
        else:
            return CanvasWidget.__getitem__(self, attr)

    def _update(self, child):
        (x1, y1, x2, y2) = child.bbox()
        width = max((y2 - y1) / 6, 4)
        self.canvas().coords(self._oparen, x1 - width, y1, x1 + width, y2)
        self.canvas().coords(self._cparen, x2 - width, y1, x2 + width, y2)

    def _tags(self):
        return [self._oparen, self._cparen]


class BracketWidget(AbstractContainerWidget):
    """

    A canvas widget that places a pair of brackets around a child

    widget.



    Attributes:

      - ``color``: The color used to draw the brackets.

      - ``width``: The width of the brackets.

      - ``draggable``: whether the text can be dragged by the user.

    """

    def __init__(self, canvas, child, **attribs):
        """

        Create a new bracket widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :param child: The child widget.  ``child`` must not have a

            parent.

        :type child: CanvasWidget

        :param attribs: The new canvas widget's attributes.

        """
        self._child = child
        self._obrack = canvas.create_line(1, 1, 1, 1, 1, 1, 1, 1)
        self._cbrack = canvas.create_line(1, 1, 1, 1, 1, 1, 1, 1)
        AbstractContainerWidget.__init__(self, canvas, child, **attribs)

    def __setitem__(self, attr, value):
        if attr == "color":
            self.canvas().itemconfig(self._obrack, fill=value)
            self.canvas().itemconfig(self._cbrack, fill=value)
        elif attr == "width":
            self.canvas().itemconfig(self._obrack, width=value)
            self.canvas().itemconfig(self._cbrack, width=value)
        else:
            CanvasWidget.__setitem__(self, attr, value)

    def __getitem__(self, attr):
        if attr == "color":
            return self.canvas().itemcget(self._obrack, "outline")
        elif attr == "width":
            return self.canvas().itemcget(self._obrack, "width")
        else:
            return CanvasWidget.__getitem__(self, attr)

    def _update(self, child):
        (x1, y1, x2, y2) = child.bbox()
        width = max((y2 - y1) / 8, 2)
        self.canvas().coords(
            self._obrack, x1, y1, x1 - width, y1, x1 - width, y2, x1, y2
        )
        self.canvas().coords(
            self._cbrack, x2, y1, x2 + width, y1, x2 + width, y2, x2, y2
        )

    def _tags(self):
        return [self._obrack, self._cbrack]


class SequenceWidget(CanvasWidget):
    """

    A canvas widget that keeps a list of canvas widgets in a

    horizontal line.



    Attributes:

      - ``align``: The vertical alignment of the children.  Possible

        values are ``'top'``, ``'center'``, and ``'bottom'``.  By

        default, children are center-aligned.

      - ``space``: The amount of horizontal space to place between

        children.  By default, one pixel of space is used.

      - ``ordered``: If true, then keep the children in their

        original order.

    """

    def __init__(self, canvas, *children, **attribs):
        """

        Create a new sequence widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :param children: The widgets that should be aligned

            horizontally.  Each child must not have a parent.

        :type children: list(CanvasWidget)

        :param attribs: The new canvas widget's attributes.

        """
        self._align = "center"
        self._space = 1
        self._ordered = False
        self._children = list(children)
        for child in children:
            self._add_child_widget(child)
        CanvasWidget.__init__(self, canvas, **attribs)

    def __setitem__(self, attr, value):
        if attr == "align":
            if value not in ("top", "bottom", "center"):
                raise ValueError("Bad alignment: %r" % value)
            self._align = value
        elif attr == "space":
            self._space = value
        elif attr == "ordered":
            self._ordered = value
        else:
            CanvasWidget.__setitem__(self, attr, value)

    def __getitem__(self, attr):
        if attr == "align":
            return self._align
        elif attr == "space":
            return self._space
        elif attr == "ordered":
            return self._ordered
        else:
            return CanvasWidget.__getitem__(self, attr)

    def _tags(self):
        return []

    def _yalign(self, top, bot):
        if self._align == "top":
            return top
        if self._align == "bottom":
            return bot
        if self._align == "center":
            return (top + bot) / 2

    def _update(self, child):
        # Align all children with child.
        (left, top, right, bot) = child.bbox()
        y = self._yalign(top, bot)
        for c in self._children:
            (x1, y1, x2, y2) = c.bbox()
            c.move(0, y - self._yalign(y1, y2))

        if self._ordered and len(self._children) > 1:
            index = self._children.index(child)

            x = right + self._space
            for i in range(index + 1, len(self._children)):
                (x1, y1, x2, y2) = self._children[i].bbox()
                if x > x1:
                    self._children[i].move(x - x1, 0)
                    x += x2 - x1 + self._space

            x = left - self._space
            for i in range(index - 1, -1, -1):
                (x1, y1, x2, y2) = self._children[i].bbox()
                if x < x2:
                    self._children[i].move(x - x2, 0)
                    x -= x2 - x1 + self._space

    def _manage(self):
        if len(self._children) == 0:
            return
        child = self._children[0]

        # Align all children with child.
        (left, top, right, bot) = child.bbox()
        y = self._yalign(top, bot)

        index = self._children.index(child)

        # Line up children to the right of child.
        x = right + self._space
        for i in range(index + 1, len(self._children)):
            (x1, y1, x2, y2) = self._children[i].bbox()
            self._children[i].move(x - x1, y - self._yalign(y1, y2))
            x += x2 - x1 + self._space

        # Line up children to the left of child.
        x = left - self._space
        for i in range(index - 1, -1, -1):
            (x1, y1, x2, y2) = self._children[i].bbox()
            self._children[i].move(x - x2, y - self._yalign(y1, y2))
            x -= x2 - x1 + self._space

    def __repr__(self):
        return "[Sequence: " + repr(self._children)[1:-1] + "]"

    # Provide an alias for the child_widgets() member.
    children = CanvasWidget.child_widgets

    def replace_child(self, oldchild, newchild):
        """

        Replace the child canvas widget ``oldchild`` with ``newchild``.

        ``newchild`` must not have a parent.  ``oldchild``'s parent will

        be set to None.



        :type oldchild: CanvasWidget

        :param oldchild: The child canvas widget to remove.

        :type newchild: CanvasWidget

        :param newchild: The canvas widget that should replace

            ``oldchild``.

        """
        index = self._children.index(oldchild)
        self._children[index] = newchild
        self._remove_child_widget(oldchild)
        self._add_child_widget(newchild)
        self.update(newchild)

    def remove_child(self, child):
        """

        Remove the given child canvas widget.  ``child``'s parent will

        be set to None.



        :type child: CanvasWidget

        :param child: The child canvas widget to remove.

        """
        index = self._children.index(child)
        del self._children[index]
        self._remove_child_widget(child)
        if len(self._children) > 0:
            self.update(self._children[0])

    def insert_child(self, index, child):
        """

        Insert a child canvas widget before a given index.



        :type child: CanvasWidget

        :param child: The canvas widget that should be inserted.

        :type index: int

        :param index: The index where the child widget should be

            inserted.  In particular, the index of ``child`` will be

            ``index``; and the index of any children whose indices were

            greater than equal to ``index`` before ``child`` was

            inserted will be incremented by one.

        """
        self._children.insert(index, child)
        self._add_child_widget(child)


class StackWidget(CanvasWidget):
    """

    A canvas widget that keeps a list of canvas widgets in a vertical

    line.



    Attributes:

      - ``align``: The horizontal alignment of the children.  Possible

        values are ``'left'``, ``'center'``, and ``'right'``.  By

        default, children are center-aligned.

      - ``space``: The amount of vertical space to place between

        children.  By default, one pixel of space is used.

      - ``ordered``: If true, then keep the children in their

        original order.

    """

    def __init__(self, canvas, *children, **attribs):
        """

        Create a new stack widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :param children: The widgets that should be aligned

            vertically.  Each child must not have a parent.

        :type children: list(CanvasWidget)

        :param attribs: The new canvas widget's attributes.

        """
        self._align = "center"
        self._space = 1
        self._ordered = False
        self._children = list(children)
        for child in children:
            self._add_child_widget(child)
        CanvasWidget.__init__(self, canvas, **attribs)

    def __setitem__(self, attr, value):
        if attr == "align":
            if value not in ("left", "right", "center"):
                raise ValueError("Bad alignment: %r" % value)
            self._align = value
        elif attr == "space":
            self._space = value
        elif attr == "ordered":
            self._ordered = value
        else:
            CanvasWidget.__setitem__(self, attr, value)

    def __getitem__(self, attr):
        if attr == "align":
            return self._align
        elif attr == "space":
            return self._space
        elif attr == "ordered":
            return self._ordered
        else:
            return CanvasWidget.__getitem__(self, attr)

    def _tags(self):
        return []

    def _xalign(self, left, right):
        if self._align == "left":
            return left
        if self._align == "right":
            return right
        if self._align == "center":
            return (left + right) / 2

    def _update(self, child):
        # Align all children with child.
        (left, top, right, bot) = child.bbox()
        x = self._xalign(left, right)
        for c in self._children:
            (x1, y1, x2, y2) = c.bbox()
            c.move(x - self._xalign(x1, x2), 0)

        if self._ordered and len(self._children) > 1:
            index = self._children.index(child)

            y = bot + self._space
            for i in range(index + 1, len(self._children)):
                (x1, y1, x2, y2) = self._children[i].bbox()
                if y > y1:
                    self._children[i].move(0, y - y1)
                    y += y2 - y1 + self._space

            y = top - self._space
            for i in range(index - 1, -1, -1):
                (x1, y1, x2, y2) = self._children[i].bbox()
                if y < y2:
                    self._children[i].move(0, y - y2)
                    y -= y2 - y1 + self._space

    def _manage(self):
        if len(self._children) == 0:
            return
        child = self._children[0]

        # Align all children with child.
        (left, top, right, bot) = child.bbox()
        x = self._xalign(left, right)

        index = self._children.index(child)

        # Line up children below the child.
        y = bot + self._space
        for i in range(index + 1, len(self._children)):
            (x1, y1, x2, y2) = self._children[i].bbox()
            self._children[i].move(x - self._xalign(x1, x2), y - y1)
            y += y2 - y1 + self._space

        # Line up children above the child.
        y = top - self._space
        for i in range(index - 1, -1, -1):
            (x1, y1, x2, y2) = self._children[i].bbox()
            self._children[i].move(x - self._xalign(x1, x2), y - y2)
            y -= y2 - y1 + self._space

    def __repr__(self):
        return "[Stack: " + repr(self._children)[1:-1] + "]"

    # Provide an alias for the child_widgets() member.
    children = CanvasWidget.child_widgets

    def replace_child(self, oldchild, newchild):
        """

        Replace the child canvas widget ``oldchild`` with ``newchild``.

        ``newchild`` must not have a parent.  ``oldchild``'s parent will

        be set to None.



        :type oldchild: CanvasWidget

        :param oldchild: The child canvas widget to remove.

        :type newchild: CanvasWidget

        :param newchild: The canvas widget that should replace

            ``oldchild``.

        """
        index = self._children.index(oldchild)
        self._children[index] = newchild
        self._remove_child_widget(oldchild)
        self._add_child_widget(newchild)
        self.update(newchild)

    def remove_child(self, child):
        """

        Remove the given child canvas widget.  ``child``'s parent will

        be set to None.



        :type child: CanvasWidget

        :param child: The child canvas widget to remove.

        """
        index = self._children.index(child)
        del self._children[index]
        self._remove_child_widget(child)
        if len(self._children) > 0:
            self.update(self._children[0])

    def insert_child(self, index, child):
        """

        Insert a child canvas widget before a given index.



        :type child: CanvasWidget

        :param child: The canvas widget that should be inserted.

        :type index: int

        :param index: The index where the child widget should be

            inserted.  In particular, the index of ``child`` will be

            ``index``; and the index of any children whose indices were

            greater than equal to ``index`` before ``child`` was

            inserted will be incremented by one.

        """
        self._children.insert(index, child)
        self._add_child_widget(child)


class SpaceWidget(CanvasWidget):
    """

    A canvas widget that takes up space but does not display

    anything.  A ``SpaceWidget`` can be used to add space between

    elements.  Each space widget is characterized by a width and a

    height.  If you wish to only create horizontal space, then use a

    height of zero; and if you wish to only create vertical space, use

    a width of zero.

    """

    def __init__(self, canvas, width, height, **attribs):
        """

        Create a new space widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :type width: int

        :param width: The width of the new space widget.

        :type height: int

        :param height: The height of the new space widget.

        :param attribs: The new canvas widget's attributes.

        """
        # For some reason,
        if width > 4:
            width -= 4
        if height > 4:
            height -= 4
        self._tag = canvas.create_line(1, 1, width, height, fill="")
        CanvasWidget.__init__(self, canvas, **attribs)

    # note: width() and height() are already defined by CanvasWidget.
    def set_width(self, width):
        """

        Change the width of this space widget.



        :param width: The new width.

        :type width: int

        :rtype: None

        """
        [x1, y1, x2, y2] = self.bbox()
        self.canvas().coords(self._tag, x1, y1, x1 + width, y2)

    def set_height(self, height):
        """

        Change the height of this space widget.



        :param height: The new height.

        :type height: int

        :rtype: None

        """
        [x1, y1, x2, y2] = self.bbox()
        self.canvas().coords(self._tag, x1, y1, x2, y1 + height)

    def _tags(self):
        return [self._tag]

    def __repr__(self):
        return "[Space]"


class ScrollWatcherWidget(CanvasWidget):
    """

    A special canvas widget that adjusts its ``Canvas``'s scrollregion

    to always include the bounding boxes of all of its children.  The

    scroll-watcher widget will only increase the size of the

    ``Canvas``'s scrollregion; it will never decrease it.

    """

    def __init__(self, canvas, *children, **attribs):
        """

        Create a new scroll-watcher widget.



        :type canvas: Tkinter.Canvas

        :param canvas: This canvas widget's canvas.

        :type children: list(CanvasWidget)

        :param children: The canvas widgets watched by the

            scroll-watcher.  The scroll-watcher will ensure that these

            canvas widgets are always contained in their canvas's

            scrollregion.

        :param attribs: The new canvas widget's attributes.

        """
        for child in children:
            self._add_child_widget(child)
        CanvasWidget.__init__(self, canvas, **attribs)

    def add_child(self, canvaswidget):
        """

        Add a new canvas widget to the scroll-watcher.  The

        scroll-watcher will ensure that the new canvas widget is

        always contained in its canvas's scrollregion.



        :param canvaswidget: The new canvas widget.

        :type canvaswidget: CanvasWidget

        :rtype: None

        """
        self._add_child_widget(canvaswidget)
        self.update(canvaswidget)

    def remove_child(self, canvaswidget):
        """

        Remove a canvas widget from the scroll-watcher.  The

        scroll-watcher will no longer ensure that the new canvas

        widget is always contained in its canvas's scrollregion.



        :param canvaswidget: The canvas widget to remove.

        :type canvaswidget: CanvasWidget

        :rtype: None

        """
        self._remove_child_widget(canvaswidget)

    def _tags(self):
        return []

    def _update(self, child):
        self._adjust_scrollregion()

    def _adjust_scrollregion(self):
        """

        Adjust the scrollregion of this scroll-watcher's ``Canvas`` to

        include the bounding boxes of all of its children.

        """
        bbox = self.bbox()
        canvas = self.canvas()
        scrollregion = [int(n) for n in canvas["scrollregion"].split()]
        if len(scrollregion) != 4:
            return
        if (
            bbox[0] < scrollregion[0]
            or bbox[1] < scrollregion[1]
            or bbox[2] > scrollregion[2]
            or bbox[3] > scrollregion[3]
        ):
            scrollregion = "%d %d %d %d" % (
                min(bbox[0], scrollregion[0]),
                min(bbox[1], scrollregion[1]),
                max(bbox[2], scrollregion[2]),
                max(bbox[3], scrollregion[3]),
            )
            canvas["scrollregion"] = scrollregion


##//////////////////////////////////////////////////////
##  Canvas Frame
##//////////////////////////////////////////////////////


class CanvasFrame:
    """

    A ``Tkinter`` frame containing a canvas and scrollbars.

    ``CanvasFrame`` uses a ``ScrollWatcherWidget`` to ensure that all of

    the canvas widgets contained on its canvas are within its

    scrollregion.  In order for ``CanvasFrame`` to make these checks,

    all canvas widgets must be registered with ``add_widget`` when they

    are added to the canvas; and destroyed with ``destroy_widget`` when

    they are no longer needed.



    If a ``CanvasFrame`` is created with no parent, then it will create

    its own main window, including a "Done" button and a "Print"

    button.

    """

    def __init__(self, parent=None, **kw):
        """

        Create a new ``CanvasFrame``.



        :type parent: Tkinter.BaseWidget or Tkinter.Tk

        :param parent: The parent ``Tkinter`` widget.  If no parent is

            specified, then ``CanvasFrame`` will create a new main

            window.

        :param kw: Keyword arguments for the new ``Canvas``.  See the

            documentation for ``Tkinter.Canvas`` for more information.

        """
        # If no parent was given, set up a top-level window.
        if parent is None:
            self._parent = Tk()
            self._parent.title("NLTK")
            self._parent.bind("<Control-p>", lambda e: self.print_to_file())
            self._parent.bind("<Control-x>", self.destroy)
            self._parent.bind("<Control-q>", self.destroy)
        else:
            self._parent = parent

        # Create a frame for the canvas & scrollbars
        self._frame = frame = Frame(self._parent)
        self._canvas = canvas = Canvas(frame, **kw)
        xscrollbar = Scrollbar(self._frame, orient="horizontal")
        yscrollbar = Scrollbar(self._frame, orient="vertical")
        xscrollbar["command"] = canvas.xview
        yscrollbar["command"] = canvas.yview
        canvas["xscrollcommand"] = xscrollbar.set
        canvas["yscrollcommand"] = yscrollbar.set
        yscrollbar.pack(fill="y", side="right")
        xscrollbar.pack(fill="x", side="bottom")
        canvas.pack(expand=1, fill="both", side="left")

        # Set initial scroll region.
        scrollregion = "0 0 {} {}".format(canvas["width"], canvas["height"])
        canvas["scrollregion"] = scrollregion

        self._scrollwatcher = ScrollWatcherWidget(canvas)

        # If no parent was given, pack the frame, and add a menu.
        if parent is None:
            self.pack(expand=1, fill="both")
            self._init_menubar()

    def _init_menubar(self):
        menubar = Menu(self._parent)

        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(
            label="Print to Postscript",
            underline=0,
            command=self.print_to_file,
            accelerator="Ctrl-p",
        )
        filemenu.add_command(
            label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
        )
        menubar.add_cascade(label="File", underline=0, menu=filemenu)

        self._parent.config(menu=menubar)

    def print_to_file(self, filename=None):
        """

        Print the contents of this ``CanvasFrame`` to a postscript

        file.  If no filename is given, then prompt the user for one.



        :param filename: The name of the file to print the tree to.

        :type filename: str

        :rtype: None

        """
        if filename is None:
            ftypes = [("Postscript files", ".ps"), ("All files", "*")]
            filename = asksaveasfilename(filetypes=ftypes, defaultextension=".ps")
            if not filename:
                return
        (x0, y0, w, h) = self.scrollregion()
        postscript = self._canvas.postscript(
            x=x0,
            y=y0,
            width=w + 2,
            height=h + 2,
            pagewidth=w + 2,  # points = 1/72 inch
            pageheight=h + 2,  # points = 1/72 inch
            pagex=0,
            pagey=0,
        )
        # workaround for bug in Tk font handling
        postscript = postscript.replace(" 0 scalefont ", " 9 scalefont ")
        with open(filename, "wb") as f:
            f.write(postscript.encode("utf8"))

    def scrollregion(self):
        """

        :return: The current scroll region for the canvas managed by

            this ``CanvasFrame``.

        :rtype: 4-tuple of int

        """
        (x1, y1, x2, y2) = self._canvas["scrollregion"].split()
        return (int(x1), int(y1), int(x2), int(y2))

    def canvas(self):
        """

        :return: The canvas managed by this ``CanvasFrame``.

        :rtype: Tkinter.Canvas

        """
        return self._canvas

    def add_widget(self, canvaswidget, x=None, y=None):
        """

        Register a canvas widget with this ``CanvasFrame``.  The

        ``CanvasFrame`` will ensure that this canvas widget is always

        within the ``Canvas``'s scrollregion.  If no coordinates are

        given for the canvas widget, then the ``CanvasFrame`` will

        attempt to find a clear area of the canvas for it.



        :type canvaswidget: CanvasWidget

        :param canvaswidget: The new canvas widget.  ``canvaswidget``

            must have been created on this ``CanvasFrame``'s canvas.

        :type x: int

        :param x: The initial x coordinate for the upper left hand

            corner of ``canvaswidget``, in the canvas's coordinate

            space.

        :type y: int

        :param y: The initial y coordinate for the upper left hand

            corner of ``canvaswidget``, in the canvas's coordinate

            space.

        """
        if x is None or y is None:
            (x, y) = self._find_room(canvaswidget, x, y)

        # Move to (x,y)
        (x1, y1, x2, y2) = canvaswidget.bbox()
        canvaswidget.move(x - x1, y - y1)

        # Register with scrollwatcher.
        self._scrollwatcher.add_child(canvaswidget)

    def _find_room(self, widget, desired_x, desired_y):
        """

        Try to find a space for a given widget.

        """
        (left, top, right, bot) = self.scrollregion()
        w = widget.width()
        h = widget.height()

        if w >= (right - left):
            return (0, 0)
        if h >= (bot - top):
            return (0, 0)

        # Move the widget out of the way, for now.
        (x1, y1, x2, y2) = widget.bbox()
        widget.move(left - x2 - 50, top - y2 - 50)

        if desired_x is not None:
            x = desired_x
            for y in range(top, bot - h, int((bot - top - h) / 10)):
                if not self._canvas.find_overlapping(
                    x - 5, y - 5, x + w + 5, y + h + 5
                ):
                    return (x, y)

        if desired_y is not None:
            y = desired_y
            for x in range(left, right - w, int((right - left - w) / 10)):
                if not self._canvas.find_overlapping(
                    x - 5, y - 5, x + w + 5, y + h + 5
                ):
                    return (x, y)

        for y in range(top, bot - h, int((bot - top - h) / 10)):
            for x in range(left, right - w, int((right - left - w) / 10)):
                if not self._canvas.find_overlapping(
                    x - 5, y - 5, x + w + 5, y + h + 5
                ):
                    return (x, y)
        return (0, 0)

    def destroy_widget(self, canvaswidget):
        """

        Remove a canvas widget from this ``CanvasFrame``.  This

        deregisters the canvas widget, and destroys it.

        """
        self.remove_widget(canvaswidget)
        canvaswidget.destroy()

    def remove_widget(self, canvaswidget):
        # Deregister with scrollwatcher.
        self._scrollwatcher.remove_child(canvaswidget)

    def pack(self, cnf={}, **kw):
        """

        Pack this ``CanvasFrame``.  See the documentation for

        ``Tkinter.Pack`` for more information.

        """
        self._frame.pack(cnf, **kw)
        # Adjust to be big enough for kids?

    def destroy(self, *e):
        """

        Destroy this ``CanvasFrame``.  If this ``CanvasFrame`` created a

        top-level window, then this will close that window.

        """
        if self._parent is None:
            return
        self._parent.destroy()
        self._parent = None

    def mainloop(self, *args, **kwargs):
        """

        Enter the Tkinter mainloop.  This function must be called if

        this frame is created from a non-interactive program (e.g.

        from a secript); otherwise, the frame will close as soon as

        the script completes.

        """
        if in_idle():
            return
        self._parent.mainloop(*args, **kwargs)


##//////////////////////////////////////////////////////
##  Text display
##//////////////////////////////////////////////////////


class ShowText:
    """

    A ``Tkinter`` window used to display a text.  ``ShowText`` is

    typically used by graphical tools to display help text, or similar

    information.

    """

    def __init__(self, root, title, text, width=None, height=None, **textbox_options):
        if width is None or height is None:
            (width, height) = self.find_dimentions(text, width, height)

        # Create the main window.
        if root is None:
            self._top = top = Tk()
        else:
            self._top = top = Toplevel(root)
        top.title(title)

        b = Button(top, text="Ok", command=self.destroy)
        b.pack(side="bottom")

        tbf = Frame(top)
        tbf.pack(expand=1, fill="both")
        scrollbar = Scrollbar(tbf, orient="vertical")
        scrollbar.pack(side="right", fill="y")
        textbox = Text(tbf, wrap="word", width=width, height=height, **textbox_options)
        textbox.insert("end", text)
        textbox["state"] = "disabled"
        textbox.pack(side="left", expand=1, fill="both")
        scrollbar["command"] = textbox.yview
        textbox["yscrollcommand"] = scrollbar.set

        # Make it easy to close the window.
        top.bind("q", self.destroy)
        top.bind("x", self.destroy)
        top.bind("c", self.destroy)
        top.bind("<Return>", self.destroy)
        top.bind("<Escape>", self.destroy)

        # Focus the scrollbar, so they can use up/down, etc.
        scrollbar.focus()

    def find_dimentions(self, text, width, height):
        lines = text.split("\n")
        if width is None:
            maxwidth = max(len(line) for line in lines)
            width = min(maxwidth, 80)

        # Now, find height.
        height = 0
        for line in lines:
            while len(line) > width:
                brk = line[:width].rfind(" ")
                line = line[brk:]
                height += 1
            height += 1
        height = min(height, 25)

        return (width, height)

    def destroy(self, *e):
        if self._top is None:
            return
        self._top.destroy()
        self._top = None

    def mainloop(self, *args, **kwargs):
        """

        Enter the Tkinter mainloop.  This function must be called if

        this window is created from a non-interactive program (e.g.

        from a secript); otherwise, the window will close as soon as

        the script completes.

        """
        if in_idle():
            return
        self._top.mainloop(*args, **kwargs)


##//////////////////////////////////////////////////////
##  Entry dialog
##//////////////////////////////////////////////////////


class EntryDialog:
    """

    A dialog box for entering

    """

    def __init__(

        self, parent, original_text="", instructions="", set_callback=None, title=None

    ):
        self._parent = parent
        self._original_text = original_text
        self._set_callback = set_callback

        width = int(max(30, len(original_text) * 3 / 2))
        self._top = Toplevel(parent)

        if title:
            self._top.title(title)

        # The text entry box.
        entryframe = Frame(self._top)
        entryframe.pack(expand=1, fill="both", padx=5, pady=5, ipady=10)
        if instructions:
            l = Label(entryframe, text=instructions)
            l.pack(side="top", anchor="w", padx=30)
        self._entry = Entry(entryframe, width=width)
        self._entry.pack(expand=1, fill="x", padx=30)
        self._entry.insert(0, original_text)

        # A divider
        divider = Frame(self._top, borderwidth=1, relief="sunken")
        divider.pack(fill="x", ipady=1, padx=10)

        # The buttons.
        buttons = Frame(self._top)
        buttons.pack(expand=0, fill="x", padx=5, pady=5)
        b = Button(buttons, text="Cancel", command=self._cancel, width=8)
        b.pack(side="right", padx=5)
        b = Button(buttons, text="Ok", command=self._ok, width=8, default="active")
        b.pack(side="left", padx=5)
        b = Button(buttons, text="Apply", command=self._apply, width=8)
        b.pack(side="left")

        self._top.bind("<Return>", self._ok)
        self._top.bind("<Control-q>", self._cancel)
        self._top.bind("<Escape>", self._cancel)

        self._entry.focus()

    def _reset(self, *e):
        self._entry.delete(0, "end")
        self._entry.insert(0, self._original_text)
        if self._set_callback:
            self._set_callback(self._original_text)

    def _cancel(self, *e):
        try:
            self._reset()
        except:
            pass
        self._destroy()

    def _ok(self, *e):
        self._apply()
        self._destroy()

    def _apply(self, *e):
        if self._set_callback:
            self._set_callback(self._entry.get())

    def _destroy(self, *e):
        if self._top is None:
            return
        self._top.destroy()
        self._top = None


##//////////////////////////////////////////////////////
##  Colorized List
##//////////////////////////////////////////////////////


class ColorizedList:
    """

    An abstract base class for displaying a colorized list of items.

    Subclasses should define:



    - ``_init_colortags``, which sets up Text color tags that

      will be used by the list.

    - ``_item_repr``, which returns a list of (text,colortag)

      tuples that make up the colorized representation of the

      item.



    :note: Typically, you will want to register a callback for

        ``'select'`` that calls ``mark`` on the given item.

    """

    def __init__(self, parent, items=[], **options):
        """

        Construct a new list.



        :param parent: The Tk widget that contains the colorized list

        :param items: The initial contents of the colorized list.

        :param options:

        """
        self._parent = parent
        self._callbacks = {}

        # Which items are marked?
        self._marks = {}

        # Initialize the Tkinter frames.
        self._init_itemframe(options.copy())

        # Set up key & mouse bindings.
        self._textwidget.bind("<KeyPress>", self._keypress)
        self._textwidget.bind("<ButtonPress>", self._buttonpress)

        # Fill in the given CFG's items.
        self._items = None
        self.set(items)

    # ////////////////////////////////////////////////////////////
    # Abstract methods
    # ////////////////////////////////////////////////////////////
    @abstractmethod
    def _init_colortags(self, textwidget, options):
        """

        Set up any colortags that will be used by this colorized list.

        E.g.:

            textwidget.tag_config('terminal', foreground='black')

        """

    @abstractmethod
    def _item_repr(self, item):
        """

        Return a list of (text, colortag) tuples that make up the

        colorized representation of the item.  Colorized

        representations may not span multiple lines.  I.e., the text

        strings returned may not contain newline characters.

        """

    # ////////////////////////////////////////////////////////////
    # Item Access
    # ////////////////////////////////////////////////////////////

    def get(self, index=None):
        """

        :return: A list of the items contained by this list.

        """
        if index is None:
            return self._items[:]
        else:
            return self._items[index]

    def set(self, items):
        """

        Modify the list of items contained by this list.

        """
        items = list(items)
        if self._items == items:
            return
        self._items = list(items)

        self._textwidget["state"] = "normal"
        self._textwidget.delete("1.0", "end")
        for item in items:
            for (text, colortag) in self._item_repr(item):
                assert "\n" not in text, "item repr may not contain newline"
                self._textwidget.insert("end", text, colortag)
            self._textwidget.insert("end", "\n")
        # Remove the final newline
        self._textwidget.delete("end-1char", "end")
        self._textwidget.mark_set("insert", "1.0")
        self._textwidget["state"] = "disabled"
        # Clear all marks
        self._marks.clear()

    def unmark(self, item=None):
        """

        Remove highlighting from the given item; or from every item,

        if no item is given.

        :raise ValueError: If ``item`` is not contained in the list.

        :raise KeyError: If ``item`` is not marked.

        """
        if item is None:
            self._marks.clear()
            self._textwidget.tag_remove("highlight", "1.0", "end+1char")
        else:
            index = self._items.index(item)
            del self._marks[item]
            (start, end) = ("%d.0" % (index + 1), "%d.0" % (index + 2))
            self._textwidget.tag_remove("highlight", start, end)

    def mark(self, item):
        """

        Highlight the given item.

        :raise ValueError: If ``item`` is not contained in the list.

        """
        self._marks[item] = 1
        index = self._items.index(item)
        (start, end) = ("%d.0" % (index + 1), "%d.0" % (index + 2))
        self._textwidget.tag_add("highlight", start, end)

    def markonly(self, item):
        """

        Remove any current highlighting, and mark the given item.

        :raise ValueError: If ``item`` is not contained in the list.

        """
        self.unmark()
        self.mark(item)

    def view(self, item):
        """

        Adjust the view such that the given item is visible.  If

        the item is already visible, then do nothing.

        """
        index = self._items.index(item)
        self._textwidget.see("%d.0" % (index + 1))

    # ////////////////////////////////////////////////////////////
    # Callbacks
    # ////////////////////////////////////////////////////////////

    def add_callback(self, event, func):
        """

        Register a callback function with the list.  This function

        will be called whenever the given event occurs.



        :param event: The event that will trigger the callback

            function.  Valid events are: click1, click2, click3,

            space, return, select, up, down, next, prior, move

        :param func: The function that should be called when

            the event occurs.  ``func`` will be called with a

            single item as its argument.  (The item selected

            or the item moved to).

        """
        if event == "select":
            events = ["click1", "space", "return"]
        elif event == "move":
            events = ["up", "down", "next", "prior"]
        else:
            events = [event]

        for e in events:
            self._callbacks.setdefault(e, {})[func] = 1

    def remove_callback(self, event, func=None):
        """

        Deregister a callback function.  If ``func`` is none, then

        all callbacks are removed for the given event.

        """
        if event is None:
            events = list(self._callbacks.keys())
        elif event == "select":
            events = ["click1", "space", "return"]
        elif event == "move":
            events = ["up", "down", "next", "prior"]
        else:
            events = [event]

        for e in events:
            if func is None:
                del self._callbacks[e]
            else:
                try:
                    del self._callbacks[e][func]
                except:
                    pass

    # ////////////////////////////////////////////////////////////
    # Tkinter Methods
    # ////////////////////////////////////////////////////////////

    def pack(self, cnf={}, **kw):
        #        "@include: Tkinter.Pack.pack"
        self._itemframe.pack(cnf, **kw)

    def grid(self, cnf={}, **kw):
        #        "@include: Tkinter.Grid.grid"
        self._itemframe.grid(cnf, *kw)

    def focus(self):
        #        "@include: Tkinter.Widget.focus"
        self._textwidget.focus()

    # ////////////////////////////////////////////////////////////
    # Internal Methods
    # ////////////////////////////////////////////////////////////

    def _init_itemframe(self, options):
        self._itemframe = Frame(self._parent)

        # Create the basic Text widget & scrollbar.
        options.setdefault("background", "#e0e0e0")
        self._textwidget = Text(self._itemframe, **options)
        self._textscroll = Scrollbar(self._itemframe, takefocus=0, orient="vertical")
        self._textwidget.config(yscrollcommand=self._textscroll.set)
        self._textscroll.config(command=self._textwidget.yview)
        self._textscroll.pack(side="right", fill="y")
        self._textwidget.pack(expand=1, fill="both", side="left")

        # Initialize the colorization tags
        self._textwidget.tag_config(
            "highlight", background="#e0ffff", border="1", relief="raised"
        )
        self._init_colortags(self._textwidget, options)

        # How do I want to mark keyboard selection?
        self._textwidget.tag_config("sel", foreground="")
        self._textwidget.tag_config(
            "sel", foreground="", background="", border="", underline=1
        )
        self._textwidget.tag_lower("highlight", "sel")

    def _fire_callback(self, event, itemnum):
        if event not in self._callbacks:
            return
        if 0 <= itemnum < len(self._items):
            item = self._items[itemnum]
        else:
            item = None
        for cb_func in list(self._callbacks[event].keys()):
            cb_func(item)

    def _buttonpress(self, event):
        clickloc = "@%d,%d" % (event.x, event.y)
        insert_point = self._textwidget.index(clickloc)
        itemnum = int(insert_point.split(".")[0]) - 1
        self._fire_callback("click%d" % event.num, itemnum)

    def _keypress(self, event):
        if event.keysym == "Return" or event.keysym == "space":
            insert_point = self._textwidget.index("insert")
            itemnum = int(insert_point.split(".")[0]) - 1
            self._fire_callback(event.keysym.lower(), itemnum)
            return
        elif event.keysym == "Down":
            delta = "+1line"
        elif event.keysym == "Up":
            delta = "-1line"
        elif event.keysym == "Next":
            delta = "+10lines"
        elif event.keysym == "Prior":
            delta = "-10lines"
        else:
            return "continue"

        self._textwidget.mark_set("insert", "insert" + delta)
        self._textwidget.see("insert")
        self._textwidget.tag_remove("sel", "1.0", "end+1char")
        self._textwidget.tag_add("sel", "insert linestart", "insert lineend")

        insert_point = self._textwidget.index("insert")
        itemnum = int(insert_point.split(".")[0]) - 1
        self._fire_callback(event.keysym.lower(), itemnum)

        return "break"


##//////////////////////////////////////////////////////
##  Improved OptionMenu
##//////////////////////////////////////////////////////


class MutableOptionMenu(Menubutton):
    def __init__(self, master, values, **options):
        self._callback = options.get("command")
        if "command" in options:
            del options["command"]

        # Create a variable
        self._variable = variable = StringVar()
        if len(values) > 0:
            variable.set(values[0])

        kw = {
            "borderwidth": 2,
            "textvariable": variable,
            "indicatoron": 1,
            "relief": RAISED,
            "anchor": "c",
            "highlightthickness": 2,
        }
        kw.update(options)
        Widget.__init__(self, master, "menubutton", kw)
        self.widgetName = "tk_optionMenu"
        self._menu = Menu(self, name="menu", tearoff=0)
        self.menuname = self._menu._w

        self._values = []
        for value in values:
            self.add(value)

        self["menu"] = self._menu

    def add(self, value):
        if value in self._values:
            return

        def set(value=value):
            self.set(value)

        self._menu.add_command(label=value, command=set)
        self._values.append(value)

    def set(self, value):
        self._variable.set(value)
        if self._callback:
            self._callback(value)

    def remove(self, value):
        # Might raise indexerror: pass to parent.
        i = self._values.index(value)
        del self._values[i]
        self._menu.delete(i, i)

    def __getitem__(self, name):
        if name == "menu":
            return self.__menu
        return Widget.__getitem__(self, name)

    def destroy(self):
        """Destroy this widget and the associated menu."""
        Menubutton.destroy(self)
        self._menu = None


##//////////////////////////////////////////////////////
##  Test code.
##//////////////////////////////////////////////////////


def demo():
    """

    A simple demonstration showing how to use canvas widgets.

    """

    def fill(cw):
        from random import randint

        cw["fill"] = "#00%04d" % randint(0, 9999)

    def color(cw):
        from random import randint

        cw["color"] = "#ff%04d" % randint(0, 9999)

    cf = CanvasFrame(closeenough=10, width=300, height=300)
    c = cf.canvas()
    ct3 = TextWidget(c, "hiya there", draggable=1)
    ct2 = TextWidget(c, "o  o\n||\n___\n  U", draggable=1, justify="center")
    co = OvalWidget(c, ct2, outline="red")
    ct = TextWidget(c, "o  o\n||\n\\___/", draggable=1, justify="center")
    cp = ParenWidget(c, ct, color="red")
    cb = BoxWidget(c, cp, fill="cyan", draggable=1, width=3, margin=10)
    equation = SequenceWidget(
        c,
        SymbolWidget(c, "forall"),
        TextWidget(c, "x"),
        SymbolWidget(c, "exists"),
        TextWidget(c, "y: "),
        TextWidget(c, "x"),
        SymbolWidget(c, "notequal"),
        TextWidget(c, "y"),
    )
    space = SpaceWidget(c, 0, 30)
    cstack = StackWidget(c, cb, ct3, space, co, equation, align="center")
    prompt_msg = TextWidget(
        c, "try clicking\nand dragging", draggable=1, justify="center"
    )
    cs = SequenceWidget(c, cstack, prompt_msg)
    zz = BracketWidget(c, cs, color="green4", width=3)
    cf.add_widget(zz, 60, 30)

    cb.bind_click(fill)
    ct.bind_click(color)
    co.bind_click(fill)
    ct2.bind_click(color)
    ct3.bind_click(color)

    cf.mainloop()
    # ShowText(None, 'title', ((('this is text'*150)+'\n')*5))


if __name__ == "__main__":
    demo()