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 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868
|
# Copyright and User License
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
# Copyright Vasilis.Vlachoudis@cern.ch for the
# European Organization for Nuclear Research (CERN)
#
# Please consult the flair documentation for the license
#
# DISCLAIMER
# ~~~~~~~~~~
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY, OF
# SATISFACTORY QUALITY, AND FITNESS FOR A PARTICULAR PURPOSE
# OR USE ARE DISCLAIMED. THE COPYRIGHT HOLDERS AND THE
# AUTHORS MAKE NO REPRESENTATION THAT THE SOFTWARE AND
# MODIFICATIONS THEREOF, WILL NOT INFRINGE ANY PATENT,
# COPYRIGHT, TRADE SECRET OR OTHER PROPRIETARY RIGHT.
#
# LIMITATION OF LIABILITY
# ~~~~~~~~~~~~~~~~~~~~~~~
# THE COPYRIGHT HOLDERS AND THE AUTHORS SHALL HAVE NO
# LIABILITY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
# CONSEQUENTIAL, EXEMPLARY, OR PUNITIVE DAMAGES OF ANY
# CHARACTER INCLUDING, WITHOUT LIMITATION, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES, LOSS OF USE, DATA OR PROFITS,
# OR BUSINESS INTERRUPTION, HOWEVER CAUSED AND ON ANY THEORY
# OF CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE), PRODUCT
# LIABILITY OR OTHERWISE, ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
#
# Author: Vasilis Vlachoudis
# Email: Vasilis.Vlachoudis@cern.ch
# Date: 12-Oct-2006
from __future__ import absolute_import
import Utils
__author__ = Utils.__author__
__email__ = Utils.__email__
GLOBAL_CONTROL_BACKGROUND = "White"
import re
import time
import Unicode
import bFileDialog
import sys
#from log import say
from Utils import unicode,to_zip
try:
from Tkinter import *
from Tkinter import _setit, _cnfmerge
from tkColorChooser import askcolor
except ImportError:
from tkinter import *
from tkinter import _setit, _cnfmerge
from tkinter.colorchooser import askcolor
ARROW_LEFT = u"\u2190"
ARROW_UP = u"\u2191"
ARROW_RIGHT = u"\u2192"
ARROW_DOWN = u"\u2193"
CROSS = u"\u2A2F" # x -cross product
MULT = u"\u00D7" # x -multiplication
DIAMOND_SQUARE = u"\u26CB"
# Key state codes
SHIFT_MASK = 1
CONTROL_MASK = 4
ALT_MASK = 8
# Ansi escape sequences
ANSI_CLEAR = "\033[2J"
ANSI_BOLD = "\033[1m"
ANSI_UNDERLINE = "\033[4m"
ANSI_REVERSE = "\033[7m"
ANSI_NORMAL = "\033[m"
# base64.encodestring(open("save.gif","rb").read())
_SAVEICON = """
R0lGODlhEAAQAOcBAAAAAP//////////////////////////////////////////////////////
/////////0xLS0RDRLO0ubO0ubO0ubO0ubS2ubS2u7a4vLe5vLi5vLm5vERDRExLS////////0JA
QVpZWfH0+vD0+vD0+vD0+vH0+vL1+vP2+/X4/Pf5/Pn7/WVjZEJAQf///////0A+P2JgYfn6/Zy2
z5y2z5y2z5y2z522z523z5630J+40P39/mJgYUA+P////////z08PV5cXfv8/fj5+/n5+/j5+/j5
+/f4+vX3+vT2+fL0+PL2+V5cXT08Pf///////zs6O1tZWu7z9+/z9+/z9+/z9+7z9+7y9u3x9uvw
9env9Oft81tZWjs6O////////zk3OFdVVuLq8YuiuIuiuIuiuIuiuIqht4qht4qgt4mgttzl7VdV
Vjk3OP///////zc1NlRSU9Te6NDd6NDd6NDd6NDc58/c58/b587b5s3a5s/a5lRSUzc1Nv//////
/zUzM1FOT1FOT1FOT1FOT1FOT1FOT1FOT1FOT1FOT1FOT1FOT1FOTzUzM////////zIwMU1KS01K
S01KS01KS01KS01KS01KS01KS01KS01KS01KS01KSzIwMf///////zAtLklFRklFRs3NzdbW1tbV
1dXV1dbW1tbW1tPT0ykmJyYjJElFRjAtLv///////yspKkI/QEI/QKenpz88PTUzM66urq6urq6u
rq6urkJAQSgnJ0ZDRCspKv///////yYkJTs4OTs4OZmZmTQxMiglJp+fn5+fn5+fn5+fn0E+PyYk
JVpXWCYkJf///////yQiIi0qKjUxMouKiysoKSEfIJCQkJCQkJCQkJCQkDYzNCEfIFFNTiMgIf//
/////yUfHx8dHR4bHHV1dXh4eHh4eHh4eHh4eHh4eHh4eBsZGhsZGR8cHSEfH///////////////
/////////////////////////////////////////////////////yH5BAEKAP8ALAAAAAAQABAA
AAj4AP8JHEiwoMAIEiZQqGDhAoYMGjZw6OBBYAgRI0iUMHECRQoVK1i0cCEwhowZNGrYuIEjh44d
PHr4EBhEyBAiRYwcQZJEyRImTZwIjCJlCpUqVq5gyaJlC5cuXgSGETOGTBkzZ9CkUbOGTRs3AuPI
mUOnjp07ePLo2cOnjx+BgQQNIlTI0CFEiRQtYtTIkcBIkiZRqmTpEqZMmjZx6uRJYChRo0iVMnUK
VSpVq1i1ciUwlqxZtGrZuoUrl65dvHr5EhhM2DBixYwdQ5ZM2TJmzZwJjCZtGrVq1q5hy6ZtG7du
3gSGEzeOXDlz59ClU7eOXTt3BrMTDAgAOw==
"""
_ALLICON = """
R0lGODlhEAAQAKUiAMwjzMcmx6wt2MEqv7ksxLosxL8su6wv1sMru4c4674swLwtvb8sv70tvbou
vrouwrovucQsurovurkwu7gxuLsxu7szu01N/9gtrNgtrbo1uro2uus4h61XraxZrP9NTYech03/
Tf//////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////yH5BAEKAD8ALAAAAAAQABAAAAaMwJ9Q
OEBgMojBcEmMcD4fTkTJ/AEQT2gUAagCMlptpsv8hqFjLzbM4VZ/AydUSl0CAAGI4WiABO5DDBYe
HRUSAQESDgIHDwo/DBsgISEgGw0LBAkXFwkEChaTlJUUE5ucnQ8do6MaBaioB6usIa6wnAehrCCl
p5wJD5Gilpiav5+Qgx0WDEIKD4yOP0EAOw==
"""
_NONEICON = """
R0lGODlhEAAQAKUhAAAAAA8EBAYLEBgHBwQPBAkLHgoKKwcOFQoLKwUTBQsOJyYKCgoSGysKCg4R
LwcYBy8MDBERRhISSQgeCEYRERgYYkkSEgomCgorCgwvDCAggGIYGIAgIBFGERJJEhhiGCCAIP//
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////yH5BAEKAD8ALAAAAAAQABAAAAaGwJ9Q
OIBQLJDBcElsbDicTUPJFEKe0CikKrRksxbuz/vlhLnX72bLHTihUmoVEFgcFwEA85HpeC4EAAAC
ChESDgw/DxgfICAfGAkHCBUaGhUIDBmNjo8TBZWWlw4enZ0YBqKiEqWmIKiqlhKbph+foZYVDouc
kJKUuZmKfR4ZD0IMDoaIP0EAOw==
"""
_INVICON = """
R0lGODlhEAAQAOeLAFBQUFBQWFFRUVJSUk1UVE9UVFNUVFRUVFRVVVVVVdI1NVdXV1RYWNM2NlhY
XtM4OFlZXdM6OtM7O1lcXF5bW9Q8PNQ9PdQ+PtQ/P1phYdVAQGBgYNVBQWFhZNVCQmJiYtVDQ9VE
RG5gYGNjY9dERNVFRWZkZNZGRtpFRWVlZdZHRtZHR9hHR9ZISNZJSWdnZ9ZKStZKT9xJSdZLS2pq
a9dNTdhOToRlZWxsbNhPT9hPU21ta9hQUNlQUJthYdhRUdlRUdhSUNhSUppjY9lTVNlWVnJyctlX
V3V1WOFWVt5XV291dXR0dOBXV91YWNpZWdpZW6FpaXV1dXJ3d9pbXNpcXnd3d8liYtNgYNVgYNtf
X79nZ9xhYb1qatxiYnx8fHp9fX19fdZoaICAgN9oaN5sWYODg4WFhd9tbd9vb4mIiN9xceBzc7l+
fqeDg+B0dIOPj+F3d46Ojo+Pj5OTk5eXYZmZTJSUlJWUlKOUlJ2dVeWLWZ2dnZWioqGhoaenp+qj
T+2xUfLCRfLHWNnZON/fPODgMPbYSOPjMvffRvnmO///////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////yH5BAEKAP8ALAAAAAAQABAA
AAjhAP8JHOjlyRMvAxMKTMMDg4QHDSRUEMJGIZoWJTBEaKDgAYcQK9YMfGNjxgkOFh48sKChBIwa
cQRqASIDBQgNF1iWaFHjBxeBTqLA6dOGRAgPJ1QEKaOjiBgyIkxYOYMnj4sWMGIMOgSISpYhBhak
kDJGjRsWOY4kUiQIChYwCQLoqUODyZQtPYjsCVRFSRczB+wQMrSDwocMPrg0SXLlxpJ/A5AgKuQg
wYEDCAhMYFBAwJx/Lw50gJCgtGnTOATSAXC6dQIAfAbKEeC6NIA7Cv1sGHB6wIg/ChN+MWIkTPCA
ADs=
"""
#-------------------------------------------------------------------------------
# bind event with data as a replacement of the Tkinter bind for virtual
# events to send data <<...>>
#
# Example, instead of binding like
# widget.bind("<<VirtualEvent>>", function)
# use it as
# bindEventData(widget, "<<VirtualEvent>>", function)
#
# def function(event):
# print event.serial, event.widget, event.data
#
# Send message as
# widget.event_generate("<<VirtualEvent>>", data="Hello")
# widget.event_generate("<<VirtualEvent>>", data=("One","Two"))
# widget.event_generate("<<VirtualEvent>>", serial=10, data=("One","Two"))
#
# WARNING: Unfortunatelly it will convert data to STRING!!!
#-------------------------------------------------------------------------------
def bindEventData(widget, sequence, func, add = None):
def _substitute(*args):
e = Event()
nsign, b, t, T, d, W = args
try: e.serial = int(nsign)
except: e.serial = nsign
try: e.num = int(b)
except: e.num = b
try: e.time = int(t)
except: e.time = t
e.type = T
e.data = d
try:
e.widget = widget._nametowidget(W)
except KeyError:
e.widget = W
return (e,)
funcid = widget._register(func, _substitute, needcleanup=1)
cmd = '{0}if {{"[{1} %# %b %t %T %d %W]" == "break"}} break\n'.format('+' if add else '', funcid)
widget.tk.call('bind', widget._w, sequence, cmd)
#===============================================================================
# Sort Assist class for MultiListbox
#===============================================================================
class SortAssist:
def __init__(self, column):
self.column = column
def __call__(self, x):
return x[self.column]
#-------------------------------------------------------------------------------
# Multiple configuration of many widgets given in a list
# lst = list of widgets
#-------------------------------------------------------------------------------
def multiConfig(lst, **opts):
"""Multiple configuration of many widgets"""
for w in lst:
w.config(**opts)
#-------------------------------------------------------------------------------
# Toggle toplevel window height
#-------------------------------------------------------------------------------
def toggleHeight(root, oldHeight):
"""Toggle window height"""
m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", root.wm_geometry())
if not m:
root.bell()
return oldHeight
width, height, x, y = map(int, m.groups())
maxheight = root.winfo_screenheight()
if sys.platform in ("win32", "win64"):
newy = 0
newheight = maxheight - 72
else:
#newy = 24
newy = 0
#newheight = maxheight - 96
newheight = maxheight - 88
if height >= newheight:
try:
newheight = oldHeight
except:
return oldHeight
newgeom = "%dx%d+%d+%d" % (width, newheight, x, newy)
root.wm_geometry(newgeom)
return height
#===============================================================================
def _entryPaste(event):
"""global replacement for the Entry.paste"""
try:
event.widget.delete('sel.first', 'sel.last')
except TclError:
pass # nothing is selected
# in tk.call() use the widget's string representation event.widget._w
# instead of event.widget, which is the widget instance itself
try:
text = event.widget.tk.call('::tk::GetSelection', event.widget._w, 'CLIPBOARD')
except TclError:
return
event.widget.insert('insert', text)
event.widget.tk.call('tk::EntrySeeInsert', event.widget._w)
return "break"
#-------------------------------------------------------------------------------
def _textPaste(event):
"""global replacement for the Text.paste"""
oldSeparator = event.widget.cget("autoseparators")
if oldSeparator:
event.widget.config(autoseparators=0)
event.widget.edit_separator()
try:
event.widget.delete('sel.first', 'sel.last')
except TclError:
pass # nothing is selected
# in tk.call() use the widget's string representation event.widget._w
# instead of event.widget, which is the widget instance itself
try:
text = event.widget.tk.call('::tk::GetSelection', event.widget._w, 'CLIPBOARD')
except TclError:
return
event.widget.insert('insert', text)
if oldSeparator:
event.widget.edit_separator()
event.widget.config(autoseparators=1)
event.widget.see('insert')
return "break"
#-------------------------------------------------------------------------------
def bindClasses(root):
root.bind_class('Entry', '<Control-Key-a>', lambda e: e.widget.selection_range(0,END))
root.bind_class('Entry', '<<Paste>>', _entryPaste)
root.bind_class('Text', '<<Paste>>', _textPaste)
#===============================================================================
# LabelEntry. display a label when entry field is empty
#===============================================================================
class LabelEntry(Entry):
def __init__(self, master, label=None, labelcolor=None, **kw):
Entry.__init__(self, master, **kw)
self.label = label
self._empty = True
self._fg = self["foreground"]
if labelcolor is not None:
self.labelcolor = labelcolor
else:
self.labelcolor = self._fg
self.bind("<FocusIn>", self._focusIn)
self.bind("<FocusOut>", self._focusOut)
self["validate"] = "key"
self["validatecommand"] = (self.register(self.validate), '%P')
self.showLabel()
# ----------------------------------------------------------------------
def showLabel(self):
self.delete(0,END)
self.insert(0, self.label)
self["foreground"] = self.labelcolor
self._empty = True # Restore empty since validation will destroy it
# ----------------------------------------------------------------------
def removeLabel(self):
self.delete(0,END)
self["foreground"] = self._fg
# ----------------------------------------------------------------------
def _focusIn(self, event):
if self._empty:
self.removeLabel()
# ----------------------------------------------------------------------
def _focusOut(self, event):
if self._empty or self.get()=="":
self.showLabel()
# ----------------------------------------------------------------------
def validate(self, value):
self._empty = value == ""
return True
# ----------------------------------------------------------------------
def set(self, value):
self._empty = value==""
if self._empty:
self.showLabel()
self.master.focus_set() # lose focus
else:
self.removeLabel()
self.insert(0, value)
# ----------------------------------------------------------------------
def get(self):
if self._empty:
return ""
else:
return Entry.get(self)
#===============================================================================
# _ValidatingEntry
#===============================================================================
class _ValidatingEntry(Entry):
"""base class for validating entry widgets"""
# ----------------------------------------------------------------------
def __init__(self, master, value="", **kw):
Entry.__init__(self, master, **kw)
self["validate"] = "key"
self["validatecommand"] = (self.register(self.validate), '%P')
# ----------------------------------------------------------------------
def validate(self, value):
# override: return True if valid False if invalid
return True
# ----------------------------------------------------------------------
def set(self, value):
self.delete(0,END)
self.insert(0, value)
# ----------------------------------------------------------------------
def getint(self, default=0):
try:
return int(self.get())
except:
return default
# ----------------------------------------------------------------------
def getfloat(self, default=0.0):
try:
return float(self.get())
except:
return default
#===============================================================================
# Maximum Length Entry
#===============================================================================
class MaxLengthEntry(_ValidatingEntry):
"""MaxLengthEntry limit entry length maximum maxlength characters"""
def __init__(self, master, value="", maxlength=None, **kw):
_ValidatingEntry.__init__(self, master, value, **kw)
self.maxlength = maxlength
# ----------------------------------------------------------------------
def insert(self, idx, value):
m = self.maxlength
self.maxlength = None
_ValidatingEntry.insert(self, idx, value)
self.maxlength = m
# ----------------------------------------------------------------------
def validate(self, value):
if self.maxlength is not None:
return len(value) <= self.maxlength
return True
#===============================================================================
# Integer Validating Entry
#===============================================================================
class IntegerEntry(_ValidatingEntry):
"""IntegerEntry accepting only integers"""
# ----------------------------------------------------------------------
def validate(self, value):
try:
if value: int(value)
return True
except ValueError:
if value=="+" or value=="-": return True
return False
#===============================================================================
# Floating Point Validating Entry
#===============================================================================
class FloatEntry(_ValidatingEntry):
"""accept only floating point numbers"""
# ----------------------------------------------------------------------
def validate(self, value):
try:
if value: float(value)
return True
except ValueError:
if value=="+" or value=="-" or value=="." or \
value=="+." or value=="-.": return True
if len(value)>1:
last = value[-1]
if last=="e" or last=="E": return True
plast = value[-2]
if (plast=="e" or plast=="E") and \
(last=="-" or last=="+"): return True
return False
#===============================================================================
# Vector Validating Entry
#===============================================================================
class VectorEntry(_ValidatingEntry):
"""accept only vectors"""
# ----------------------------------------------------------------------
def validate(self, value):
# remove from value comma, semicolon, and parenthesis () []
for token in re.sub(r"[(),;\[\]]","",value).split():
try:
float(token)
except ValueError:
if token=="+" or token=="-" or token=="." or \
token=="+." or token=="-.": continue
if len(token)>1:
last = token[-1]
if last=="e" or last=="E": continue
plast = token[-2]
if (plast=="e" or plast=="E") and \
(last=="-" or last=="+"): continue
return False
return True
# ----------------------------------------------------------------------
# Get contents as a list
# ----------------------------------------------------------------------
def getlist(self):
return re.sub(r"[(),;\[\]]","",self.get()).split()
# ---------------------------------------------------------------------
# Split vector in to a list of widgets
# ----------------------------------------------------------------------
def split(self, widgets):
value = self.get()
for ch in " ,()[];":
if ch in value:
xyz = self.getlist()
if xyz:
self.set(xyz[0])
for i,w in enumerate(widgets):
if len(xyz)>i+1: w.set(xyz[i+1])
return
#===============================================================================
# Auto Scroll Bar
# Author: Fredrik Lundh <www.pythonware.com>
#===============================================================================
class AutoScrollbar(Scrollbar):
# ----------------------------------------------------------------------
# a scrollbar that hides itself if it's not needed. only
# works if you use the grid geometry manager.
# ----------------------------------------------------------------------
def set(self, lo, hi):
flo = float(lo)
fhi = float(hi)
try:
g = self.get()
except TclError:
return
if abs(flo-float(g[0]))<=0.001 and abs(fhi-float(g[1]))<=0.001: return
if flo <= 0.001 and fhi >= 0.999:
if self.method==0:
# grid_remove is currently missing from Tkinter!
self.tk.call("grid", "remove", self)
else:
self.tk.call("pack", "forget", self)
elif flo > 0.001 or fhi < 0.999:
if self.method==0:
Scrollbar.grid(self)
else:
Scrollbar.pack(self)
Scrollbar.set(self, lo, hi)
# ----------------------------------------------------------------------
def grid(self, **kw):
self.method = 0
Scrollbar.grid(self, **kw)
# ----------------------------------------------------------------------
def pack(self, **kw):
self.method = 1
Scrollbar.pack(self, **kw)
#raise TclError("cannot use pack with this widget")
# ----------------------------------------------------------------------
def place(self, **kw):
raise TclError("cannot use place with this widget")
#===============================================================================
# ProgressBar Canvas
#===============================================================================
class ProgressBar(Canvas):
def __init__(self, master=None, **kw):
Canvas.__init__(self, master, **kw)
#self.config(background="DarkGray")
self.currBox = self.create_rectangle(0, 0, 0, 0,
fill='Orange',
width=0)
self.doneBox = self.create_rectangle(0, 0, 0, 0,
fill='DarkGreen',
width=0)
self.text = self.create_text(0,0,
text="",
fill="White",
anchor=CENTER,
justify=CENTER)
self.auto = True
self.showTime = True
self.bind('<Configure>', self.draw)
self.setLimits()
# ----------------------------------------------------------------------
def setAuto(self, auto):
self.auto = auto
# ----------------------------------------------------------------------
def setShowTime(self, b):
self.showTime = b
# ----------------------------------------------------------------------
def setStartTime(self, t0):
self.t0 = t0
# ----------------------------------------------------------------------
def setLimits(self, low=0.0, high=100.0, step=1.0):
self.low = float(low)
self.high = float(high)
self.length = float(high-low)
self.step = float(step)
self.done = float(low)
self.now = float(low)
self.t0 = time.time()
self.msg = ""
# ----------------------------------------------------------------------
def setProgress(self, now, done=None, txt=None):
self.now = now
if self.now < self.low:
self.now = self.low
elif self.now > self.high:
self.now = self.high
if done is None:
self.done = now - self.step
else:
self.done = done
if self.done < self.low:
self.done = self.low
elif self.done > self.high:
self.done = self.high
# calculate remaining time
dt = time.time() - self.t0
p = now - self.low
if p>0:
tot = dt/p*(self.high-self.low)
else:
tot = 0.0
# elapsed time
dh,s = divmod(dt,3600)
dm,ds = divmod(s,60)
if dh > 0:
elapsedTxt = "%dh%02dm"%(dh,dm)
elif dm > 0:
elapsedTxt = "%dm%02ds"%(dm,ds)
else:
elapsedTxt = "%ds"%(ds)
# total time
th,s = divmod(tot,3600)
tm,ts = divmod(s,60)
if th > 0:
totalTxt = "Tot: %dh%02dm"%(th,tm)
elif tm > 0:
totalTxt = "Tot: %dm%02ds"%(tm,ts)
else:
totalTxt = "Tot: %ds"%(ts)
# remain time
remain = tot - dt
if remain>0:
rh,s = divmod(remain,3600)
rm,rs = divmod(s,60)
if rh > 0:
remainTxt = "Rem: %dh%02dm"%(rh,rm)
elif rm > 0:
remainTxt = "Rem: %dm%02ds"%(rm,rs)
else:
remainTxt = "Rem: %ds"%(rs)
else:
remainTxt = ""
self.draw()
if txt is not None:
self.setText(txt)
elif self.auto:
if self.showTime:
self.autoText("[%s %s %s]"%(elapsedTxt, totalTxt, remainTxt))
else:
self.autoText("")
# ----------------------------------------------------------------------
def clear(self):
self.setProgress(0, 0);
# ----------------------------------------------------------------------
def setText(self, txt):
self.itemconfig(self.text, text=txt)
# ----------------------------------------------------------------------
def configText(self, **args):
self.itemconfig(self.text, **args)
# ----------------------------------------------------------------------
def autoText(self, tmsg):
completed = self.done - self.low
if self.low != 0:
low = "%d - "%(self.low)
else:
low = ""
self.msg = "Current: %d [%s%d] Completed: %d%% %s" % \
(self.now, low, self.high,
int((100*completed)/self.length),
tmsg)
self.setText(self.msg)
# ----------------------------------------------------------------------
def getProgress(self):
return (self.done, self.now)
# ----------------------------------------------------------------------
def draw(self, event=None):
width = self.winfo_width()
height = self.winfo_height()
wn = int(width * (self.now - self.low) / self.length)
wd = int(width * (self.done - self.low) / self.length)
if wd >= wn: wd = wn - 1
self.coords(self.currBox, 0, 0, wn, height)
self.coords(self.doneBox, 0, 0, wd, height)
if self.itemcget(self.text, "justify") == CENTER:
self.coords(self.text, width/2, height/2)
else:
self.coords(self.text, 1,height/2)
#===============================================================================
# Extended Listbox
#===============================================================================
class ExListbox(Listbox):
"""Listbox that allows keyboard scanning, and a popup menu"""
_KEY_TIME_THRESHOLD = 1000 # ms
_searchTop = None
_searchLabel = None
_search = ""
_searchOrig = ""
_time = 0
def __init__(self, master, **kw):
Listbox.__init__(self, master, **kw)
ExListbox.resetSearch()
self._single = kw.get('selectmode','') in [SINGLE, BROWSE]
# self.bind('<Button-1>', lambda e,s=self:s.focus_set())
self.bind('<Key>', self.handleKey)
self.bind('<Home>', lambda e,s=self:s._scrollTo(0))
self.bind('<Prior>', lambda e,s=self:s._scrollTo(-1, PAGES))
self.bind('<Next>', lambda e,s=self:s._scrollTo( 1, PAGES))
self.bind('<End>', lambda e,s=self:s._scrollTo(END))
self.bind('<FocusOut>', ExListbox._hideSearch)
self.bind('<Unmap>', ExListbox._hideSearch)
self.bind('<<Cut>>', self.copy)
self.bind('<<Copy>>', self.copy)
if not self._single:
self.bind('<Control-Key-a>', self.selectAll)
self.bind('<Control-Shift-A>', self.selectClear)
self.bind('<Button-3>', self.popupMenu)
self.bind('<Control-Key-space>',self.popupMenu)
# User variables to modify
self.additionalChar = "-+._:$%#*"
self.ignoreNonAlpha = True # Ignore non-alpha characters
self.ignoreCase = True # Ignore case of letters
self.showSearch = True
self.usermenu = None # Assign a user-popup menu
# Should be a list with tuples
# in the form:
# (label, underline, commmand)
# or None for separator
# ----------------------------------------------------------------------
def setPopupMenu(self, menu=None):
"""Setup a popup menu list it should be in the form
[ (label, underline, command), ... ]"""
self.usermenu = menu
self.bind('<Button-3>', self.popupMenu)
self.bind('<Control-Key-space>',self.popupMenu)
# ----------------------------------------------------------------------
@staticmethod
def resetSearch():
"""Reset search string"""
ExListbox._search = ""
ExListbox._searchOrig = ""
ExListbox._time = 0
if ExListbox._searchTop is not None:
try:
ExListbox._searchTop.withdraw()
except TclError:
ExListbox._searchTop = None
# ----------------------------------------------------------------------
@staticmethod
def _hideSearch(event=None):
if ExListbox._searchTop is not None:
try: ExListbox._searchTop.withdraw()
except: pass
# ----------------------------------------------------------------------
def _showSearch(self):
if ExListbox._searchTop is None:
ExListbox._searchTop = Toplevel()
ExListbox._searchTop.overrideredirect(1)
ExListbox._searchLabel = Label(ExListbox._searchTop,
anchor=E,
relief=SOLID,
background="Yellow",
takefocus=False,
borderwidth=1)
ExListbox._searchLabel.pack(fill=BOTH)
if ExListbox._searchOrig == "":
ExListbox._hideSearch()
return
ExListbox._searchLabel["text"]=ExListbox._searchOrig
ExListbox._searchTop.update_idletasks()
# Guess position
x = self.winfo_rootx() + self.winfo_width() \
- ExListbox._searchLabel.winfo_width()
y = self.winfo_rooty() + self.winfo_height()-12
ExListbox._searchTop.wm_geometry("+%d+%d" % (x,y))
ExListbox._searchTop.deiconify()
ExListbox._searchTop.lift()
ExListbox._searchTop.update_idletasks()
# ----------------------------------------------------------------------
# Handle key events for quick searching
# ----------------------------------------------------------------------
def handleKey(self, event):
"""handle key events for quick searching"""
# Shift key -> ignore them
if event.keysym in ("Shift_L","Shift_R"):
return
elif len(event.char)==0:
ExListbox._time = 0
return
if self.ignoreCase:
ch = event.char.upper()
else:
ch = event.char
oldActive = self.index(ACTIVE)
again = False
# Delete search
if event.keysym in ("Delete","Escape","Return","KP_Enter"):
ExListbox.resetSearch()
return
# Search Again Ctrl-G
elif event.char=='\007':
# Space bar selects...
#(event.char==' ' and self.ignoreNonAlpha):
self.activate(oldActive+1)
again = True
# Backspace
elif event.keysym == "BackSpace":
ExListbox._search = ExListbox._search[:-1]
ExListbox._searchOrig = ExListbox._searchOrig[:-1]
# Ignore non-printable characters
elif self.ignoreNonAlpha and \
not (ch.isalnum() or \
self.additionalChar.find(ch)>=0):
return
# Timeout
elif event.time - ExListbox._time > ExListbox._KEY_TIME_THRESHOLD:
# Start a new search
ExListbox._search = ch
ExListbox._searchOrig = event.char
else:
ExListbox._search += ch
ExListbox._searchOrig += event.char
if self.showSearch: self._showSearch()
lsearch = len(ExListbox._search)
ExListbox._time = event.time
start = 0
cur = self.index(ACTIVE)
try :
active = unicode(self.get(ACTIVE))
except Exception as e:
active = str(self.get(ACTIVE))
if self.ignoreCase:
try: active = active.upper()
except: pass
if len(active)>0:
if self.ignoreNonAlpha:
for pos in range(len(active)):
if active[pos].isalnum() or self.additionalChar.find(active[pos])>=0:
break
else:
pos = 0
prefix = active[pos:pos+lsearch]
if ExListbox._search == prefix:
if self._single:
self.selection_clear(0, END)
self.selection_set(cur)
self.activate(cur)
self.see(cur)
self.event_generate("<<ListboxSelect>>")
return 'break'
elif ExListbox._search[:-1] == prefix[:-1]:
start = cur+1
loop = 1
while loop <= 2:
if again:
start = cur+1
again = False
#elif oldActive != self.index(ACTIVE):
else:
start = 0
loop += 1
for i in range(start, self.size()):
try :
item = unicode(self.get(i))
except Exception as e :
item = str(self.get(i))
if self.ignoreCase:
try: item = item.upper()
except: pass
if len(item)>0:
if self.ignoreNonAlpha:
for pos in range(len(item)):
if item[pos].isalnum() or self.additionalChar.find(item[pos])>=0:
break
else:
pos = 0
prefix = item[pos:pos+lsearch]
if ExListbox._search == prefix:
if self._single:
self.selection_clear(0, END)
self.selection_set(i)
self.activate(i)
self.see(i)
self.event_generate("<<ListboxSelect>>")
return "break"
loop += 1
if oldActive != self.index(ACTIVE):
self.activate(oldActive)
# ----------------------------------------------------------------------
# Create the popup menu
# ----------------------------------------------------------------------
def popupMenu(self, event):
"""Create popup menu with default actions"""
if self["state"] == DISABLED: return
self.focus_set()
menu=Menu(self, tearoff=0)
if self.usermenu:
for entry in self.usermenu:
if entry is None:
menu.add_separator()
else:
name,und,cmd = entry[:3]
if len(entry)>3:
icon = entry[3]
else:
icon = None
menu.add_command(label=name, underline=und,
image=icon, compound=LEFT,
command=cmd)
if not self._single: menu.add_separator()
if not self._single:
self._ALLICON = PhotoImage(data=_ALLICON)
self._NONEICON = PhotoImage(data=_NONEICON)
self._INVICON = PhotoImage(data=_INVICON)
menu.add_command(label='All', underline=0,
image=self._ALLICON, compound=LEFT,
command=self.selectAll)
menu.add_command(label='Clear', underline=0,
image=self._NONEICON, compound=LEFT,
command=self.selectClear)
menu.add_command(label='Invert', underline=0,
image=self._INVICON, compound=LEFT,
command=self.selectInvert)
menu.tk_popup(event.x_root, event.y_root)
return "break"
# ----------------------------------------------------------------------
# Selection
# ----------------------------------------------------------------------
def selectAll(self, event=None):
"""Select all items"""
self.selection_set(0, END)
self.event_generate("<<ListboxSelect>>")
return "break"
# ----------------------------------------------------------------------
def selectClear(self, event=None):
"""Selection Clear"""
self.selection_clear(0, END)
self.event_generate("<<ListboxSelect>>")
return "break"
# ----------------------------------------------------------------------
def selectInvert(self, event=None):
"""Invert selection"""
for i in range(self.size()):
if self.select_includes(i):
self.selection_clear(i)
else:
self.selection_set(i)
self.event_generate("<<ListboxSelect>>")
return "break"
# ----------------------------------------------------------------------
# return active and selected items
# ----------------------------------------------------------------------
def getSelected(self):
"""return a tuple of active and selected items
for restoring later"""
return (self.index(ACTIVE), map(int, self.curselection()))
# ----------------------------------------------------------------------
# select active and selected items
# ----------------------------------------------------------------------
def selectSaved(self, save, default=None):
"""selected the saved items.
If list has changed then selected the default item"""
self.selection_clear(0,END)
if save is not None:
self.activate(save[0])
for s in save[1]:
self.selection_set(s)
self.see(save[0])
if default is not None:
if save is None or \
(save is not None and save[0] >= self.size()):
if isinstance(default, tuple):
self.selection_set(default[0], default[1])
self.activate(default[0])
else:
self.selection_set(default)
self.activate(default)
self.event_generate("<<ListboxSelect>>")
# ----------------------------------------------------------------------
def _scrollTo(self, pos, unit=None):
if unit:
self.yview_scroll(pos, unit)
else:
if self._single:
self.selection_clear(0, END)
self.selection_set(pos)
self.activate(pos)
self.see(pos)
self.event_generate("<<ListboxSelect>>")
return 'break'
# ----------------------------------------------------------------------
# Change the value of a list item
# and return the value of the old one
# ----------------------------------------------------------------------
def set(self, index, value):
"""Set/Change the value of a list item"""
try:
sel = self.selection_includes(index)
act = self.index(ACTIVE)
self.delete(index)
except TclError:
return
self.insert(index, value)
if sel: self.selection_set(index)
self.activate(act)
self.event_generate("<<ListboxSelect>>")
# ----------------------------------------------------------------------
# Swap two items in the list
# ----------------------------------------------------------------------
def swap(self, a, b):
"""Swap two items in the list"""
if a>b: a, b = b, a
at = self.get(a);
bt = self.get(b);
self.delete(b);
self.delete(a);
self.insert(a, bt);
self.insert(b, at);
# ----------------------------------------------------------------------
# Move up select items by one
# ----------------------------------------------------------------------
def moveUp(self):
"""Move selected items up"""
for i in map(int,self.curselection()):
if i==0: continue
prev = i-1
if not self.selection_includes(prev):
act = self.index(ACTIVE)
self.swap(prev,i)
self.selection_set(prev)
if act == i: self.activate(prev)
self.event_generate("<<ListboxSelect>>")
# ----------------------------------------------------------------------
# Move down select items by one
# ----------------------------------------------------------------------
def moveDown(self):
"""Move selected items down"""
sz = self.size()-1
lst = map(int,self.curselection())
lst.reverse()
for i in lst:
if i >= sz: continue
next = i+1
if not self.selection_includes(next):
act = self.index(ACTIVE)
self.swap(i,next)
self.selection_set(next)
if act == i: self.activate(next)
self.event_generate("<<ListboxSelect>>")
# ----------------------------------------------------------------------
def deleteByName(self, item):
"""delete entry by name"""
act = self.index(ACTIVE)
for i in range(self.size()-1,-1,-1):
it = self.get(i)
if it == item:
self.delete(i)
self.activate(act)
# ----------------------------------------------------------------------
# Fill the listbox
# ----------------------------------------------------------------------
def fill(self, items=None):
self.delete(0,END)
for item in items: self.insert(END, item)
# ----------------------------------------------------------------------
# Copy current elements to clipboard
# ----------------------------------------------------------------------
def copy(self, event=None):
sel = self.curselection()
if not sel: return
items = []
for i in sel:
items.append(self.get(i))
self.clipboard_clear()
self.clipboard_append("\n".join(items))
#===============================================================================
# Search Listbox
# A listbox that the list is narrowing down to the matching items
#===============================================================================
class SearchListbox(ExListbox):
def __init__(self, master, **kw):
ExListbox.__init__(self, master, **kw)
self.prefixSearch = False
self._items = []
self._pos = []
# ----------------------------------------------------------------------
# Fill the listbox
# ----------------------------------------------------------------------
# def fill(self, items=None):
# del self._items[:]
# if items is None:
# for item in Listbox.get(self,0,END):
# self._items.append(unicode(item))
# else:
# self.delete(0,END)
# for item in items:
# item = unicode(item)
# self._items.append(item)
# self.insert(END, item)
# self._pos = range(len(self._items))
# ----------------------------------------------------------------------
def reset(self):
if self._items and ExListbox._search:
ExListbox.resetSearch()
Listbox.delete(self, 0, END)
for item in self._items:
Listbox.insert(self, END, item)
del self._items[:]
del self._pos[:]
# ----------------------------------------------------------------------
def handleKey(self, event):
"""handle key events for quick searching"""
if len(event.char)==0:
ExListbox._time = 0
return
backspace = False
# Delete search
if event.keysym in ("Delete", "Escape"):
ExListbox.resetSearch()
backspace = True
# Backspace
elif event.keysym == "BackSpace":
ExListbox._search = ExListbox._search[:-1]
ExListbox._searchOrig = ExListbox._searchOrig[:-1]
backspace = True
# Ignore non-printable characters
elif self.ignoreNonAlpha and \
not (event.char.isalnum() or \
self.additionalChar.find(event.char)>=0):
return
# Normal character
else:
if self.ignoreCase:
ExListbox._search += event.char.upper()
else:
ExListbox._search += event.char
ExListbox._searchOrig += event.char
if self.showSearch: self._showSearch()
# Remember time and active
ExListbox._time = event.time
active = Listbox.get(self,ACTIVE)
activepos = 0
search = ExListbox._search
prefix = self.prefixSearch
if search and search[0]=="*":
search = search[1:]
prefix = not prefix
# Fill up the list of items
if not self._items:
for item in Listbox.get(self,0,END):
self._items.append(unicode(item))
self._pos = range(len(self._items))
# if Search string is empty, fill the entire list
if not search:
Listbox.delete(self, 0, END)
for i,item in enumerate(self._items):
if active == item: activepos = i
Listbox.insert(self, END, item)
self._pos = range(len(self._items))
# Backspace removes one character then we need to expand the list
elif backspace:
# FIXME I could find the correct position and insert it
# instead of delete all and repopulate
Listbox.delete(self, 0, END)
del self._pos[:]
for i,item in enumerate(self._items):
if prefix:
if self.ignoreCase:
if item.upper().startswith(search):
if active == item: activepos = i
Listbox.insert(self, END, item)
self._pos.append(i)
else:
if item.startswith(search):
if active == item: activepos = i
Listbox.insert(self, END, item)
self._pos.append(i)
else:
if self.ignoreCase:
if item.upper().find(search)>=0:
if active == item: activepos = i
Listbox.insert(self, END, item)
self._pos.append(i)
else:
if item.find(search)>=0:
if active == item: activepos = i
Listbox.insert(self, END, item)
self._pos.append(i)
else:
# FIXME I could use the fnmatch or re to allow * and ? as pattern
# If a new character added then shrink the existing list
# Scan in reverse order
for i in range(Listbox.size(self)-1, -1, -1):
item = Listbox.get(self, i)
if active == item: activepos = i
if self.ignoreCase: item = item.upper()
if prefix:
if not item.startswith(search):
Listbox.delete(self, i)
del self._pos[i]
else:
if item.find(search)<0:
Listbox.delete(self, i)
del self._pos[i]
Listbox.selection_clear(self, 0, END)
Listbox.selection_set(self, activepos)
Listbox.activate(self, activepos)
# ----------------------------------------------------------------------
def insert(self, index, *elements):
del self._items[:]
return Listbox.insert(self, index, *elements)
# ----------------------------------------------------------------------
def delete(self, first, last=None):
del self._items[:]
return Listbox.delete(self, first, last)
# ----------------------------------------------------------------------
def curselection(self):
if self._items:
return [self._pos[int(x)] for x in Listbox.curselection(self)]
else:
return Listbox.curselection(self)
# ----------------------------------------------------------------------
# FIXME needs work to handle, ACTIVE, END...
# ----------------------------------------------------------------------
def get(self, first, last=None):
#say("SearchListbox.get",first,type(first),last,type(last))
if not self._items:
return Listbox.get(self, first, last)
elif first == ACTIVE:
return Listbox.get(self, first, last)
elif last is None:
return self._items[first]
elif last == END:
last = len(self._items)
else:
last = int(last)+1
if len(self._items)==0: return ""
return self._items[int(first):last]
#===============================================================================
# MultiListbox based on recipe from
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52266
# Author: Brent Burley
# Date: 2001/03/14
#===============================================================================
class MultiListbox(Frame):
"""Multilistbox class"""
# Add default options if not supplied
defopt = (("borderwidth", 0),
("selectmode", EXTENDED),
("selectborderwidth", 0),
("relief", FLAT),
("exportselection", FALSE),
("takefocus", FALSE))
def __init__(self, master, lists, **options):
Frame.__init__(self, master)
self.paneframe = PanedWindow(self, orient=HORIZONTAL,
showhandle=0, handlepad=0, handlesize=0,
sashwidth=2, opaqueresize=1)
self.paneframe.pack(side=LEFT, expand=YES, fill=BOTH)
self.paneframe.bind("<Button-1>", self._sashMark)
self.paneframe.bind("<B1-Motion>", self._sashDrag)
self.paneframe.bind("<ButtonRelease-1>", self._sashRelease)
self._lists = []
self._labels = []
col = 0
if "header" in options:
header = options["header"]
del options["header"]
else:
header = 1
if "stretch" in options:
stretch = options["stretch"]
del options["stretch"]
else:
stretch = "always"
for n,o in MultiListbox.defopt:
if n not in options:
options[n] = o
for l, w, a in lists:
#if header:
frame = Frame(self.paneframe, border=0)
try: self.paneframe.add(frame, minsize=16, stretch=stretch)
except: self.paneframe.add(frame, minsize=16) # tk8.4
if header:
lbl = Label(frame, text=l, borderwidth=1,
relief=RAISED)
lbl.pack(fill=X)
lbl.bind('<Button-1>', lambda e, s=self, c=col:
s.sort(c))
self._labels.append(lbl)
#else:
# frame = self
lb = ExListbox(frame, width=w, **options)
#if header:
# lb.pack(expand=YES, fill=BOTH)
#else:
lb.pack(side=LEFT, expand=YES, fill=BOTH)
self._lists.append(lb)
lb.bind('<B2-Motion>', lambda e, s=self:
s._b2motion(e.x, e.y))
lb.bind('<Button-2>', lambda e, s=self:
s._button2(e.x, e.y))
lb.bind('<Button-4>', lambda e, s=self:
s._scroll(SCROLL, -1, UNITS))
lb.bind('<Button-5>', lambda e, s=self:
s._scroll(SCROLL, 1, UNITS))
lb.bind('<<ListboxSelect>>', lambda e, s=self, l=lb:
s._updateSelect(l))
col += 1
self._lists[0]["takefocus"] = True
if header:
frame = Frame(self)
frame.pack(side=RIGHT, fill=Y)
Label(frame, borderwidth=1, relief=RAISED).pack(fill=X)
self.scrollbar = Scrollbar(frame, orient=VERTICAL,
takefocus=False,
command=self._scroll)
if header:
self.scrollbar.pack(fill=Y, expand=YES)
else:
self.scrollbar.pack(side=RIGHT, fill=Y)
self._lists[0]['yscrollcommand']=self.scrollbar.set
self.activeList = self._lists[0]
self.sortAssist = SortAssist
self._sortOrder = None # Array containing the previous order of the list after sort
self._sortColumn = -1
self._sashIndex = -1
self._sortReverse = False
self._sashx = None
# ----------------------------------------------------------------------
# Bind left/right arrow to focusing different list
# ----------------------------------------------------------------------
def bindLeftRight(self):
""" Default bindings of left/right arrow to focusing different lists"""
self.bindList('<Left>', self.focusLeft)
self.bindList('<Right>', self.focusRight)
# ----------------------------------------------------------------------
def _updateSelect(self, lst=None):
if lst is None: lst = self.activeList
ypos = lst.yview()[0]
sel = lst.curselection()
act = lst.index(ACTIVE)
for l in self._lists:
if l is lst: continue
l.selection_clear(0, END)
for s in sel:
l.selection_set(s)
l.activate(act)
l.yview_moveto(ypos)
self.event_generate('<<ListboxSelect>>')
# ----------------------------------------------------------------------
# Update header labels
# ----------------------------------------------------------------------
def labels(self, names):
for i,n in enumerate(names):
if i>len(self._labels): return
self._labels[i].config(text=n)
# ----------------------------------------------------------------------
def _button2(self, x, y):
for l in self._lists:
l.scan_mark(x, y)
return 'break'
# ----------------------------------------------------------------------
def _b2motion(self, x, y):
for l in self._lists:
l.scan_dragto(x, y)
return 'break'
# ----------------------------------------------------------------------
def _sashMark(self, event):
self._sashIndex = -1
try:
self._sashIndex,which = self.paneframe.identify(event.x, event.y)
if which == "sash":
self._sashx = [self.paneframe.sash_coord(i)[0] \
for i in range(len(self._lists)-1)]
self._sashdx = self._sashx[self._sashIndex] - event.x
self._sashDrag(event)
else:
self._sashIndex = -1
except:
return
return 'break'
# ----------------------------------------------------------------------
def _sashDrag(self, event):
if self._sashx and self._sashIndex >= 0:
ddx = event.x - self._sashdx - self._sashx[self._sashIndex]
self.paneframe.sash_place(self._sashIndex, event.x-self._sashdx, 1)
for i in range(self._sashIndex+1, len(self._lists)-1):
self.paneframe.sash_place(i, self._sashx[i]+ddx, 1)
return 'break'
# ----------------------------------------------------------------------
def _sashRelease(self, event):
if self._sashIndex >= 0:
self._sashx = None
return 'break'
# ----------------------------------------------------------------------
def _scroll(self, *args):
for l in self._lists:
l.yview(*args)
return 'break'
# ----------------------------------------------------------------------
def curselection(self):
return self._lists[0].curselection()
# ----------------------------------------------------------------------
def delete(self, first, last=None):
for l in self._lists:
l.delete(first, last)
# ----------------------------------------------------------------------
def get(self, first, last=None):
result = []
for l in self._lists:
result.append(l.get(first, last))
if last:
return to_zip(*result)
return result
# ----------------------------------------------------------------------
def listbox(self, i):
return self._lists[i]
# ----------------------------------------------------------------------
def listboxes(self):
return self._lists
# ----------------------------------------------------------------------
def listboxIndex(self, widget):
return self._lists.index(widget)
# ----------------------------------------------------------------------
def bindList(self, event, func):
self.bind(event, func)
for l in self._lists:
l.bind(event, func)
# ----------------------------------------------------------------------
def unbindList(self, event):
self.unbind(event)
for l in self._lists:
l.bind(event)
# ----------------------------------------------------------------------
def index(self, item):
return self._lists[0].index(item)
# ----------------------------------------------------------------------
def insert(self, index, *elements):
for e in elements:
for i,l in enumerate(self._lists):
l.insert(index, e[i])
if len(e) < len(self._lists):
for l in self._lists[len(e) : len(self._lists)]:
l.insert(index, "")
if self._sortColumn>=0:
txt = self._labels[self._sortColumn]["text"]
self._labels[self._sortColumn].config(text=txt[:-1])
self._sortColumn = -1
# ----------------------------------------------------------------------
# Change the value of a list item
# and return the value of the old one
# ----------------------------------------------------------------------
def set(self, index, value):
"""Set the value of a list item."""
self.delete(index)
self.insert(index, value)
# ----------------------------------------------------------------------
def size(self):
return self._lists[0].size()
# ----------------------------------------------------------------------
def setPopupMenu(self, menu):
"""Setup a popup menu list it should be in the form
[ (label, underline, command), ... ]"""
for l in self._lists:
l.setPopupMenu(menu)
# ----------------------------------------------------------------------
def nearest(self, y):
return self._lists[0].nearest(y)
# ----------------------------------------------------------------------
def see(self, index):
for l in self._lists:
l.see(index)
# ----------------------------------------------------------------------
def configure(self, **kw):
for l in self._lists:
l.configure(**kw)
config = configure
# ----------------------------------------------------------------------
def itemcget(self, index, option):
"""Return the resource value for an ITEM and an OPTION."""
return self._lists[0].itemcget(index, option)
# ----------------------------------------------------------------------
def itemconfigure(self, index, cnf=None, **kw):
"""Configure resources of an ITEM.
The values for resources are specified as keyword arguments.
To get an overview about the allowed keyword arguments
call the method without arguments.
Valid resource names: background, bg, foreground, fg,
selectbackground, selectforeground."""
for l in self._lists:
l.itemconfigure(index, cnf, **kw)
itemconfig = itemconfigure
# ----------------------------------------------------------------------
# Override of the standard Tkinter cget() routine
# ----------------------------------------------------------------------
def __getitem__(self, key):
return self._lists[0].cget(key)
# ----------------------------------------------------------------------
# Override of the standard Tkinter config() routine
# ----------------------------------------------------------------------
def __setitem__(self, key, value):
for l in self._lists:
l[key] = value
# ----------------------------------------------------------------------
# Selection
# ----------------------------------------------------------------------
def selection_anchor(self, index):
for l in self._lists:
l.selection_anchor(index)
# ----------------------------------------------------------------------
def selection_includes(self, index):
return self._lists[0].selection_includes(index)
# ----------------------------------------------------------------------
def selection_clear(self, first, last=None):
for l in self._lists:
l.selection_clear(first, last)
# ----------------------------------------------------------------------
def selection_set(self, first, last=None):
for l in self._lists:
l.selection_set(first, last)
# ----------------------------------------------------------------------
def selectAll(self, event=None):
"""Select all items"""
self.selection_set(0, END)
self.event_generate("<<ListboxSelect>>")
return "break"
# ----------------------------------------------------------------------
def selectClear(self, event=None):
"""Unselect all items"""
self.selection_clear(0, END)
self.event_generate("<<ListboxSelect>>")
return "break"
# ----------------------------------------------------------------------
def selectInvert(self, event=None):
"""Invert selection"""
l = self._lists[0]
for i in range(l.size()):
if l.select_includes(i):
self.selection_clear(i)
else:
self.selection_set(i)
self.event_generate("<<ListboxSelect>>")
return "break"
# ----------------------------------------------------------------------
def activate(self, index):
for l in self._lists:
l.activate(index)
# ----------------------------------------------------------------------
def focus_set(self):
self._lists[0].focus_set()
# ----------------------------------------------------------------------
def focusLeft(self, event=None):
listbox = self.focus_get()
if listbox is None: return
active = listbox.index(ACTIVE)
try:
lid = self._lists.index(listbox) - 1
if lid>=0:
self._lists[lid].activate(active)
self._lists[lid].focus_set()
except:
pass
# ----------------------------------------------------------------------
def focusRight(self, event=None):
listbox = self.focus_get()
if listbox is None: return
active = listbox.index(ACTIVE)
try:
lid = self._lists.index(listbox) + 1
if lid < len(self._lists):
self._lists[lid].activate(active)
self._lists[lid].focus_set()
except:
pass
# ----------------------------------------------------------------------
def sort(self, column, reverse=None):
""" Sort by a given column."""
if self._lists[0].cget("state") == DISABLED: return
if self.sortAssist is None: return
if column == self._sortColumn:
txt = self._labels[self._sortColumn]["text"][:-1]
if reverse is None:
reverse = not self._sortReverse
else:
if self._sortColumn>=0:
txt = self._labels[self._sortColumn]["text"][:-1]
self._labels[self._sortColumn].config(text=txt)
self._sortColumn = -1
txt = self._labels[column]["text"]
if reverse is None:
reverse = False
#elements = self.get(0, END)
elements = []
lst = self._lists[0]
for i in range(self.size()):
item = []
for l in self._lists:
item.append(l.get(i))
item.append(lst.selection_includes(i)) # Include selection
item.append(i) # Include position
elements.append(item)
try: active = int(self.index(ACTIVE))
except: active = -1
self.delete(0, END)
elements.sort(key=self.sortAssist(column), reverse=reverse)
# get selection status
status = []
self._sortOrder = []
newactive = -1
for i,item in enumerate(elements):
idx = item.pop()
if active == idx: newactive = i
self._sortOrder.append(idx)
status.append(item.pop())
self.insert(END, *elements)
for i,s in enumerate(status):
if s:
self.selection_set(i)
if newactive<0: newactive = i
if newactive>=0:
self.activate(newactive)
self._sortColumn = column
self._sortReverse = reverse
if reverse:
self._labels[column].config(text=txt+Unicode.BLACK_DOWN_POINTING_TRIANGLE)
else:
self._labels[column].config(text=txt+Unicode.BLACK_UP_POINTING_TRIANGLE)
self.event_generate("<<ListboxSort>>")
# ----------------------------------------------------------------------
def saveSort(self):
return self._sortColumn, self._sortReverse
# ----------------------------------------------------------------------
def restoreSort(self, arg):
if arg[0] >= 0:
self.sort(*arg)
# ----------------------------------------------------------------------
def yview(self):
return self._lists[0].yview()
# ----------------------------------------------------------------------
def yview_moveto(self, fraction):
for l in self._lists:
l.yview_moveto(fraction)
# ----------------------------------------------------------------------
def moveUp(self):
for l in self._lists:
l.moveUp()
# ----------------------------------------------------------------------
def moveDown(self):
for l in self._lists:
l.moveDown()
#===============================================================================
# A MultiListbox that remembers the color of items
#===============================================================================
class ColorMultiListbox(MultiListbox):
# ----------------------------------------------------------------------
def sort(self, column, dir=None):
# remember colors
colors = {}
for i in range(self.size()):
colors[self._lists[0].get(i)] = \
self._lists[0].itemcget(i, "foreground")
MultiListbox.sort(self, column, dir)
# set colors
for i in range(self.size()):
self.setColor(i, colors[self._lists[0].get(i)])
del colors
# ----------------------------------------------------------------------
def setColor(self, idx, color):
for l in self._lists:
l.itemconfigure(idx, foreground=color)
#===============================================================================
# Image list
#===============================================================================
class ImageListbox(Text):
"""ImageListbox widget which can display a list of strings and images"""
def __init__(self, master, **options):
Text.__init__(self, master, **options)
self.config(cursor="arrow",
tabs="20p",
#insertofftime=0,
#insertontime=0,
wrap=NONE,
insertwidth=0,
takefocus=TRUE,
exportselection=0)
# state=DISABLED)
self.bind("<Button-1>", self._button1)
self.bind("<Control-Button-1>", self._controlButton1)
self.bind("<Shift-Button-1>", self._motion1)
self.bind("<B1-Motion>", self._motion1)
self.bind("<Control-B1-Motion>",self._controlMotion1)
self.bind("<Key>", self._key)
self.bind("<Delete>", self._break)
self.bind("<Return>", self._break)
self.bind("<KeyRelease>", self._break)
self.bind("<<Cut>>", self.cut)
self.bind("<<Copy>>", self.copy)
self.bind("<<Paste>>", self.paste)
self._selection = []
self._anchor = 0
self._active = 0
# ----------------------------------------------------------------------
def insert(self, index, icon, text):
"""Insert ELEMENTS at INDEX."""
# self.config(state=NORMAL)
if index != END:
index = int(index)
sindex = "%d.0"%(index+1)
Text.insert(self, sindex, "\t%s\n"%(text))
self.image_create(sindex, image=icon)
self._selection.insert(index,False)
else:
self.image_create(END, image=icon)
Text.insert(self, END, "\t%s\n"%(text))
self._selection.append(False)
# self.config(state=DISABLED)
# ----------------------------------------------------------------------
def delete(self, first, last=None):
"""Delete items from FIRST to LAST (not included)."""
if first == END:
Text.delete(self, "end.0", END)
self._selection.pop()
return "break"
if first == ACTIVE:
first = self.index(ACTIVE)
if last is None:
i = int(first)
if 0 <= i < len(self._selection):
Text.delete(self, "%d.0"%(i+1), "%d.0 + 1 lines"%(i+1))
del self._selection[i]
return "break"
if last == END:
last = self.size()
first = int(first)
lines = int(last) - first
Text.delete(self, "%d.0"%(first+1), "%d.0 + %d lines"%(first+1, lines))
try:
del self._selection[first:last]
except IndexError:
pass
return "break"
# ----------------------------------------------------------------------
def size(self):
"""Return the number of elements in the listbox."""
return len(self._selection)
# ----------------------------------------------------------------------
def curselection(self):
"""Return list of indices of currently selected item."""
sel = []
for i,x in enumerate(self._selection):
if x: sel.append(i)
return sel
# ----------------------------------------------------------------------
def nearest(self, y):
"""Get index of item which is nearest to y coordinate Y."""
index = Text.index(self,"@1,%d"%(y))
i = int(index.split(".")[0])-1
if i>= self.size(): i -= 1
return i
# ----------------------------------------------------------------------
def _button1(self, event):
self.focus()
self.selection_anchor(self.nearest(event.y))
self._active = self._anchor
self._select()
return "break"
# ----------------------------------------------------------------------
def _motion1(self, event):
y = self.nearest(event.y)
if self._active != y:
self._active = y
self._selectRange()
return "break"
# ----------------------------------------------------------------------
def _controlButton1(self, event):
self.selection_anchor(self.nearest(event.y))
self._active = self._anchor
self._selection[self._anchor] = not self._selection[self._anchor]
self._tagSelection()
self.event_generate("<<ListboxSelect>>")
return "break"
# ----------------------------------------------------------------------
def _controlMotion1(self, event):
self._active = self.nearest(event.y)
last = self._selection[self._anchor]
if self._active < self._anchor:
for i in range(self._active, self._anchor):
self._selection[i] = last
elif self._active > self._anchor:
for i in range(self._anchor, self._active+1):
self._selection[i] = last
self._tagSelection()
return "break"
# ----------------------------------------------------------------------
def _key(self, event):
if event.keysym == "Up":
if self._active == 0: return "break"
self._active -= 1
if event.state & SHIFT_MASK:
self._selectRange()
else:
self._anchor = self._active
self._select()
elif event.keysym == "Down":
self._active += 1
if self._active >= self.size():
self._active = self.size()-1
return "break"
if event.state & SHIFT_MASK:
self._selectRange()
else:
self._anchor = self._active
self._select()
elif event.keysym in ("Prior", "Next", "Delete"):
return
if event.state & CONTROL_MASK != 0:
# Let system handle all Control keys
pass
else:
# Ignore all normal keys
return "break"
# ----------------------------------------------------------------------
def _break(self, event):
return "break"
# ----------------------------------------------------------------------
def _select(self):
self._selection = [False] * len(self._selection)
self.selection_set(self._active)
idx = "%d.0"%(self._active+1)
Text.see(self, idx)
Text.index(self, idx)
self.event_generate("<<ListboxSelect>>")
# ----------------------------------------------------------------------
def _selectRange(self):
self._selection = [False] * len(self._selection)
if self._active < self._anchor:
for i in range(self._active, self._anchor):
self._selection[i] = True
elif self._active > self._anchor:
for i in range(self._anchor, self._active+1):
self._selection[i] = True
try:
self._selection[self._anchor] = True
except IndexError:
pass
self._tagSelection()
self.event_generate("<<ListboxSelect>>")
return "break"
# ----------------------------------------------------------------------
def selection_anchor(self, index):
"""Set the fixed end oft the selection to INDEX."""
self._anchor = index
select_anchor = selection_anchor
# ----------------------------------------------------------------------
def selection_clear(self, first, last=None):
"""Clear the selection from FIRST to LAST (not included)."""
self._selection = [False] * len(self._selection)
self._tagSelection()
select_clear = selection_clear
# ----------------------------------------------------------------------
def selection_includes(self, index):
"""Return 1 if INDEX is part of the selection."""
return self._selection[index]
select_includes = selection_includes
# ----------------------------------------------------------------------
def selection_set(self, first, last=None):
"""Set the selection from FIRST to LAST (not included) without
changing the currently selected elements."""
if first == END:
self._selection[-1] = True
self._tagSelection()
return
if last is None:
i = int(first)
if 0 <= i < len(self._selection):
self._selection[int(first)] = True
self._tagSelection()
return
if last == END:
last = self.size()
for i in range(int(first), last):
self._selection[i] = True
self._tagSelection()
select_set = selection_set
# ----------------------------------------------------------------------
def see(self, index):
"""Scroll such that INDEX is visible."""
if index == END:
Text.see(self, index)
else:
Text.see(self, "%d.0"%(int(index)+1))
# ----------------------------------------------------------------------
def _tagSelection(self):
self.tag_delete("lola")
for i,x in enumerate(self._selection):
if x:
self.tag_add("lola", "%d.0"%(i+1), "%d.0 +1 lines"%(i+1))
self.tag_configure("lola", foreground="White", background="SteelBlue2")
Text.selection_clear(self)
# ----------------------------------------------------------------------
#def bbox(self, *args):
def bbox(self, index):
"""Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
which encloses the item identified by index in ARGS."""
if index == END:
return Text.dlineinfo(self,index)[:4]
if index == ACTIVE:
index = self.index(index)
return Text.bbox(self,"%d.2"%(int(index)+1))
# ----------------------------------------------------------------------
def dlineinfo(self,index):
if index == END:
return Text.dlineinfo(self,index)
if index == ACTIVE:
index = self.index(index)
return Text.dlineinfo(self,"%d.0"%(int(index)+1))
# ----------------------------------------------------------------------
def activate(self, index):
"""Activate item identified by INDEX."""
if index == END:
self._active = self.size()-1
else:
self._active = int(index)
# ----------------------------------------------------------------------
def get(self, first, last=None):
"""Get list of items from FIRST to LAST (not included)."""
if first == END:
first = self.size()-1
elif first == ACTIVE:
first = self._active
else:
first = int(first)
if last is None:
if 0 <= first < len(self._selection):
first += 1
img = Text.image_cget(self, "%d.0"%(first), "image")
txt = Text.get(self, "%d.2"%(first), "%d.end"%(first))
return txt
return None
if last == END:
last = self.size()
elif last == ACTIVE:
last = self._active
else:
last = int(last)
# FIXME....
# return list of text items
# ----------------------------------------------------------------------
# get both image and text
# ----------------------------------------------------------------------
def elicit(self, first, last=None):
"""Get list of items from FIRST to LAST (not included)."""
if first == END:
first = self.size()-1
elif first == ACTIVE:
first = self._active
else:
first = int(first)
if last is None:
if 0 <= first < len(self._selection):
first += 1
img = Text.image_cget(self, "%d.0"%(first), "image")
txt = Text.get(self, "%d.2"%(first), "%d.end"%(first))
return img,txt
return None,None
if last == END:
last = self.size()
elif last == ACTIVE:
last = self._active
else:
last = int(last)
# FIXME....
# return list of (image,text) items
# ----------------------------------------------------------------------
def index(self, index):
"""Return index of item identified with INDEX."""
if index == ACTIVE:
return self._active
else:
return Text.index(self,index)
# ----------------------------------------------------------------------
def scan_mark(self, x, y):
"""Remember the current X, Y coordinates."""
pass
# ----------------------------------------------------------------------
def scan_dragto(self, x, y):
"""Adjust the view of the listbox to 10 times the
difference between X and Y and the coordinates given in
scan_mark."""
pass
# ----------------------------------------------------------------------
def itemcget(self, index, option):
"""Return the resource value for an ITEM and an OPTION."""
pass
# ----------------------------------------------------------------------
def itemconfigure(self, index, cnf=None, **kw):
"""Configure resources of an ITEM.
The values for resources are specified as keyword arguments.
To get an overview about the allowed keyword arguments
call the method without arguments.
Valid resource names: background, bg, foreground, fg,
selectbackground, selectforeground."""
pass
itemconfig = itemconfigure
# ----------------------------------------------------------------------
# Override cut,copy,paste to do nothing
# ----------------------------------------------------------------------
def cut(self, event=None):
return "break"
copy = cut
paste = cut
#===============================================================================
# Class to edit in place the contents of a listbox
#===============================================================================
class InPlaceEdit:
def __init__(self, listbox, item=ACTIVE, value=None, x=None, select=True, **kw):
# Return value
self.value = None # Result
self.frame = None
self.lastkey = None # Last key that exited the editbox
self.kw = kw
self._x = x
self._select = select
# Find active
try: self.active = listbox.index(item)
except: return
self.item = item
self.listbox = listbox
# Create and set value
self.frame = Frame(listbox, relief=None)
self.createWidget()
self.old = self.set(value)
self.defaultBinds()
# Bindings
self.frame.bind("<FocusOut>", self.focusOut)
# Unmap creates core dump when Fn key is pressed
self.frame.bind("<ButtonRelease-1>", self.clickOk)
self.frame.bind("<ButtonRelease-3>", self.clickCancel)
self.listbox.bind("<Configure>", self.resize)
#self.frame.bind("<Unmap>", self._destroy)
try:
self._grab_window = self.frame.grab_current()
except TclError:
self._grab_window = None
self.resize()
self.show()
# ----------------------------------------------------------------------
def show(self):
# Show and wait to be destroyed
try:
self.frame.wait_visibility()
self.frame.grab_set()
self.icursor()
self.frame.wait_window()
except TclError:
pass
#self.listbox.focus_set()
# ----------------------------------------------------------------------
# Override method if another widget is requested
# ----------------------------------------------------------------------
def createWidget(self):
self.edit = Entry(self.frame, **self.kw)
self.edit.pack(expand=YES, fill=BOTH)
self.edit.focus_set()
# ----------------------------------------------------------------------
# set insert cursor at location
# ----------------------------------------------------------------------
def icursor(self):
if self._x is not None:
self.edit.icursor("@%d"%(self._x))
# ----------------------------------------------------------------------
# Set default bindings
# ----------------------------------------------------------------------
def defaultBinds(self):
try:
self.edit.bind("<Return>", self.ok)
self.edit.bind("<KP_Enter>", self.ok)
self.edit.bind("<Up>", self.ok)
self.edit.bind("<Down>", self.ok)
self.edit.bind("<Escape>", self.cancel)
except AttributeError:
pass
# ----------------------------------------------------------------------
def resize(self, event=None):
if self.frame is None: return
bbox = self.listbox.bbox(self.item)
if bbox is None: return
x, y, w, h = bbox
w = self.listbox.winfo_width() - x
h += 3
try:
self.frame.place(in_=self.listbox,
x=x-1, y=y-1,
width=w, height=h,
bordermode=OUTSIDE)
self.frame.update_idletasks()
except TclError:
pass
# ----------------------------------------------------------------------
# Override method to set the value
# ----------------------------------------------------------------------
def set(self, value):
if self.frame is None: return
if value is None:
value = self.listbox.get(self.item)
self.edit.delete(0, END)
self.edit.insert(0, value)
if self._select:
self.edit.selection_range(0, END)
return value
# ----------------------------------------------------------------------
# Override method to get the value
# ----------------------------------------------------------------------
def get(self):
if self.frame is None: return None
return self.edit.get()
# ----------------------------------------------------------------------
def reset_grab(self):
if self.frame is None: return
self.frame.grab_release()
if self._grab_window is not None:
try:
self._grab_window.grab_set()
except TclError:
pass
# ----------------------------------------------------------------------
def clickOk(self, event):
# If clicked outside return ok
if event.x < 0 or \
event.y < 0 or \
event.x > self.frame.winfo_width() or \
event.y > self.frame.winfo_height():
self.ok(event)
# ----------------------------------------------------------------------
def clickCancel(self, event):
# If clicked outside return cancel
if event.x < 0 or \
event.y < 0 or \
event.x > self.frame.winfo_width() or \
event.y > self.frame.winfo_height():
self.cancel(event)
# ----------------------------------------------------------------------
def focusOut(self, event=None):
self.ok()
# ----------------------------------------------------------------------
def updateValue(self):
if isinstance(self.listbox, Listbox):
self.listbox.delete(self.active)
self.listbox.insert(self.active, self.value)
# ----------------------------------------------------------------------
def ok(self, event=None):
if event: self.lastkey = event.keysym
self.value = self.get()
self.frame.unbind('<FocusOut>')
act = self.listbox.index(ACTIVE)
sel = self.listbox.selection_includes(self.active)
self.updateValue()
self.listbox.see(self.active)
if sel:
self.listbox.selection_set(self.active)
self.listbox.activate(act)
if self.value == self.old: self.value = None
self.reset_grab()
self.listbox.focus_set()
self.frame.place_forget()
self.frame.destroy()
return "break"
# ----------------------------------------------------------------------
def cancel(self, event=None):
self.reset_grab()
self.listbox.focus_set()
self.frame.place_forget()
self.frame.destroy()
return "break"
#===============================================================================
class InPlaceSpinbox(InPlaceEdit):
# ----------------------------------------------------------------------
def createWidget(self):
self.edit = Spinbox(self.frame, **self.kw)
self.edit.pack(expand=YES, fill=BOTH)
self.edit.focus_set()
# ----------------------------------------------------------------------
def set(self, value):
if self.frame is None: return
if value is None:
value = self.listbox.get(self.item)
self.edit.delete(0, END)
self.edit.insert(0, value)
return value
#===============================================================================
class InPlaceInteger(InPlaceEdit):
# ----------------------------------------------------------------------
def createWidget(self):
self.edit = IntegerEntry(self.frame, **self.kw)
self.edit.pack(expand=YES, fill=BOTH)
self.edit.focus_set()
#===============================================================================
class InPlaceFloat(InPlaceEdit):
# ----------------------------------------------------------------------
def createWidget(self):
self.edit = FloatEntry(self.frame, **self.kw)
self.edit.pack(expand=YES, fill=BOTH)
self.edit.focus_set()
#===============================================================================
class InPlaceList(InPlaceEdit):
def __init__(self, listbox, item=ACTIVE, value=None, height=None, values=[], **kw):
self.values = values
self.height = height
InPlaceEdit.__init__(self, listbox, item, value, **kw)
# ----------------------------------------------------------------------
def createWidget(self):
self.frame.config(relief=RAISED)
sb = Scrollbar(self.frame)
sb.pack(side=RIGHT, fill=Y)
if self.height is None:
if len(self.values)<10:
self.height = max(len(self.values)+1,3)
else:
self.height = 10
self.edit = ExListbox(self.frame,
selectmode=BROWSE,
height=self.height,
#background="White",
yscrollcommand=sb.set)
sb.config(command=self.edit.yview)
self.edit.pack(side=LEFT, fill=BOTH, expand=YES)
self.edit.bind('<ButtonRelease-1>', self.ok)
self.edit.focus_set()
# ----------------------------------------------------------------------
def set(self, value):
if value is None:
value = self.listbox.get(self.item)
# Fill&select listbox
for item in self.values:
self.edit.insert(END, item)
if item == value:
self.edit.activate(END)
self.edit.selection_set(END)
if len(self.edit.curselection()) == 0:
self.edit.activate(0)
self.edit.see(ACTIVE)
return value
# ----------------------------------------------------------------------
def get(self):
cur = self.edit.curselection()
if len(cur)>0:
return self.edit.get(cur[0])
else:
return ""
# ----------------------------------------------------------------------
def defaultBinds(self):
InPlaceEdit.defaultBinds(self)
try:
self.edit.unbind("<Up>")
self.edit.unbind("<Down>")
except AttributeError:
pass
# ----------------------------------------------------------------------
def resize(self, event=None):
if self.frame is None: return
bbox = self.listbox.bbox(self.item)
if bbox is None: return
x, y, item_width, item_height = bbox
list_width = self.listbox.winfo_width()
list_height = self.listbox.winfo_height()
h = item_height*self.height + 2
if y+h > list_height:
y = list_height - h
if y <= 0:
y = 0
h = list_height
try:
self.frame.place(in_=self.listbox,
x=x-1, y=y,
width=list_width, height=h,
bordermode=OUTSIDE)
self.frame.update_idletasks()
except TclError:
pass
#===============================================================================
class InPlaceColor(InPlaceEdit):
# ----------------------------------------------------------------------
def createWidget(self):
b = Button(self.frame, text="x",
padx=0, pady=0, command=self.clearColor)
b.pack(side=LEFT)
self.edit = Button(self.frame, command=self.selectColor)
self.edit.pack(side=RIGHT, expand=YES, fill=BOTH)
self.edit.focus_set()
# ----------------------------------------------------------------------
def set(self, value):
if value is None:
value = self.listbox.get(self.item)
self.value = value
if self.value is None or self.value=="": self.value = "White"
if value != "":
self.edit.config(text=value,
background=value,
activebackground=value)
return value
# ----------------------------------------------------------------------
def get(self):
return self.edit["text"]
# ----------------------------------------------------------------------
def selectColor(self):
self.frame.unbind("<FocusOut>")
try:
rgb, colorStr = askcolor(
title="Color",
initialcolor=self.value,
parent=self.listbox.master)
except TclError:
colorStr = None
if colorStr is not None:
colorStr = str(colorStr)
self.value = colorStr
self.edit.config(text=colorStr,
background=colorStr,
activebackground=colorStr)
self.frame.bind("<FocusOut>", self.cancel)
self.edit.focus_set()
# ----------------------------------------------------------------------
def clearColor(self):
self.frame.unbind("<FocusOut>")
self.value = None
self.edit.config(text="",
background="White",
activebackground="White")
self.frame.bind("<FocusOut>", self.cancel)
self.edit.focus_set()
#===============================================================================
class InPlaceMaxLength(InPlaceEdit):
def __init__(self, listbox, item=ACTIVE, value=None, maxlength=None, **kw):
self.maxlength = maxlength
InPlaceEdit.__init__(self, listbox, item, value, **kw)
# ----------------------------------------------------------------------
# Override method if another widget is requested
# ----------------------------------------------------------------------
def createWidget(self):
self.edit = MaxLengthEntry(self.frame,
maxlength=self.maxlength,
**self.kw)
self.edit.pack(expand=YES, fill=BOTH)
self.edit.focus_set()
#===============================================================================
class InPlaceText(InPlaceEdit):
# ----------------------------------------------------------------------
def show(self):
self.toplevel.bind("<FocusOut>", self.focusOut)
try:
self.toplevel.wait_visibility()
self.toplevel.grab_set()
self.toplevel.wait_window()
except TclError:
pass
# ----------------------------------------------------------------------
def defaultBinds(self):
InPlaceEdit.defaultBinds(self)
self.toplevel.bind("<ButtonRelease-1>", self.clickOk)
self.toplevel.bind("<ButtonRelease-3>", self.clickCancel)
#self.edit.bind("<ButtonRelease-1>", self.clickOk)
#self.edit.bind("<ButtonRelease-3>", self.clickCancel)
self.edit.bind("<Shift-Return>", self.shiftReturn)
self.edit.bind("<Escape>", self.cancel)
# ----------------------------------------------------------------------
def createWidget(self):
global GLOBAL_CONTROL_BACKGROUND
self.toplevel = Toplevel(self.listbox)
self.toplevel.transient(self.listbox)
if sys.platform in ("win32", "win64"):
self.toplevel.update_idletasks()
self.toplevel.overrideredirect(1)
self.edit = Text(self.toplevel, width=70, height=10,
background=GLOBAL_CONTROL_BACKGROUND,
undo=True)
self.edit.pack(side=LEFT, expand=YES, fill=BOTH)
self.edit.focus_set()
# ----------------------------------------------------------------------
def resize(self, event=None):
if self.frame is None: return
bbox = self.listbox.bbox(self.item)
if bbox is None: return
x, y, w, h = bbox
x += self.listbox.winfo_rootx()
y += self.listbox.winfo_rooty()
w = self.listbox.winfo_width()
try:
self.toplevel.wm_geometry("+%d+%d" % (x,y))
except TclError:
pass
# ----------------------------------------------------------------------
def set(self, value):
if self.frame is None: return
if value is None:
value = self.listbox.get(self.item)
self.edit.delete("0.0", END)
self.edit.insert("0.0", value)
self.edit.tag_add(SEL, "0.0", END)
return value
# ----------------------------------------------------------------------
def get(self):
if self.frame is None: return None
return self.edit.get("0.0", END).strip()
# ----------------------------------------------------------------------
def shiftReturn(self, event):
# Empty binding to avoid the Shift-Return to trigger the "ok"
pass
# ----------------------------------------------------------------------
def clickOk(self, event):
# If clicked outside return ok
if event.x < 0 or \
event.y < 0 or \
event.x > self.toplevel.winfo_width() or \
event.y > self.toplevel.winfo_height():
self.ok(event)
# ----------------------------------------------------------------------
def clickCancel(self, event):
# If clicked outside return cancel
if event.x < 0 or \
event.y < 0 or \
event.x > self.toplevel.winfo_width() or \
event.y > self.toplevel.winfo_height():
self.cancel(event)
# ----------------------------------------------------------------------
def ok(self, event=None):
InPlaceEdit.ok(self, event)
self.toplevel.destroy()
return "break"
# ----------------------------------------------------------------------
def cancel(self, event=None):
InPlaceEdit.cancel(self, event)
self.toplevel.destroy()
return "break"
#===============================================================================
class InPlaceFile(InPlaceEdit):
# ----------------------------------------------------------------------
def __init__(self, listbox, item=ACTIVE, value=None,
title=None, filetypes=None,
save=True, **kw):
self.title = title
self.filetypes = filetypes
self._save = save
self._icon = PhotoImage(data=_SAVEICON)
InPlaceEdit.__init__(self, listbox, item, value, **kw)
# ----------------------------------------------------------------------
def createWidget(self):
self.edit = Entry(self.frame, width=5, **self.kw)
self.edit.pack(side=LEFT, expand=YES, fill=BOTH)
b = Button(self.frame, image=self._icon,
padx=0, pady=0, command=self.fileDialog)
b.pack(side=RIGHT)
self.edit.focus_set()
# ----------------------------------------------------------------------
def fileDialog(self):
self.frame.unbind("<FocusOut>")
self.frame.grab_release()
if self.value is None:
filename = self.listbox.get(self.item)
else:
filename = self.value
if self._save:
fn = bFileDialog.asksaveasfilename(master=self.listbox,
title=self.title,
initialfile=filename,
filetypes=self.filetypes)
else:
fn = bFileDialog.askopenfilename(master=self.listbox,
title=self.title,
initialfile=filename,
filetypes=self.filetypes)
self.frame.grab_set()
#self.frame.bind("<FocusOut>", self.cancel)
self._icon = None
if len(fn) > 0:
self.edit.delete(0, END)
self.edit.insert(0, fn)
self.ok()
else:
self.cancel()
#=============================================================================
# PopupList
# Show a popup list on a top level and return selected item
#=============================================================================
class PopupList(Toplevel):
def __init__(self, master, items=None, selected=None, **kw):
Toplevel.__init__(self, master, **kw)
self.selected = selected
self.overrideredirect(1)
self.transient(master)
# Create the listbox inside the dropdown window
sb = Scrollbar(self)
sb.pack(side=RIGHT, fill=Y)
self._listbox = SearchListbox(self,
selectmode=BROWSE,
yscrollcommand=sb.set)
self._listbox.pack(side=LEFT, expand=YES, fill=BOTH)
sb.config(command=self._listbox.yview)
if items:
for item in items:
self._listbox.insert(END, item)
if selected == item:
self._listbox.selection_set(END)
self._listbox.activate(END)
self.see(ACTIVE)
self._listbox.bind('<Escape>', self.close)
self._listbox.bind('<Return>', self._select)
self._listbox.bind('<KP_Enter>', self._select)
self._listbox.bind('<Tab>', self._select)
self._listbox.bind('<Shift-Tab>', self._select)
self._listbox.bind('<ButtonRelease-1>', self._release)
self.bind('<FocusOut>', self.close)
# ----------------------------------------------------------------------
def show(self, x, y):
self.deiconify()
if x is not None and y is not None:
self.geometry('+%d+%d' % (x,y))
self._listbox.focus_set()
#self.wait_visibility()
#self.grab_set()
self.wait_window()
return self.selected
# ----------------------------------------------------------------------
def close(self, event=None):
self.grab_release()
self.destroy()
# ----------------------------------------------------------------------
def _select(self, event=None):
self.selected = self._listbox.get(ACTIVE)
self.close()
# ----------------------------------------------------------------------
def _release(self, event):
act = self._listbox.nearest(event.y)
self._listbox.activate(act)
self._select()
#=============================================================================
# Combobox
#=============================================================================
class Combobox(Frame):
def __init__(self, master, label=True, *args, **kwargs):
Frame.__init__(self, master, class_="Combobox")
Frame.config(self, padx=0, pady=0)
if "command" in kwargs:
self.command = kwargs.get("command")
del kwargs["command"]
else:
self.command = None
# Create entry and button
if label:
self._text = Label(self, relief=GROOVE, anchor=W, *args, **kwargs)
else:
self._text = Entry(self, *args, **kwargs)
self._text.pack(side=LEFT, expand=YES, fill=BOTH)
# Arrow button
self._post = IntVar()
self._post.trace("w", self._showList)
self._arrowBtn = Checkbutton(self,
text=u"\u25BC",
variable=self._post,
indicatoron=False,
padx=2, pady=0)
self._arrowBtn.pack(side=RIGHT, fill=Y)
# Bindings
self._text.bind('<Up>', self.postList)
self._text.bind('<Down>', self.postList)
self._text.bind('<Return>', self.postList)
self._text.bind('<KP_Enter>', self.postList)
self.bind('<Up>', self.postList)
self.bind('<Down>', self.postList)
self.bind('<Return>', self.postList)
self.bind('<KP_Enter>', self.postList)
if label:
self._text.bind('<Key-space>', self.postList)
self._text.bind('<Button-1>', self._togglePost)
else:
self._text.bind('<Button-1>', self.button1)
self._text.bind('<ButtonRelease-1>', self.release1)
# Need to unpost the popup if the entryfield is unmapped (eg:
# its toplevel window is withdrawn) while the popup list is
# displayed.
self._text.bind('<Unmap>', self.unpostList)
# Create a static popup window with dropdown list
self._popup = Toplevel(master)
self._popup.overrideredirect(1)
self._popup.transient(master)
self._popup.withdraw()
# Create the listbox inside the dropdown window
sb = Scrollbar(self._popup)
sb.pack(side=RIGHT, fill=Y)
for k in ("anchor","justify"):
try: del kwargs[k]
except KeyError: pass
self._listbox = SearchListbox(self._popup,
selectmode=BROWSE,
yscrollcommand=sb.set,
*args,
**kwargs)
self._listbox.pack(side=LEFT, expand=YES, fill=BOTH)
sb.config(command=self._listbox.yview)
# Bind events to the dropdown window.
self._listbox.bind('<Escape>', self.unpostList)
self._listbox.bind('<Return>', self._selectUnpost)
self._listbox.bind('<KP_Enter>', self._selectUnpost)
self._listbox.bind('<Tab>', self._selectUnpost)
self._listbox.bind('<Shift-Tab>', self._selectUnpost)
self._listbox.bind('<ButtonRelease-1>', self._release)
self._popup.bind('<FocusOut>', self._focusOut)
self._popup.bind('<Button-1>', self.popupClick)
self._popup.bind('<Button-3>', self.popupClick)
#self._popup.bind('<Shift-Tab>', self._selectUnpostPrev)
#self._popup.bind('<Tab>', self._selectUnpostNext)
#if sys.platform in ("linux","linux2"):
# self._popup.bind('<ISO_Left_Tab>', self._selectUnpostPrev)
# grab_set redirects all mouse events to the list even
# when the list is posted with a mouse click
#self._hide_on_next_release = False
self._grab_window = None
# ----------------------------------------------------------------------
# Unhide and post the list
# ----------------------------------------------------------------------
def _showList(self, a=False, b=False, c=False):
if self._post.get():
Balloon.hide()
self._grab_window = None
try:
self._grab_window = self.grab_current()
if self._grab_window is not None:
self._grab_window.grab_release()
except KeyError:
pass
if self._text.cget("state") == DISABLED:
self._post.set(False)
return
ExListbox.resetSearch()
self.beforeShow()
h = self._popup.winfo_height()
if h == 1:
self._popup.deiconify()
self._popup.lift()
self._popup.update_idletasks()
h = self._popup.winfo_height()
w = self._text.winfo_width() + self._arrowBtn.winfo_width()
x = self._text.winfo_rootx()
y = self._text.winfo_rooty() + self._text.winfo_height()
sh = self.winfo_screenheight()
if y + h > sh and y > sh / 2:
y = self._text.winfo_rooty() - h
self._popup.deiconify()
self._popup.geometry('%dx%d+%d+%d' % (w,h,x,y))
self._popup.lift()
self._popup.grab_set()
self._popup.update_idletasks()
# Grab the popup, so that all events are delivered to it, and
# set focus to the listbox, to make keyboard navigation
# easier.
#self._popup.grab_set()
#self._popup.focus_set()
self._listbox.focus_set()
self._showSelection()
elif self._popup.winfo_ismapped():
self._popup.grab_release()
if self._grab_window:
self._grab_window.grab_set()
self._grab_window = None
self._popup.withdraw()
self._arrowBtn.focus_set()
self.afterHide()
# ----------------------------------------------------------------------
def _showSelection(self):
lb = self._listbox
lb.selection_clear(0,END)
item = self.get()
# Test active
if lb.get(ACTIVE) != item:
# Scan list
for i in range(lb.size()):
if item == lb.get(i):
lb.activate(i)
lb.selection_set(ACTIVE)
lb.see(ACTIVE)
# ----------------------------------------------------------------------
# Post list on click
# ----------------------------------------------------------------------
def button1(self, event):
if self.focus_get() is not self._text:
self.postList()
return "break"
# ----------------------------------------------------------------------
# Edit on release
# ----------------------------------------------------------------------
def release1(self, event):
if self.focus_get() is not self._text:
self._text.focus_set()
self._text.icursor("@%d"%(event.x))
# ----------------------------------------------------------------------
def postList(self, event=None):
if self._arrowBtn.cget("state") != DISABLED:
self._post.set(True)
return "break"
# ----------------------------------------------------------------------
def unpostList(self, event=None):
self._listbox.reset()
if self._arrowBtn.cget("state") != DISABLED:
self._post.set(False)
return "break"
# ----------------------------------------------------------------------
def _togglePost(self, event):
if self._text.cget("state") != DISABLED:
self._post.set( not self._post.get() )
return "break"
# ----------------------------------------------------------------------
def _focusOut(self, event):
try:
f = self._popup.focus_get()
except KeyError:
pass
else:
if f == self._popup or f == self._listbox:
return
self._focus = None
self.unpostList()
# ----------------------------------------------------------------------
def _selectUnpost(self, event=None):
if self._post.get():
sel = self._listbox.get(ACTIVE)
self.set(sel)
self.unpostList()
# ----------------------------------------------------------------------
def invoke(self):
if self.command is not None:
self.command()
# ----------------------------------------------------------------------
def _release(self, event):
act = self._listbox.nearest(event.y)
self._listbox.activate(act)
self._selectUnpost()
# ----------------------------------------------------------------------
def popupClick(self, event):
if event.x < 0 or event.y < 0 or \
event.x > self._popup.winfo_width() or \
event.y > self._popup.winfo_height():
self.unpostList()
# ----------------------------------------------------------------------
# The following methods are called before the show of the list ...
# ----------------------------------------------------------------------
def beforeShow(self):
pass
# ----------------------------------------------------------------------
# ... and after hide it
# The user should override them in case some special treatment is needed
# ----------------------------------------------------------------------
def afterHide(self):
pass
# ----------------------------------------------------------------------
# Public methods
# ----------------------------------------------------------------------
def get(self, first=None, last=None):
try:
stringclass = basestring
except NameError:
stringclass = False
if first is None:
if isinstance(self._text, Label):
return self._text.cget("text")
else:
return self._text.get()
else:
return self._listbox.get(first, last)
# ----------------------------------------------------------------------
def set(self, txt):
if isinstance(self._text, Label):
self._text.config(text=txt)
else:
self._text.delete(0, END)
self._text.insert(0, txt)
self._text.update_idletasks()
self.invoke()
# ----------------------------------------------------------------------
def size(self):
return self._listbox.size()
# ----------------------------------------------------------------------
def clear(self):
self.clearLabel()
self.clearList()
# ----------------------------------------------------------------------
def clearLabel(self):
if isinstance(self._text, Label):
self._text.config(text="")
else:
self._text.delete(0, END)
# ----------------------------------------------------------------------
def clearList(self):
self._listbox.delete(0, END)
# ----------------------------------------------------------------------
def insert(self, index, *elements):
self._listbox.insert(index, *elements)
# ----------------------------------------------------------------------
def delete(self, first, last=None):
self._listbox.delete(first, last)
# ----------------------------------------------------------------------
def fill(self, items):
self.clearList()
for item in items:
self._listbox.insert(END, item)
# ----------------------------------------------------------------------
def select(self, index=None):
if index is None:
txt = self.get()
for i in range(self.size()):
if txt == self._listbox.get(i):
return i
return -1
elif 0 <= index < self._listbox.size():
self.set(self._listbox.get(index))
# ----------------------------------------------------------------------
def configure(self, **kwargs):
if "command" in kwargs:
self.command = kwargs.get("command")
del kwargs["command"]
self._text.configure(**kwargs)
self._arrowBtn.configure(**kwargs)
config = configure
# ----------------------------------------------------------------------
def __setitem__(self, key, value):
self.configure({key:value})
# ----------------------------------------------------------------------
def cget(self, key):
return self._text.cget(key)
__getitem__ = cget
# ----------------------------------------------------------------------
def bindWidgets(self, event, func):
self._text.bind(event, func)
self._arrowBtn.bind(event, func)
#===============================================================================
# ExOptionMenu
#===============================================================================
class ExOptionMenu(OptionMenu):
def __init__(self, master, variable, value, *values, **kwargs):
OptionMenu.__init__(self, master, variable, value,
*values, **kwargs)
self.variable = variable
self.command = kwargs.get("command")
# ----------------------------------------------------------------------
def delete(self, from_=0, to_=END):
"""Delete items from menu"""
self["menu"].delete(from_, to_)
# ----------------------------------------------------------------------
def add(self, value):
"""Add an extra value to the menu"""
menu = self["menu"]
menu.add_command(label=value,
command=_setit(self.variable, value, None))
# ----------------------------------------------------------------------
def set(self, valueList, value=None):
"""
clear and reload the menu with a new set of options.
valueList - list of new options
value - initial value to set the optionmenu's menubutton to
"""
self['menu'].delete(0, END)
for item in valueList:
self['menu'].add_command(label=item,
command=_setit(self.variable, item, self.command))
if value:
self.variable.set(value)
#===============================================================================
# Splitter Frame
#===============================================================================
class Splitter(Frame):
"""Base class for horizontal or vertical frame splitter"""
def __init__(self, master, split=0.5, horizontal=True, absolute=False):
Frame.__init__(self, master, class_="Splitter")
self.f1 = Frame(self, bd=1, relief=SUNKEN)
self.f2 = Frame(self, bd=1, relief=SUNKEN)
self.dragFrame = Frame(self, bd=1, relief=GROOVE)
self.dragFrame.bind("<B1-Motion>", self.motion) # Overridden
self.dragFrame.bind("<ButtonRelease-1>", self.placeChilds) # Overridden
self.dragFrame.bind("<Double-Button-1>", self.toggle)
self.split = split
self.save = split
self.absolute = absolute
self.setRange()
self.setOrientation(horizontal)
if self.absolute:
self.bind("<Configure>", self.placeChilds)
# ----------------------------------------------------------------------
def orient(self): return self._hori
# ----------------------------------------------------------------------
def firstFrame(self): return self.f1
# ----------------------------------------------------------------------
def secondFrame(self): return self.f2
# ----------------------------------------------------------------------
def setOrientation(self, horizontal=True):
self._hori = horizontal # True horizontal / False vertical
self.f1.place_forget()
self.f2.place_forget()
if self._hori:
self.dragFrame["cursor"] = "sb_h_double_arrow"
else:
self.dragFrame["cursor"] = "sb_v_double_arrow"
self.placeChilds()
# ----------------------------------------------------------------------
def swapOrient(self):
self.dragFrame.place_forget()
self.setOrientation(not self._hori)
# ----------------------------------------------------------------------
def equal(self):
self._setSplit(0.5)
self.placeChilds()
# ----------------------------------------------------------------------
def minimize(self):
self._setSplit(0.0)
self.placeChilds()
# ----------------------------------------------------------------------
def maximize(self):
self._setSplit(1.0)
self.placeChilds()
# ----------------------------------------------------------------------
# Toggle position normally with double click
# ----------------------------------------------------------------------
def toggle(self, event=None):
if self.absolute:
if self.save == self.split: self.save = 100
if self.split > 20:
self.save = self.split
self.split = 1
else:
self.split = self.save
else:
if self.save == self.split: self.save = 0.3
if self.split <= self.min or self.split >= self.max:
self.split = self.save
elif self.split < 0.5:
self.split = self.min
else:
self.split = self.max
self.placeChilds()
# ----------------------------------------------------------------------
# Set acceptable range
# ----------------------------------------------------------------------
def setRange(self, _min=0.005, _max=0.995):
if _min<0.01: _min=0.01
if _max>0.99: _max=0.99
self.margin = 5 # pixels on absolute
self.min = _min
self.max = _max
# ----------------------------------------------------------------------
def _setSplit(self, newSplit):
if newSplit == self.split: return
if self.absolute:
if newSplit <= self.margin: newSplit = self.margin
if self._hori:
if newSplit + self.margin >= self.winfo_width():
newSplit = self.winfo_width() - self.margin
else:
if newSplit + self.margin >= self.winfo_height():
newSplit = self.winfo_height() - self.margin
else:
if newSplit <= self.min: newSplit = self.min
if newSplit >= self.max: newSplit = self.max
self.save = self.split
self.split = newSplit
# ----------------------------------------------------------------------
# Set the split position
# ----------------------------------------------------------------------
def setSplit(self, newSplit):
"""Change the spliting position"""
self._setSplit(newSplit)
self.placeChilds()
# ----------------------------------------------------------------------
def motion(self, event):
if self.absolute:
if self._hori:
# Horizontal
self._setSplit(event.x_root - self.winfo_rootx())
self.dragFrame.place(x=self.split-2,
relheight=1.0, width=5)
else:
pass
else:
if self._hori:
# Horizontal
self._setSplit(float(event.x_root - self.winfo_rootx()) / \
float(self.winfo_width()))
self.dragFrame.place(relx=self.split, x=-2,
relheight=1.0, width=5)
else:
# Vertical
self._setSplit(float(event.y_root - self.winfo_rooty()) / \
float(self.winfo_height()))
self.dragFrame.place(rely=self.split, y=-2,
relwidth=1.0, height=5)
# ----------------------------------------------------------------------
# Place the two frames
# ----------------------------------------------------------------------
def placeChilds(self, event=None):
"""(Re)Place the two frames"""
if self.absolute:
if self._hori:
# Horizontal
self.f1.place( relx=0.0,
width=self.split,
relheight=1.0)
self.f2.place( x=self.split+3,
width=self.winfo_width()-self.split-4,
relheight=1.0)
self.dragFrame.place(x=self.split-1,
relheight=1.0,
width=3)
else:
# Vertical
self.f1.place( rely=0.0,
height=self.split,
relwidth=1.0)
self.f2.place( y=self.split+3,
height=self.winfo_height()-self.split()-4,
relwidth=1.0)
self.dragFrame.place(y=self.split-1,
relwidth=1.0,
height=3)
else:
if self._hori:
# Horizontal
self.f1.place( relx=0.0,
relwidth=self.split,
relheight=1.0)
self.f2.place( relx=self.split,
x=3,
relwidth=1.0-self.split,
relheight=1.0)
self.dragFrame.place(relx=self.split,
x=-1,
relheight=1.0,
width=3)
else:
# Vertical
self.f1.place( rely=0.0,
relheight=self.split,
relwidth=1.0)
self.f2.place( rely=self.split,
y=2,
relheight=1.0-self.split,
relwidth=1.0)
self.dragFrame.place(rely=self.split,
y=-2,
relwidth=1.0,
height=4)
#===============================================================================
# Horizontal Splitter
#===============================================================================
class HSplitter(Splitter):
"""Horizontal frame splitter"""
def __init__(self, master, split=0.5, absolute=False):
Splitter.__init__(self, master, split, True, absolute)
# ----------------------------------------------------------------------
def leftFrame(self): return self.firstFrame()
left = leftFrame
# ----------------------------------------------------------------------
def rightFrame(self): return self.secondFrame()
right = rightFrame
#===============================================================================
# Vertical Splitter
#===============================================================================
class VSplitter(Splitter):
"""Vertical frame splitter"""
def __init__(self, master, split=0.5, absolute=False):
Splitter.__init__(self, master, split, False, absolute)
# ----------------------------------------------------------------------
def topFrame(self): return self.firstFrame()
top = topFrame
# ----------------------------------------------------------------------
def bottomFrame(self): return self.secondFrame()
bottom = bottomFrame
#===============================================================================
# Splitter Node
#-------------------------------------------------------------------------------
class _SplitNode:
def __init__(self, parent, widget, pos=0.5, hori=True):
self.parent = parent # Parent of node
self.left = None # Left child node
self.right = None # Right child node (None if end node)
self.pos = pos # Splitting position (<0.0 inverts hori)
self.hori = hori # Horizontal splitting (Vertical frames)
if self.pos < 0.0:
self.pos = -self.pos
self.hori = not self.hori
self.child = widget
self.split = None # drawing frame
self._xy = 0.0 # Absolute limits of window size for splitter
self._wh = 1.0
# ----------------------------------------------------------------------
def end(self): return self.child is not None
# ----------------------------------------------------------------------
def full(self): return self.left is not None and self.right is not None
# ----------------------------------------------------------------------
def getpos(self):
if self.hori:
return self.pos
else:
return -self.pos
# ----------------------------------------------------------------------
def setCursor(self):
if self.split:
if self.hori:
self.split["cursor"] = "sb_h_double_arrow"
else:
self.split["cursor"] = "sb_v_double_arrow"
# ----------------------------------------------------------------------
def makeSplit(self, master, drag):
self.split = Frame(master, bd=1, relief=GROOVE)
self.split.bind("<B1-Motion>",drag)
#split.bind("<ButtonRelease-1>", self.placeChilds)
#split.bind("<Double-Button-1>", self.toggle)
self.setCursor()
# ----------------------------------------------------------------------
def printNode(self, depth):
if self.left: self.left.printNode(depth+1)
#if self.child:
# say(" "*depth, self.child, self.child["bg"])
#else:
# say(" "*depth, " ======== H=",self.hori," pos=",self.pos)
if self.right: self.right.printNode(depth+1)
#===============================================================================
# Tree Splitter allows any nesting of splitting using a tree structure
#===============================================================================
class TreeSplitter(Frame):
"""Splitter using a tree structure"""
def __init__(self, master, **kw):
Frame.__init__(self, master, class_="TreeSplitter", **kw)
self.tree = None
self.width = 3
self.border = 0.01
self._maxchild = None
self._splitters= {} # Dictionary of splitters for faster lookup
self._drag = None
self._dragFrame = None
self._drag_x_root = -1
self._drag_y_root = -1
# ----------------------------------------------------------------------
def isempty(self): return self.tree is None
# ----------------------------------------------------------------------
# Add a new node under a parent
# ----------------------------------------------------------------------
def add(self, parent, child, pos=0.5, hori=True):
node = self.node(child)
if node is not None: return node
if isinstance(parent, Widget):
parent = self.node(parent)
node = self._add(parent, child, pos, hori)
self.placeChilds()
return node
# ----------------------------------------------------------------------
def _add(self, parent, child, pos=0.5, hori=True):
node = _SplitNode(parent, child, pos, hori)
if parent is None:
# Add to top
if self.tree is None:
self.tree = node # Set up the root node
self._maxchild = child
else:
oldtree = self.tree
self.tree = parent = _SplitNode(None, None, pos, hori)
parent.left = node
node.parent = parent
parent.right = oldtree
oldtree.parent = parent
self._maxchild = None
else:
if parent.end(): # End node with only a child
# Keep parent the same and make a new node for both childs
parent.left = _SplitNode(parent, parent.child, pos, hori)
parent.right = node
parent.child = None
else:
raise Exception("Parent node is full")
self._maxchild = None
if parent and parent.child is None:
parent.makeSplit(self, self.dragSplitter)
self._splitters[parent.split] = parent
self.placeChilds()
return node
# ----------------------------------------------------------------------
# Remove a child from the frame
# ----------------------------------------------------------------------
def remove(self, node):
if isinstance(node,Widget):
node = self.node(node)
if node is None: return
if node.child is self._maxchild: self._maxchild = None
self._remove(node)
self.placeChilds()
# ----------------------------------------------------------------------
def _remove(self, node):
if not node.end():
raise Exception("Only end nodes can be removed")
if node.child is not None: node.child.place_forget()
if node.split is not None:
del self._splitters[node.split]
node.split.place_forget()
parent = node.parent
if parent is None:
self.tree = None
elif parent.right is node:
# re-parent left node
grandpa = parent.parent
if grandpa is None: # root tree
self.tree = parent.left
elif grandpa.left is parent:
grandpa.left = parent.left
else:
grandpa.right = parent.left
parent.left.parent = grandpa
if parent.split is not None:
del self._splitters[parent.split]
parent.split.place_forget()
elif parent.left is node:
# re-parent right node
grandpa = parent.parent
if grandpa is None: # root tree
self.tree = parent.right
elif grandpa.left is parent:
grandpa.left = parent.right
else:
grandpa.right = parent.right
parent.right.parent = grandpa
if parent.split is not None:
del self._splitters[parent.split]
parent.split.place_forget()
else:
raise Exception("TreeSplitter is broken")
# ----------------------------------------------------------------------
# Replace the child of the node
# ----------------------------------------------------------------------
def replace(self, node, child):
place = node.child.place_info()
if self._maxchild is node.child:
self._maxchild = child
node.child.place_forget()
node.child = child
node.child.place(**place)
# ----------------------------------------------------------------------
# Clean up the whole tree
# ----------------------------------------------------------------------
def removeAll(self):
self.__remove(self.tree)
self.tree = None
self._splitters = {}
self._maxchild = None
# ----------------------------------------------------------------------
def __remove(self, node):
if node is None: return
self.__remove(node.left)
self.__remove(node.right)
if node.child is not None: node.child.place_forget()
if node.split is not None: node.split.place_forget()
# ----------------------------------------------------------------------
# Depending on rpn
# if None: Return RPN expression of the tree
# else: Create the tree from the rpn expression
# ----------------------------------------------------------------------
def RPN(self, rpn=None):
if rpn is not None:
self.removeAll()
stack = []
for item in rpn:
if isinstance(item, Widget):
stack.append(_SplitNode(None, item))
else:
try:
right = stack.pop()
left = stack.pop()
except IndexError:
break
node = _SplitNode(None, None, item)
node.makeSplit(self, self.dragSplitter)
self._splitters[node.split] = node
node.left = left
left.parent = node
node.right = right
right.parent = node
stack.append(node)
try:
self.tree = stack.pop()
self.placeChilds()
except IndexError:
pass
else:
rpn = []
self.__rpn(self.tree, rpn)
return rpn
# ----------------------------------------------------------------------
def __rpn(self, node, rpn):
if node is None: return
self.__rpn(node.left, rpn)
self.__rpn(node.right, rpn)
if node.child is not None:
rpn.append(node.child)
else:
rpn.append(node.getpos())
# ----------------------------------------------------------------------
def printTree(self):
if self.tree: self.tree.printNode(0)
# ----------------------------------------------------------------------
def childs(self):
"""Return list of child nodes"""
lst = []
self.__childNode(self.tree, lst)
return lst
# ----------------------------------------------------------------------
def __childNode(self, node, lst):
if node is None: return
self.__childNode(node.left, lst)
self.__childNode(node.right, lst)
if node.child is not None: lst.append(node.child)
# ----------------------------------------------------------------------
# return node that has as child the widget
# ----------------------------------------------------------------------
def __searchWidget(self, node, widget):
if node is None: return None
n = self.__searchWidget(node.left, widget)
if n is not None: return n
n = self.__searchWidget(node.right, widget)
if n is not None: return n
if node.child is widget: return node
return None
# ----------------------------------------------------------------------
def node(self, widget):
if widget is None: return None
return self.__searchWidget(self.tree, widget)
# ----------------------------------------------------------------------
def __placeForget(self, node):
if node is None: return
if node.split is not None: node.split.place_forget()
if node.child is not None: node.child.place_forget()
self.__placeForget(node.left)
self.__placeForget(node.right)
# ----------------------------------------------------------------------
def __placeNode(self, node, x, y, w, h):
if node is None: return
#say("placeNode", node, node.child)
#say(" ",x, y, w, h)
if node.end():
# Place the child
if x>0.0: xx = self.width
else: xx = 0
if x+w<1.0: ww = -self.width-xx
else: ww = 0
if y>0.0: yy = self.width
else: yy = 0
if y+h<1.0: hh = -self.width-yy
else: hh = 0
node.child.place(in_ = self,
relx = x,
x = xx,
relwidth = w,
width = ww,
rely = y,
y = yy,
relheight= h,
height = hh)
return
# Place the splitter
if node.hori: # Splitting along X => Vertical frames
node._xy = x
node._wh = w
pos = x + w*node.pos
sw = pos - x
if sw <= self.border:
pos = min(x+self.border, x+w-self.border)
sw = pos - x
if y>0.0: yy = self.width
else: yy = 0
if y+h<1.0: hh = -self.width
else: hh = 0
node.split.place(in_ = self,
relx = pos,
x = -self.width,
relwidth = 0.0,
width = 2*self.width,
rely = y,
y = yy,
relheight = h,
height = hh)
self.__placeNode(node.left, x, y, sw, h)
self.__placeNode(node.right, x+sw, y, w-sw, h)
else: # Splitting along Y => Horizontal frames
node._xy = y
node._wh = h
pos = y + h*node.pos
sh = pos - y
if sh <= self.border:
pos = min(y+self.border, y+w-self.border)
sh = pos - y
if x>0.0: xx = self.width
else: xx = 0
if x+w<1.0: ww = -self.width
else: ww = 0
node.split.place(in_ = self,
relx = x,
x = xx,
relwidth = w,
width = ww,
rely = pos,
y = -self.width,
relheight = 0.0,
height = 2*self.width)
self.__placeNode(node.left, x, y, w, sh)
self.__placeNode(node.right, x, y+sh, w, h-sh)
# ----------------------------------------------------------------------
# Place the frames [1..3]
# ----------------------------------------------------------------------
def placeChilds(self):
if self.tree is None: return
if self._maxchild is not None:
self.__placeForget(self.tree)
self._maxchild.place(in_ = self,
relx=0.0, x=0, relwidth=1.0, width=0,
rely=0.0, y=0, relheight=1.0, height=0)
else:
self.__placeNode(self.tree, 0.0, 0.0, 1.0, 1.0)
# ----------------------------------------------------------------------
# drag splitter and reposition childs
# ----------------------------------------------------------------------
def dragSplitter(self, event):
node = self._splitters[event.widget]
if node.hori:
pos = float(event.x_root - self.winfo_rootx()) / \
float(self.winfo_width())
else:
pos = float(event.y_root - self.winfo_rooty()) / \
float(self.winfo_height())
# Absolute positioning
pos = min(max(pos,self.border),1.0-self.border)
# Convert to relative
node.pos = (pos - node._xy) / node._wh
self.placeChilds()
# ----------------------------------------------------------------------
def maximize(self, child=None):
if self._maxchild is child:
self._maxchild = None
else:
self._maxchild = child
self.placeChilds()
# ----------------------------------------------------------------------
def maxchild(self):
return self._maxchild
# ----------------------------------------------------------------------
# return node containing x,y position in absolute coordinates
# ----------------------------------------------------------------------
def nodeContaining(self, node, x, y):
if node is None: return None
n = self.nodeContaining(node.left, x, y)
if n is not None: return n
n = self.nodeContaining(node.right, x, y)
if n is not None: return n
if node.child is not None:
rx = node.child.winfo_rootx()
ry = node.child.winfo_rooty()
if rx <= x <= rx+node.child.winfo_width() and \
ry <= y <= ry+node.child.winfo_height():
return node
return None
# ----------------------------------------------------------------------
# Find position in screen
# ----------------------------------------------------------------------
def reposition(self, node, event):
if self._maxchild is not None: return False
# First check absolute placement
x = float(event.x_root - self.winfo_rootx()) / float(self.winfo_width())
y = float(event.y_root - self.winfo_rooty()) / float(self.winfo_height())
if x<0.0 or x>1.0 or y<0.0 or y>1.0:
return
if x<0.1 or x>0.9 or y<0.1 or y>0.9:
x1 = 1.0-x
y1 = 1.0-y
self._remove(node)
newnode = self._add(None, node.child)
parent = self.tree
if x>y and x>y1:
# Move to right
parent.hori = True
if parent.right is not None: # Swap to move to TOP
parent.left, parent.right = parent.right, parent.left
elif x1>y and x1>y1:
# Move to left
parent.hori = True
elif y>x and y>x1:
# Move to bottom
parent.hori = False
if parent.right is not None: # Swap to move to TOP
parent.left, parent.right = parent.right, parent.left
else:
# Move to top
parent.hori = False
parent.setCursor()
self.placeChilds()
return True
# Place inside another widget
overnode = self.nodeContaining(self.tree, event.x_root, event.y_root)
if overnode is None or overnode is node: return False
overwidget = overnode.child
# Then inside other widgets
x = float(event.x_root - overwidget.winfo_rootx()) / float(overwidget.winfo_width())
y = float(event.y_root - overwidget.winfo_rooty()) / float(overwidget.winfo_height())
x1 = 1.0-x
y1 = 1.0-y
if 0.4<x<0.6 and 0.4<y<0.6:
# Swap children
overnode.child, node.child = node.child, overnode.child
else:
self._remove(node)
overnode = self.node(overwidget) # Maybe it has changed
newnode = self._add(overnode, node.child)
parent = newnode.parent
if x>y and x>y1:
# Move to right
parent.hori = True
elif x1>y and x1>y1:
# Move to left
parent.hori = True
if parent.right is not None: # Swap to move to TOP
parent.left, parent.right = parent.right, parent.left
elif y>x and y>x1:
# Move to bottom
parent.hori = False
else:
# Move to top
parent.hori = False
if parent.right is not None: # Swap to move to TOP
parent.left, parent.right = parent.right, parent.left
parent.setCursor()
self.placeChilds()
return True
# ----------------------------------------------------------------------
# Event handlers for dragging and placing
# Bind to <Button-1>
# ----------------------------------------------------------------------
def dragStart(self, event):
self._drag = None
self._dragFrame = None
self._drag_x_root = event.x_root
self._drag_y_root = event.y_root
# ----------------------------------------------------------------------
# Bind to <B1-Motion>
# ----------------------------------------------------------------------
def dragMove(self, event):
if self._maxchild is not None: return
if self.tree.child is not None: return # Only one node
if self._drag is None:
if abs(self._drag_x_root - event.x_root)>10 or \
abs(self._drag_y_root - event.y_root)>10:
self["cursor"] = "hand1"
self._drag = self.nodeContaining(self.tree,
self._drag_x_root,
self._drag_y_root)
if self._drag:
self._dragFrame = Frame(self._drag.child.master,
relief=RIDGE,
borderwidth=2*self.width,
bg="LightYellow")
elif self._dragFrame is not None:
# First check absolute placement
sx = float(self.winfo_rootx())
sy = float(self.winfo_rooty())
sw = float(self.winfo_width())
sh = float(self.winfo_height())
x = (float(event.x_root) - sx) / sw
y = (float(event.y_root) - sy) / sh
if x<0.0 or x>1.0 or y<0.0 or y>1.0:
self._dragFrame.place_forget()
return
if x<0.1 or x>0.9 or y<0.1 or y>0.9:
x1 = 1.0-x
y1 = 1.0-y
if x>y and x>y1:
# Move to right
self._dragFrame.place(in_=self,
relx = 0.5,
relwidth = 0.5,
rely = 0.0,
relheight = 1.0)
elif x1>y and x1>y1:
# Move to left
self._dragFrame.place(in_=self,
relx = 0.0,
relwidth = 0.5,
rely = 0.0,
relheight = 1.0)
elif y>x and y>x1:
# Move to bottom
self._dragFrame.place(in_=self,
relx = 0.0,
relwidth = 1.0,
rely = 0.5,
relheight = 0.5)
else:
# Move to top
self._dragFrame.place(in_=self,
relx = 0.0,
relwidth = 1.0,
rely = 0.0,
relheight = 0.5)
self._dragFrame.lift()
return
# Test inside a widget
over = self.nodeContaining(self.tree,
event.x_root,
event.y_root)
if over is None or over is self._drag:
self._dragFrame.place_forget()
return
overwidget = over.child
# Then inside other widgets
wx = float(overwidget.winfo_rootx())
wy = float(overwidget.winfo_rooty())
ww = float(overwidget.winfo_width())
wh = float(overwidget.winfo_height())
x = (float(event.x_root) - wx) / ww
y = (float(event.y_root) - wy) / wh
x1 = 1.0-x
y1 = 1.0-y
if 0.4<x<0.6 and 0.4<y<0.6:
# Swap children
self._dragFrame.place(in_=self,
relx = (wx-sx)/sw,
relwidth = ww/sw,
rely = (wy-sy)/sh,
relheight = wh/sh)
else:
if x>y and x>y1:
# Move to left
self._dragFrame.place(in_=self,
relx = (wx+ww/2.0-sx)/sw,
relwidth = ww/sw/2.0,
rely = (wy-sy)/sh,
relheight = wh/sh)
elif x1>y and x1>y1:
# Move to right
self._dragFrame.place(in_=self,
relx = (wx-sx)/sw,
relwidth = ww/sw/2.0,
rely = (wy-sy)/sh,
relheight = wh/sh)
elif y>x and y>x1:
# Move to bottom
self._dragFrame.place(in_=self,
relx = (wx-sx)/sw,
relwidth = ww/sw,
rely = (wy+wh/2.0-sy)/sh,
relheight = wh/sh/2.0)
else:
# Move to top
self._dragFrame.place(in_=self,
relx = (wx-sx)/sw,
relwidth = ww/sw,
rely = (wy-sy)/sh,
relheight = wh/sh/2.0)
# ----------------------------------------------------------------------
# Bind to <ButtonRelease-1>
# ----------------------------------------------------------------------
def dragEnd(self, event):
if self._maxchild is not None: return
if self._dragFrame is None: return
if self._drag:
self["cursor"] = ""
self._dragFrame.place_forget()
self._dragFrame = None
return self.reposition(self._drag, event)
return False
#=============================================================================
# Display a balloon message (only static methods)
#=============================================================================
class Balloon:
_top = None
_widget = None
font = ("Helvetica","-12")
foreground = "Black"
background = "LightYellow"
delay = 1500
x_mouse = 0
y_mouse = 0
# ----------------------------------------------------------------------
# set a balloon message to a widget
# ----------------------------------------------------------------------
@staticmethod
def set(widget, help):
widget._help = help
widget.bind('<Any-Enter>', Balloon.enter)
widget.bind('<Any-Leave>', Balloon.leave)
widget.bind('<Key>', Balloon.hide)
# ----------------------------------------------------------------------
@staticmethod
def enter(event):
if Balloon._widget is event.widget: return
Balloon._widget = event.widget
Balloon.x_mouse = event.x_root
Balloon.y_mouse = event.y_root
return event.widget.after(Balloon.delay, Balloon.show)
# ----------------------------------------------------------------------
@staticmethod
def leave(event=None):
Balloon._widget = None
if Balloon._top is None: return
try:
if Balloon._top.winfo_ismapped():
Balloon._top.withdraw()
except TclError:
Balloon._top = None
hide=leave
# ----------------------------------------------------------------------
@staticmethod
def setWidget(widget, x, y):
Balloon._widget = widget
Balloon.x_mouse = x
Balloon.y_mouse = y
# ----------------------------------------------------------------------
@staticmethod
def show():
try:
if Balloon._widget is None: return
widget = Balloon._widget
if Balloon._top is None:
Balloon._top = Toplevel()
Balloon._top.overrideredirect(1)
Balloon._msg = Message(Balloon._top,
aspect=300,
foreground=Balloon.foreground,
background=Balloon.background,
relief=SOLID,
borderwidth=1,
font=Balloon.font)
Balloon._msg.pack()
Balloon._top.bind("<1>",Balloon.hide)
Balloon._msg.config(text=widget._help)
# Guess position
x = widget.winfo_rootx() + widget.winfo_width()//2
y = widget.winfo_rooty() + widget.winfo_height()+5
# if too far away use mouse
if abs(x - Balloon.x_mouse) > 30:
x = Balloon.x_mouse + 20
if abs(y - Balloon.y_mouse) > 30:
y = Balloon.y_mouse + 10
Balloon._top.wm_geometry("+%d+%d" % (x,y))
Balloon._top.deiconify()
Balloon._top.lift()
Balloon._top.update_idletasks()
# Check if it is hidden on bottom-right sides
move = False
if Balloon._top.winfo_rootx() + Balloon._top.winfo_width() >= \
Balloon._top.winfo_screenwidth():
x = Balloon._top.winfo_screenwidth() \
- Balloon._top.winfo_width() - 20
move = True
if Balloon._top.winfo_rooty() + Balloon._top.winfo_height() >= \
Balloon._top.winfo_screenheight():
y = Balloon._top.winfo_screenheight() \
- Balloon._top.winfo_height() - 10
move = True
if move:
Balloon._top.wm_geometry("+%d+%d" % (x,y))
except TclError:
Balloon._top = None
#===============================================================================
# A LabelFrame that can collapse/expand
#===============================================================================
class ExLabelFrame(LabelFrame):
def __init__(self, master, *args, **kwargs):
if "command" in kwargs:
self.command = kwargs.get("command")
del kwargs["command"]
else:
self.command = None
LabelFrame.__init__(self, master, *args, **kwargs)
self.frame = Frame(self)
self.frame.pack(expand=YES, fill=BOTH)
self.bind("<Button-1>", self. click)
if self["height"]==0:
self["height"] = 20
self.width = self["width"]
# ----------------------------------------------------------------------
def click(self, event=None):
if self.frame.winfo_ismapped():
self.collapse()
else:
self.expand()
if self.command is not None:
self.command(event)
# ----------------------------------------------------------------------
def collapse(self):
self["width"] = self.winfo_width()
self.frame.pack_forget()
lbl = self["text"]
if lbl[-1] in (Unicode.BLACK_UP_POINTING_TRIANGLE, Unicode.BLACK_DOWN_POINTING_TRIANGLE):
lbl = lbl[:-1]
self["text"] = lbl+Unicode.BLACK_UP_POINTING_TRIANGLE
# ----------------------------------------------------------------------
def expand(self):
self["width"] = self.width
self.frame.pack(fill=BOTH)
lbl = self["text"]
if lbl[-1] in (Unicode.BLACK_UP_POINTING_TRIANGLE, Unicode.BLACK_DOWN_POINTING_TRIANGLE):
self["text"] = lbl[:-1]
# ----------------------------------------------------------------------
def isexpanded(self):
return self.frame.winfo_ismapped()
# ----------------------------------------------------------------------
def __call__(self): return self.frame
#================================================================================
# ScrollFrame based on Bruno's implementation
#================================================================================
class ScrollFrame(Frame):
# ----------------------------------------------------------------------
def __init__(self, master=None, stretch=True, cnf={}, **kw):
Frame.__init__(self, master, cnf, **kw)
self.client = Frame(self, border=0)
# width and height of Scrollframe
self.W = 1.0
self.H = 1.0
# top left corner coordinates of client frame
self.client_x = 0
self.client_y = 0
# width and height of client frame
self.client_w = 1.0
self.client_h = 1.0
# scroll commands (default)
self.xscrollcommand=lambda *args:None
self.yscrollcommand=lambda *args:None
# scroll increments
self.xscrollincrement = 15
self.yscrollincrement = 15
# stretches
self.stretch = stretch
self.stretch_x = stretch
self.stretch_y = stretch
#self.bind("<Expose>", self.updateScrollRegion)
self.bind("<Configure>", self.updateScrollRegion)
self.defaultBinds()
#w = self.client.winfo_toplevel()
self.mult = 1.0
self._drag = None
self._startx = self._startx = 0
# ----------------------------------------------------------------------
def cget(self,item):
if not hasattr(self,item):
return Frame.cget(self,item)
else:
getattr(self,item)
__getitem__ = cget
def __setitem__(self,item,value):self.configure({item:value})
# ----------------------------------------------------------------------
def configure(self,cnf=None,**kw):
if kw: cnf=_cnfmerge((cnf,kw))
for key in cnf.keys():
if not hasattr(self,key):
Frame.configure(self,cnf)
else:
setattr(self,key,cnf[key])
config=configure
# ----------------------------------------------------------------------
# Use this method to get the parent widget of the frame
# ----------------------------------------------------------------------
def __call__(self): return self.client
# ----------------------------------------------------------------------
def position(self):
return self.client_x, self.client_y
#-------------------------------------------------------------------------------
@staticmethod
def bindChilds(widget, event, function, ignore=None):
if ignore is None: ignore = [event]
for child in widget.winfo_children():
ScrollFrame.bindChilds(child, event, function, ignore)
for e in ignore:
if child.bind(e) or child.bind_class(child.__class__.__name__,e):
break
else:
child.bind(event, function)
#-------------------------------------------------------------------------------
def defaultBinds(self):
ignore = ["<2>", "<B2-Motion>", "<ButtonRelease-2>"]
ScrollFrame.bindChilds(self.client, "<B2-Motion>", self.drag, ignore)
del ignore[1] # delete motion that was already assigned
ScrollFrame.bindChilds(self.client, "<ButtonRelease-2>",self.dragRelease, ignore)
ScrollFrame.bindChilds(self.client, "<Button-4>", self.scrollUp)
ScrollFrame.bindChilds(self.client, "<Button-5>", self.scrollDown)
ScrollFrame.bindChilds(self.client, "<Shift-Button-4>", self.scrollLeft)
ScrollFrame.bindChilds(self.client, "<Shift-Button-5>", self.scrollRight)
# ----------------------------------------------------------------------
def ischild(self, widget):
if widget is None: return False
if widget is self.client: return True
return self.ischild(widget.master)
# ----------------------------------------------------------------------
def drag(self, event):
if self._drag is not None:
dx = (event.x_root - self._drag[0])*self.mult
dy = (event.y_root - self._drag[1])*self.mult
self.client_x = int(self._start_x + dx)
self.client_y = int(self._start_y + dy)
self.updateScrollx()
self.updateScrolly()
self.client.place_configure(x=self.client_x, y=self.client_y)
else:
if not self.ischild(event.widget): return
self.config(cursor="hand2")
self._drag = event.x_root, event.y_root
self._start_x = self.client_x
self._start_y = self.client_y
return "break"
# ----------------------------------------------------------------------
def dragRelease(self, event):
self._drag = None
self.config(cursor="")
return "break"
# ----------------------------------------------------------------------
def scrollUp(self, event):
if not self.ischild(event.widget): return
self.yview(SCROLL, -1, UNITS)
return "break"
# ----------------------------------------------------------------------
def scrollDown(self, event):
if not self.ischild(event.widget): return
self.yview(SCROLL, 1, UNITS)
return "break"
# ----------------------------------------------------------------------
def scrollLeft(self, event):
if not self.ischild(event.widget): return
self.xview(SCROLL, -1, UNITS)
return "break"
# ----------------------------------------------------------------------
def scrollRight(self, event):
if not self.ischild(event.widget): return
self.xview(SCROLL, 1, UNITS)
return "break"
# ----------------------------------------------------------------------
def xview(self, action, value, units='pages'):
if action == "moveto":
fraction = float(value)
if fraction <= 0.0:
self.client_x = 0
elif fraction >= float(self.client_w-self.W)/self.client_w:
self.client_x = self.W-self.client_w
else:
self.client_x = int(-self.client_w*fraction)
elif action == "scroll":
amount=int(value)
if self.client_x == 0 and amount < 0:return
if self.W >= self.client_w: return
if self.client_x == self.W-self.client_w and amount > 0:return
if units == "units":
dx = self.xscrollincrement
else:
dx = amount*self.W*0.99
self.client_x -= amount*dx
else:
return
self.updateScrollx()
self.client.place_configure(x=self.client_x)
# ----------------------------------------------------------------------
def yview(self, action, value, units='pages'):
if action == "moveto":
fraction=float(value)
if fraction <= 0.0:
self.client_y = 0
elif fraction >= float(self.client_h-self.H)/self.client_h:
self.client_y = self.H-self.client_h
else:
self.client_y = int(-self.client_h*fraction)
elif action == "scroll":
amount=int(value)
if self.client_y == 0 and amount < 0:return
if self.H >= self.client_h: return
if self.client_y == self.H-self.client_h and amount > 0:return
if units == "units":
dy = self.yscrollincrement
else:
dy = self.H
self.client_y -= amount*dy
else:
return
self.updateScrolly()
self.client.place_configure(y=self.client_y)
# ----------------------------------------------------------------------
def moveto(self, x, y):
if x >= 0:
self.client_x = 0
elif x <= self.W - self.client_w:
self.client_x = self.W-self.client_w
else:
self.client_x = x
if y >= 0:
self.client_y = 0
elif y <= self.H - self.client_h:
self.client_y = self.H-self.client_h
else:
self.client_y = y
self.updateScrollx()
self.updateScrolly()
self.client.place_configure(x=self.client_x,y=self.client_y)
# ----------------------------------------------------------------------
def updateScrollx(self, *args):
if self.client_x >= 0:
low = 0.0
else:
low = -float(self.client_x)/self.client_w
if self.client_x+self.client_w <= self.W:
high = 1.0
else:
high = low+float(self.W)/self.client_w
if low <= 0.0:
self.client_x=0
elif high >= 1.0:
if self.client_w > self.W:
self.client_x = self.W-self.client_w
low = -float(self.client_x)/self.client_w
else:
self.client_x = 0
low = 0.0
high = low+float(self.W)/self.client_w
if self.client_w < self.W:
self.stretch_x = self.stretch
else:
self.stretch_x = False
self.xscrollcommand(low,high)
# ----------------------------------------------------------------------
def updateScrolly(self, *args):
if self.client_y >= 0:
low = 0.0
else:
low = -float(self.client_y)/self.client_h
if self.client_y+self.client_h <= self.H:
high = 1.0
else:
high = low+float(self.H)/self.client_h
if low <= 0.0:
self.client_y = 0
elif high >= 1.0:
if self.client_h > self.H:
self.client_y = self.H-self.client_h
low = -float(self.client_y)/self.client_h
else:
self.client_y = 0
low = 0.0
high = low+float(self.H)/self.client_h
if self.client_h < self.H:
self.stretch_y = self.stretch
else:
self.stretch_y = False
self.yscrollcommand(low,high)
# ----------------------------------------------------------------------
def updateScrollRegion(self, *args):
if len(self.client.children):
self.client_w = self.client.winfo_reqwidth()
self.client_h = self.client.winfo_reqheight()
self.W = self.winfo_width()
self.H = self.winfo_height()
self.updateScrolly()
self.updateScrollx()
if self.stretch_y:
h = self.H
else:
h = self.client_h
if self.stretch_x:
w = self.W
else:
w = self.client_w
self.client.place_configure(
x=self.client_x,
y=self.client_y,
height=h,
width=w,
anchor="nw")
else:
self.xscrollcommand(0.0,1.0)
self.yscrollcommand(0.0,1.0)
self.client.place_forget()
#================================================================================
# The following is from idlelib (tabpage.py)
#================================================================================
class InvalidTabPage(Exception): pass
class AlreadyExists(Exception): pass
#===============================================================================
# A page tab frame button
#===============================================================================
class PageTab(Frame):
"""
a 'page tab' like framed button
"""
# ----------------------------------------------------------------------
def __init__(self, parent):
Frame.__init__(self, parent, borderwidth=2, relief=RIDGE)
self.button=Radiobutton(self, padx=5, pady=2, takefocus=FALSE,
indicatoron=FALSE, highlightthickness=0,
borderwidth=0, selectcolor=self.cget('bg'))
self.button.pack(fill=BOTH)
#===============================================================================
# Tab pages
#===============================================================================
class TabPageSet(Frame):
"""
a set of 'pages' with TabButtons for controlling their display
"""
# ----------------------------------------------------------------------
def __init__(self, parent, pageNames=[], top=True, hidetext=False, **kw):
"""
pageNames - a list of strings, each string will be the dictionary key
to a page's data, and the name displayed on the page's tab. Should be
specified in desired page order. The first page will be the default
and first active page.
"""
Frame.__init__(self, parent, kw)
self.grid_location(0, 0)
self.tabBar=Frame(self)
self.top = top
self.hidetext = hidetext
if top:
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.tabBar.grid(row=0, column=0, sticky=EW)
else:
self.columnconfigure(1, weight=1)
self.rowconfigure(0, weight=1)
self.tabBar.grid(row=0, column=0, sticky=NSEW)
self.activePage=StringVar(self)
self.defaultPage=''
self.pages={}
for name in pageNames:
if isinstance(name,tuple):
self.addPage(*name)
else:
self.addPage(name)
# ----------------------------------------------------------------------
def page(self, name):
return self.pages[name]['page']
# ----------------------------------------------------------------------
def __getitem__(self, name): return self.page(name)
# ----------------------------------------------------------------------
def changePage(self, pageName=None):
if pageName:
if pageName in self.pages.keys():
self.activePage.set(pageName)
else:
raise InvalidTabPage("Invalid TabPage Name")
## pop up the active 'tab' only
for page in self.pages.keys():
tab = self.pages[page]['tab']
tab.config(relief=RIDGE)
tab.button.config(background="DarkGray",
activebackground="DarkGray")
if self.hidetext: tab.button.config(text="")
tab = self.pages[self.getActivePage()]['tab']
tab.config(relief=RAISED)
tab.button.config(
background="LightGray",
activebackground="LightGray")
if self.hidetext:
tab.button.config(text=self.getActivePage())
## switch page
self.pages[self.getActivePage()]['page'].lift()
self.event_generate("<<ChangePage>>") #, data=pageName)
# ----------------------------------------------------------------------
def getActivePage(self):
return self.activePage.get()
# ----------------------------------------------------------------------
def addPage(self, pageName, icon=None):
if pageName in self.pages.keys():
raise AlreadyExists("TabPage Name Already Exists")
self.pages[pageName]={
'tab' : PageTab(self.tabBar),
'page': Frame(self, borderwidth=2, relief=RAISED) }
if icon:
self.pages[pageName]['tab'].button.config(text=pageName,
image=icon, compound=LEFT)
self.icons = True
else:
self.pages[pageName]['tab'].button.config(text=pageName)
self.pages[pageName]['tab'].button.config(
command=self.changePage,
variable=self.activePage,
value=pageName)
if self.top:
self.pages[pageName]['tab'].pack(side=LEFT)
self.pages[pageName]['page'].grid(row=1, column=0, sticky=NSEW)
else:
self.pages[pageName]['tab'].pack(side=TOP, fill=X)
self.pages[pageName]['page'].grid(row=0, column=1, sticky=NSEW)
if len(self.pages)==1: # adding first page
self.defaultPage=pageName
self.activePage.set(self.defaultPage)
self.changePage()
# ----------------------------------------------------------------------
def removePage(self, pageName):
if not pageName in self.pages.keys():
raise InvalidTabPage("Invalid TabPage Name")
self.pages[pageName]['tab'].pack_forget()
self.pages[pageName]['page'].grid_forget()
self.pages[pageName]['tab'].destroy()
self.pages[pageName]['page'].destroy()
del(self.pages[pageName])
# handle removing last remaining, or default, or active page
if not self.pages: # removed last remaining page
self.defaultPage=''
return
if pageName==self.defaultPage: # set a new default page
self.defaultPage=\
self.tabBar.winfo_children()[0].button.cget('text')
if pageName==self.getActivePage(): # set a new active page
self.activePage.set(self.defaultPage)
self.changePage()
# ----------------------------------------------------------------------
def renamePage(self, old, new):
if not old in self.pages.keys():
raise InvalidTabPage("Invalid TabPage Name")
self.pages[new] = self.pages[old]
del self.pages[old]
self.pages[new]['tab'].button.config(text=new, value=new)
if old == self.getActivePage():
self.activePage.set(new)
#===============================================================================
if __name__ == "__main__":
root = Tk()
frame = Frame(root)
frame.pack(side=TOP, fill=X)
p = ProgressBar(frame, background="DarkGray", height=24)
p.pack(side=TOP, fill=X)
def addProg(ev):
global p
p.setProgress(p.getProgress()[0]+10.0)
p.autoText()
p.bind('<1>', addProg)
frame = Frame(root)
frame.pack(side=BOTTOM, expand=YES, fill=BOTH)
hsplit = HSplitter(frame, 0.7)
vsplitL = VSplitter(hsplit.leftFrame(), 0.5)
vsplitR = VSplitter(hsplit.rightFrame(), 0.3)
Label(vsplitL.topFrame(), text='MultiListbox').pack()
mlb = MultiListbox(vsplitL.topFrame(),
(('Subject', 40, None),
('Sender', 20, None),
('Date', 10, None)))
for i in range(100):
mlb.insert(END, ('%d Important Message' % i,
'John Doe', '10/10/%04d' % (1900+i)))
mlb.pack(expand=YES, fill=BOTH)
l = Label(vsplitL.bottomFrame(), text="Combobox")
l.pack(side=TOP)
cb = Combobox(vsplitL.bottomFrame(), label=True)
cb.pack(side=BOTTOM, expand=YES, fill=X)
cb.fill(("one","two","three","four", "fix-six-seven-eight-nine-ten"))
cb.select(0)
Label(vsplitR.topFrame(), text='SearchListbox').pack()
lb = SearchListbox(vsplitR.topFrame(), selectmode=BROWSE,
exportselection=FALSE)
lb.insert(END,"Starting")
lb.insert(END,"Loading card database")
lb.insert(END,"Loading isotopes database")
lb.insert(END,"Layout initialization")
lb.insert(END,"Layout create Tree list")
lb.insert(END,"--Initialize Tk")
lb.insert(END,"After initialization of Tk")
lb.insert(END,"Creating frames")
lb.insert(END,"Creation of windows...")
lb.insert(END,"Writing ini file")
lb.insert(END,"Exiting program")
# lb.fill()
lb.pack(expand=YES, fill=BOTH)
lb.focus_set()
lb.ignoreCase = True
# lb.ignoreNonAlpha = False
# v = StringVar()
# lst = ["One", "Two", "Three", "Four"]
# v.set("One")
# o = ExOptionMenu(vsplitR.bottomFrame(), v, *lst)
# o.pack()
# o.delete()
# lst.reverse()
# for i in lst:
# o.add(i)
#test dialog
frame = vsplitR.bottomFrame()
tabPage=TabPageSet(frame, pageNames=['Foobar','Baz'])
tabPage.pack(expand=TRUE, fill=BOTH)
Label(tabPage['Foobar'], text='Foo', pady=20).pack()
Label(tabPage['Foobar'], text='Bar', pady=20).pack()
Label(tabPage['Baz'], text='Baz').pack()
entryPgName=Entry(frame)
buttonAdd=Button(frame, text='Add Page',
command=lambda:tabPage.addPage(entryPgName.get()))
buttonRemove=Button(frame, text='Remove Page',
command=lambda:tabPage.removePage(entryPgName.get()))
labelPgName=Label(frame, text='name of page to add/remove:')
buttonAdd.pack(padx=5, pady=5)
buttonRemove.pack(padx=5, pady=5)
labelPgName.pack(padx=5)
entryPgName.pack(padx=5)
tabPage.changePage()
b = Button(root, text="Exit", command=root.destroy)
Balloon.set(b, "Push me to exit")
b.pack()
e = FloatEntry(root)
Balloon.set(e, "Enter a floating point number")
e.pack()
e = IntegerEntry(root)
Balloon.set(e, "Enter an integer number")
e.pack()
root.geometry("800x600")
root.mainloop()
|