summaryrefslogtreecommitdiffstats
path: root/Lib/lib-tk/Tkinter.py
blob: f8b4bd5485075c28fbc4647530bb01746b02ff03 (plain)
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
# Tkinter.py -- Tk/Tcl widget wrappers

__version__ = "$Revision$"

import sys
if sys.platform == "win32":
	import FixTk # Attempt to configure Tcl/Tk without requiring PATH
import _tkinter # If this fails your Python may not be configured for Tk
tkinter = _tkinter # b/w compat for export
TclError = _tkinter.TclError
from types import *
from Tkconstants import *
import string; _string = string; del string
try:
	import MacOS; _MacOS = MacOS; del MacOS
except ImportError:
	_MacOS = None

TkVersion = _string.atof(_tkinter.TK_VERSION)
TclVersion = _string.atof(_tkinter.TCL_VERSION)

READABLE = _tkinter.READABLE
WRITABLE = _tkinter.WRITABLE
EXCEPTION = _tkinter.EXCEPTION

# These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
try: _tkinter.createfilehandler
except AttributeError: _tkinter.createfilehandler = None
try: _tkinter.deletefilehandler
except AttributeError: _tkinter.deletefilehandler = None
    
    
def _flatten(tuple):
	res = ()
	for item in tuple:
		if type(item) in (TupleType, ListType):
			res = res + _flatten(item)
		elif item is not None:
			res = res + (item,)
	return res

def _cnfmerge(cnfs):
	if type(cnfs) is DictionaryType:
		return cnfs
	elif type(cnfs) in (NoneType, StringType):
		return cnfs
	else:
		cnf = {}
		for c in _flatten(cnfs):
			try:
				cnf.update(c)
			except (AttributeError, TypeError), msg:
				print "_cnfmerge: fallback due to:", msg
				for k, v in c.items():
					cnf[k] = v
		return cnf

class Event:
	pass

_support_default_root = 1
_default_root = None

def NoDefaultRoot():
	global _support_default_root
	_support_default_root = 0
	global _default_root
	_default_root = None
	del _default_root

def _tkerror(err):
	pass

def _exit(code='0'):
	raise SystemExit, code

_varnum = 0
class Variable:
	_default = ""
	def __init__(self, master=None):
		global _varnum
		if not master:
			master = _default_root
		self._master = master
		self._tk = master.tk
		self._name = 'PY_VAR' + `_varnum`
		_varnum = _varnum + 1
		self.set(self._default)
	def __del__(self):
		self._tk.globalunsetvar(self._name)
	def __str__(self):
		return self._name
	def set(self, value):
		return self._tk.globalsetvar(self._name, value)
	def trace_variable(self, mode, callback):
		cbname = self._master._register(callback)
		self._tk.call("trace", "variable", self._name, mode, cbname)
		return cbname
	trace = trace_variable
	def trace_vdelete(self, mode, cbname):
		self._tk.call("trace", "vdelete", self._name, mode, cbname)
		self._master.deletecommand(cbname)
	def trace_vinfo(self):
		return map(self._tk.split, self._tk.splitlist(
			self._tk.call("trace", "vinfo", self._name)))

class StringVar(Variable):
	_default = ""
	def __init__(self, master=None):
		Variable.__init__(self, master)
	def get(self):
		return self._tk.globalgetvar(self._name)

class IntVar(Variable):
	_default = 0
	def __init__(self, master=None):
		Variable.__init__(self, master)
	def get(self):
		return getint(self._tk.globalgetvar(self._name))

class DoubleVar(Variable):
	_default = 0.0
	def __init__(self, master=None):
		Variable.__init__(self, master)
	def get(self):
		return getdouble(self._tk.globalgetvar(self._name))

class BooleanVar(Variable):
	_default = "false"
	def __init__(self, master=None):
		Variable.__init__(self, master)
	def get(self):
		return self._tk.getboolean(self._tk.globalgetvar(self._name))

def mainloop(n=0):
	_default_root.tk.mainloop(n)

getint = int

getdouble = float

def getboolean(s):
	return _default_root.tk.getboolean(s)

# Methods defined on both toplevel and interior widgets
class Misc:
	# XXX font command?
	_tclCommands = None
	def destroy(self):
		if self._tclCommands is not None:
			for name in self._tclCommands:
				#print '- Tkinter: deleted command', name
				self.tk.deletecommand(name)
			self._tclCommands = None
	def deletecommand(self, name):
		#print '- Tkinter: deleted command', name
		self.tk.deletecommand(name)
		try:
			self._tclCommands.remove(name)
		except ValueError:
			pass
	def tk_strictMotif(self, boolean=None):
		return self.tk.getboolean(self.tk.call(
			'set', 'tk_strictMotif', boolean))
	def tk_bisque(self):
		self.tk.call('tk_bisque')
	def tk_setPalette(self, *args, **kw):
		self.tk.call(('tk_setPalette',)
		      + _flatten(args) + _flatten(kw.items()))
	def tk_menuBar(self, *args):
		pass # obsolete since Tk 4.0
	def wait_variable(self, name='PY_VAR'):
		self.tk.call('tkwait', 'variable', name)
	waitvar = wait_variable # XXX b/w compat
	def wait_window(self, window=None):
		if window == None:
			window = self
		self.tk.call('tkwait', 'window', window._w)
	def wait_visibility(self, window=None):
		if window == None:
			window = self
		self.tk.call('tkwait', 'visibility', window._w)
	def setvar(self, name='PY_VAR', value='1'):
		self.tk.setvar(name, value)
	def getvar(self, name='PY_VAR'):
		return self.tk.getvar(name)
	getint = int
	getdouble = float
	def getboolean(self, s):
		return self.tk.getboolean(s)
	def focus_set(self):
		self.tk.call('focus', self._w)
	focus = focus_set # XXX b/w compat?
	def focus_force(self):
		self.tk.call('focus', '-force', self._w)
	def focus_get(self):
		name = self.tk.call('focus')
		if name == 'none' or not name: return None
		return self._nametowidget(name)
	def focus_displayof(self):
		name = self.tk.call('focus', '-displayof', self._w)
		if name == 'none' or not name: return None
		return self._nametowidget(name)
	def focus_lastfor(self):
		name = self.tk.call('focus', '-lastfor', self._w)
		if name == 'none' or not name: return None
		return self._nametowidget(name)
	def tk_focusFollowsMouse(self):
		self.tk.call('tk_focusFollowsMouse')
	def tk_focusNext(self):
		name = self.tk.call('tk_focusNext', self._w)
		if not name: return None
		return self._nametowidget(name)
	def tk_focusPrev(self):
		name = self.tk.call('tk_focusPrev', self._w)
		if not name: return None
		return self._nametowidget(name)
	def after(self, ms, func=None, *args):
		if not func:
			# I'd rather use time.sleep(ms*0.001)
			self.tk.call('after', ms)
		else:
			# XXX Disgusting hack to clean up after calling func
			tmp = []
			def callit(func=func, args=args, self=self, tmp=tmp):
				try:
					apply(func, args)
				finally:
					try:
						self.deletecommand(tmp[0])
					except TclError:
						pass
			name = self._register(callit)
			tmp.append(name)
			return self.tk.call('after', ms, name)
	def after_idle(self, func, *args):
		return apply(self.after, ('idle', func) + args)
	def after_cancel(self, id):
		self.tk.call('after', 'cancel', id)
	def bell(self, displayof=0):
		self.tk.call(('bell',) + self._displayof(displayof))
	# Clipboard handling:
	def clipboard_clear(self, **kw):
		if not kw.has_key('displayof'): kw['displayof'] = self._w
		self.tk.call(('clipboard', 'clear') + self._options(kw))
	def clipboard_append(self, string, **kw):
		if not kw.has_key('displayof'): kw['displayof'] = self._w
		self.tk.call(('clipboard', 'append') + self._options(kw)
		      + ('--', string))
	# XXX grab current w/o window argument
	def grab_current(self):
		name = self.tk.call('grab', 'current', self._w)
		if not name: return None
		return self._nametowidget(name)
	def grab_release(self):
		self.tk.call('grab', 'release', self._w)
	def grab_set(self):
		self.tk.call('grab', 'set', self._w)
	def grab_set_global(self):
		self.tk.call('grab', 'set', '-global', self._w)
	def grab_status(self):
		status = self.tk.call('grab', 'status', self._w)
		if status == 'none': status = None
		return status
	def lower(self, belowThis=None):
		self.tk.call('lower', self._w, belowThis)
	def option_add(self, pattern, value, priority = None):
		self.tk.call('option', 'add', pattern, value, priority)
	def option_clear(self):
		self.tk.call('option', 'clear')
	def option_get(self, name, className):
		return self.tk.call('option', 'get', self._w, name, className)
	def option_readfile(self, fileName, priority = None):
		self.tk.call('option', 'readfile', fileName, priority)
	def selection_clear(self, **kw):
		if not kw.has_key('displayof'): kw['displayof'] = self._w
		self.tk.call(('selection', 'clear') + self._options(kw))
	def selection_get(self, **kw):
		if not kw.has_key('displayof'): kw['displayof'] = self._w
		return self.tk.call(('selection', 'get') + self._options(kw))
	def selection_handle(self, command, **kw):
		name = self._register(command)
		self.tk.call(('selection', 'handle') + self._options(kw)
		      + (self._w, name))
	def selection_own(self, **kw):
		"Become owner of X selection."
		self.tk.call(('selection', 'own') +
			     self._options(kw) + (self._w,))
	def selection_own_get(self, **kw):
		"Find owner of X selection."
		if not kw.has_key('displayof'): kw['displayof'] = self._w
		name = self.tk.call(('selection', 'own') + self._options(kw))
		if not name: return None
		return self._nametowidget(name)
	def send(self, interp, cmd, *args):
		return self.tk.call(('send', interp, cmd) + args)
	def lower(self, belowThis=None):
		self.tk.call('lower', self._w, belowThis)
	def tkraise(self, aboveThis=None):
		self.tk.call('raise', self._w, aboveThis)
	lift = tkraise
	def colormodel(self, value=None):
		return self.tk.call('tk', 'colormodel', self._w, value)
	def winfo_atom(self, name, displayof=0):
		args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
		return getint(self.tk.call(args))
	def winfo_atomname(self, id, displayof=0):
		args = ('winfo', 'atomname') \
		       + self._displayof(displayof) + (id,)
		return self.tk.call(args)
	def winfo_cells(self):
		return getint(
			self.tk.call('winfo', 'cells', self._w))
	def winfo_children(self):
		return map(self._nametowidget,
			   self.tk.splitlist(self.tk.call(
				   'winfo', 'children', self._w)))
	def winfo_class(self):
		return self.tk.call('winfo', 'class', self._w)
	def winfo_colormapfull(self):
		return self.tk.getboolean(
			self.tk.call('winfo', 'colormapfull', self._w))
	def winfo_containing(self, rootX, rootY, displayof=0):
		args = ('winfo', 'containing') \
		       + self._displayof(displayof) + (rootX, rootY)
		name = self.tk.call(args)
		if not name: return None
		return self._nametowidget(name)
	def winfo_depth(self):
		return getint(self.tk.call('winfo', 'depth', self._w))
	def winfo_exists(self):
		return getint(
			self.tk.call('winfo', 'exists', self._w))
	def winfo_fpixels(self, number):
		return getdouble(self.tk.call(
			'winfo', 'fpixels', self._w, number))
	def winfo_geometry(self):
		return self.tk.call('winfo', 'geometry', self._w)
	def winfo_height(self):
		return getint(
			self.tk.call('winfo', 'height', self._w))
	def winfo_id(self):
		return self.tk.getint(
			self.tk.call('winfo', 'id', self._w))
	def winfo_interps(self, displayof=0):
		args = ('winfo', 'interps') + self._displayof(displayof)
		return self.tk.splitlist(self.tk.call(args))
	def winfo_ismapped(self):
		return getint(
			self.tk.call('winfo', 'ismapped', self._w))
	def winfo_manager(self):
		return self.tk.call('winfo', 'manager', self._w)
	def winfo_name(self):
		return self.tk.call('winfo', 'name', self._w)
	def winfo_parent(self):
		return self.tk.call('winfo', 'parent', self._w)
	def winfo_pathname(self, id, displayof=0):
		args = ('winfo', 'pathname') \
		       + self._displayof(displayof) + (id,)
		return self.tk.call(args)
	def winfo_pixels(self, number):
		return getint(
			self.tk.call('winfo', 'pixels', self._w, number))
	def winfo_pointerx(self):
		return getint(
			self.tk.call('winfo', 'pointerx', self._w))
	def winfo_pointerxy(self):
		return self._getints(
			self.tk.call('winfo', 'pointerxy', self._w))
	def winfo_pointery(self):
		return getint(
			self.tk.call('winfo', 'pointery', self._w))
	def winfo_reqheight(self):
		return getint(
			self.tk.call('winfo', 'reqheight', self._w))
	def winfo_reqwidth(self):
		return getint(
			self.tk.call('winfo', 'reqwidth', self._w))
	def winfo_rgb(self, color):
		return self._getints(
			self.tk.call('winfo', 'rgb', self._w, color))
	def winfo_rootx(self):
		return getint(
			self.tk.call('winfo', 'rootx', self._w))
	def winfo_rooty(self):
		return getint(
			self.tk.call('winfo', 'rooty', self._w))
	def winfo_screen(self):
		return self.tk.call('winfo', 'screen', self._w)
	def winfo_screencells(self):
		return getint(
			self.tk.call('winfo', 'screencells', self._w))
	def winfo_screendepth(self):
		return getint(
			self.tk.call('winfo', 'screendepth', self._w))
	def winfo_screenheight(self):
		return getint(
			self.tk.call('winfo', 'screenheight', self._w))
	def winfo_screenmmheight(self):
		return getint(
			self.tk.call('winfo', 'screenmmheight', self._w))
	def winfo_screenmmwidth(self):
		return getint(
			self.tk.call('winfo', 'screenmmwidth', self._w))
	def winfo_screenvisual(self):
		return self.tk.call('winfo', 'screenvisual', self._w)
	def winfo_screenwidth(self):
		return getint(
			self.tk.call('winfo', 'screenwidth', self._w))
	def winfo_server(self):
		return self.tk.call('winfo', 'server', self._w)
	def winfo_toplevel(self):
		return self._nametowidget(self.tk.call(
			'winfo', 'toplevel', self._w))
	def winfo_viewable(self):
		return getint(
			self.tk.call('winfo', 'viewable', self._w))
	def winfo_visual(self):
		return self.tk.call('winfo', 'visual', self._w)
	def winfo_visualid(self):
		return self.tk.call('winfo', 'visualid', self._w)
	def winfo_visualsavailable(self, includeids=0):
		data = self.tk.split(
			self.tk.call('winfo', 'visualsavailable', self._w,
				     includeids and 'includeids' or None))
		return map(self.__winfo_parseitem, data)
	def __winfo_parseitem(self, t):
		return t[:1] + tuple(map(self.__winfo_getint, t[1:]))
	def __winfo_getint(self, x):
		return _string.atoi(x, 0)
	def winfo_vrootheight(self):
		return getint(
			self.tk.call('winfo', 'vrootheight', self._w))
	def winfo_vrootwidth(self):
		return getint(
			self.tk.call('winfo', 'vrootwidth', self._w))
	def winfo_vrootx(self):
		return getint(
			self.tk.call('winfo', 'vrootx', self._w))
	def winfo_vrooty(self):
		return getint(
			self.tk.call('winfo', 'vrooty', self._w))
	def winfo_width(self):
		return getint(
			self.tk.call('winfo', 'width', self._w))
	def winfo_x(self):
		return getint(
			self.tk.call('winfo', 'x', self._w))
	def winfo_y(self):
		return getint(
			self.tk.call('winfo', 'y', self._w))
	def update(self):
		self.tk.call('update')
	def update_idletasks(self):
		self.tk.call('update', 'idletasks')
	def bindtags(self, tagList=None):
		if tagList is None:
			return self.tk.splitlist(
				self.tk.call('bindtags', self._w))
		else:
			self.tk.call('bindtags', self._w, tagList)
	def _bind(self, what, sequence, func, add, needcleanup=1):
		if type(func) is StringType:
			self.tk.call(what + (sequence, func))
		elif func:
			funcid = self._register(func, self._substitute,
						needcleanup)
			cmd = ('%sif {"[%s %s]" == "break"} break\n'
			       %
			       (add and '+' or '',
				funcid,
				_string.join(self._subst_format)))
			self.tk.call(what + (sequence, cmd))
			return funcid
		elif sequence:
			return self.tk.call(what + (sequence,))
		else:
			return self.tk.splitlist(self.tk.call(what))
	def bind(self, sequence=None, func=None, add=None):
		return self._bind(('bind', self._w), sequence, func, add)
	def unbind(self, sequence, funcid=None):
		self.tk.call('bind', self._w, sequence, '')
		if funcid:
			self.deletecommand(funcid)
	def bind_all(self, sequence=None, func=None, add=None):
		return self._bind(('bind', 'all'), sequence, func, add, 0)
	def unbind_all(self, sequence):
		self.tk.call('bind', 'all' , sequence, '')
	def bind_class(self, className, sequence=None, func=None, add=None):
		return self._bind(('bind', className), sequence, func, add, 0)
	def unbind_class(self, className, sequence):
		self.tk.call('bind', className , sequence, '')
	def mainloop(self, n=0):
		self.tk.mainloop(n)
	def quit(self):
		self.tk.quit()
	def _getints(self, string):
		if string:
			return tuple(map(getint, self.tk.splitlist(string)))
	def _getdoubles(self, string):
		if string:
			return tuple(map(getdouble, self.tk.splitlist(string)))
	def _getboolean(self, string):
		if string:
			return self.tk.getboolean(string)
	def _displayof(self, displayof):
		if displayof:
			return ('-displayof', displayof)
		if displayof is None:
			return ('-displayof', self._w)
		return ()
	def _options(self, cnf, kw = None):
		if kw:
			cnf = _cnfmerge((cnf, kw))
		else:
			cnf = _cnfmerge(cnf)
		res = ()
		for k, v in cnf.items():
			if v is not None:
				if k[-1] == '_': k = k[:-1]
				if callable(v):
					v = self._register(v)
				res = res + ('-'+k, v)
		return res
	def nametowidget(self, name):
		w = self
		if name[0] == '.':
			w = w._root()
			name = name[1:]
		find = _string.find
		while name:
			i = find(name, '.')
			if i >= 0:
				name, tail = name[:i], name[i+1:]
			else:
				tail = ''
			w = w.children[name]
			name = tail
		return w
	_nametowidget = nametowidget
	def _register(self, func, subst=None, needcleanup=1):
		f = CallWrapper(func, subst, self).__call__
		name = `id(f)`
		try:
			func = func.im_func
		except AttributeError:
			pass
		try:
			name = name + func.__name__
		except AttributeError:
			pass
		self.tk.createcommand(name, f)
		if needcleanup:
			if self._tclCommands is None:
				self._tclCommands = []
			self._tclCommands.append(name)
		#print '+ Tkinter created command', name
		return name
	register = _register
	def _root(self):
		w = self
		while w.master: w = w.master
		return w
	_subst_format = ('%#', '%b', '%f', '%h', '%k', 
			 '%s', '%t', '%w', '%x', '%y',
			 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
	def _substitute(self, *args):
		if len(args) != len(self._subst_format): return args
		getboolean = self.tk.getboolean
		getint = int
		nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
		# Missing: (a, c, d, m, o, v, B, R)
		e = Event()
		e.serial = getint(nsign)
		e.num = getint(b)
		try: e.focus = getboolean(f)
		except TclError: pass
		e.height = getint(h)
		e.keycode = getint(k)
		# For Visibility events, event state is a string and
		# not an integer:
		try:
			e.state = getint(s)
		except ValueError:
			e.state = s
		e.time = getint(t)
		e.width = getint(w)
		e.x = getint(x)
		e.y = getint(y)
		e.char = A
		try: e.send_event = getboolean(E)
		except TclError: pass
		e.keysym = K
		e.keysym_num = getint(N)
		e.type = T
		try:
			e.widget = self._nametowidget(W)
		except KeyError:
			e.widget = W
		e.x_root = getint(X)
		e.y_root = getint(Y)
		return (e,)
	def _report_exception(self):
		import sys
		exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
		root = self._root()
		root.report_callback_exception(exc, val, tb)
	# These used to be defined in Widget:
	def configure(self, cnf=None, **kw):
		# XXX ought to generalize this so tag_config etc. can use it
		if kw:
			cnf = _cnfmerge((cnf, kw))
		elif cnf:
			cnf = _cnfmerge(cnf)
		if cnf is None:
			cnf = {}
			for x in self.tk.split(
				self.tk.call(self._w, 'configure')):
				cnf[x[0][1:]] = (x[0][1:],) + x[1:]
			return cnf
		if type(cnf) is StringType:
			x = self.tk.split(self.tk.call(
				self._w, 'configure', '-'+cnf))
			return (x[0][1:],) + x[1:]
		self.tk.call((self._w, 'configure')
		      + self._options(cnf))
	config = configure
	def cget(self, key):
		return self.tk.call(self._w, 'cget', '-' + key)
	__getitem__ = cget
	def __setitem__(self, key, value):
		self.configure({key: value})
	def keys(self):
		return map(lambda x: x[0][1:],
			   self.tk.split(self.tk.call(self._w, 'configure')))
	def __str__(self):
		return self._w
	# Pack methods that apply to the master
	_noarg_ = ['_noarg_']
	def pack_propagate(self, flag=_noarg_):
		if flag is Misc._noarg_:
			return self._getboolean(self.tk.call(
				'pack', 'propagate', self._w))
		else:
			self.tk.call('pack', 'propagate', self._w, flag)
	propagate = pack_propagate
	def pack_slaves(self):
		return map(self._nametowidget,
			   self.tk.splitlist(
				   self.tk.call('pack', 'slaves', self._w)))
	slaves = pack_slaves
	# Place method that applies to the master
	def place_slaves(self):
		return map(self._nametowidget,
			   self.tk.splitlist(
				   self.tk.call(
					   'place', 'slaves', self._w)))
	# Grid methods that apply to the master
	def grid_bbox(self, column=None, row=None, col2=None, row2=None):
		args = ('grid', 'bbox', self._w)
		if column is not None and row is not None:
			args = args + (column, row)
		if col2 is not None and row2 is not None:
			args = args + (col2, row2)
		return self._getints(apply(self.tk.call, args)) or None

	bbox = grid_bbox
	def _grid_configure(self, command, index, cnf, kw):
		if type(cnf) is StringType and not kw:
			if cnf[-1:] == '_':
				cnf = cnf[:-1]
			if cnf[:1] != '-':
				cnf = '-'+cnf
			options = (cnf,)
		else:
			options = self._options(cnf, kw)
		if not options:
			res = self.tk.call('grid',
					   command, self._w, index)
			words = self.tk.splitlist(res)
			dict = {}
			for i in range(0, len(words), 2):
				key = words[i][1:]
				value = words[i+1]
				if not value:
					value = None
				elif '.' in value:
					value = getdouble(value)
				else:
					value = getint(value)
				dict[key] = value
			return dict
		res = self.tk.call(
			      ('grid', command, self._w, index) 
			      + options)
		if len(options) == 1:
			if not res: return None
			# In Tk 7.5, -width can be a float
			if '.' in res: return getdouble(res)
			return getint(res)
	def grid_columnconfigure(self, index, cnf={}, **kw):
		return self._grid_configure('columnconfigure', index, cnf, kw)
	columnconfigure = grid_columnconfigure
	def grid_propagate(self, flag=_noarg_):
		if flag is Misc._noarg_:
			return self._getboolean(self.tk.call(
				'grid', 'propagate', self._w))
		else:
			self.tk.call('grid', 'propagate', self._w, flag)
	def grid_rowconfigure(self, index, cnf={}, **kw):
		return self._grid_configure('rowconfigure', index, cnf, kw)
	rowconfigure = grid_rowconfigure
	def grid_size(self):
		return self._getints(
			self.tk.call('grid', 'size', self._w)) or None
	size = grid_size
	def grid_slaves(self, row=None, column=None):
		args = ()
		if row is not None:
			args = args + ('-row', row)
		if column is not None:
			args = args + ('-column', column)
		return map(self._nametowidget,
			   self.tk.splitlist(self.tk.call(
				   ('grid', 'slaves', self._w) + args)))

	# Support for the "event" command, new in Tk 4.2.
	# By Case Roole.

	def event_add(self, virtual, *sequences):
		args = ('event', 'add', virtual) + sequences
		self.tk.call(args)

	def event_delete(self, virtual, *sequences):
		args = ('event', 'delete', virtual) + sequences
		self.tk.call(args)

	def event_generate(self, sequence, **kw):
		args = ('event', 'generate', self._w, sequence)
		for k, v in kw.items():
			args = args + ('-%s' % k, str(v))
		self.tk.call(args)

	def event_info(self, virtual=None):
		return self.tk.splitlist(
		    self.tk.call('event', 'info', virtual))

	# Image related commands

	def image_names(self):
		return self.tk.call('image', 'names')

	def image_types(self):
		return self.tk.call('image', 'types')


class CallWrapper:
	def __init__(self, func, subst, widget):
		self.func = func
		self.subst = subst
		self.widget = widget
	def __call__(self, *args):
		try:
			if self.subst:
				args = apply(self.subst, args)
			return apply(self.func, args)
		except SystemExit, msg:
			raise SystemExit, msg
		except:
			self.widget._report_exception()


class Wm:
	def wm_aspect(self, 
		   minNumer=None, minDenom=None, 
		   maxNumer=None, maxDenom=None):
		return self._getints(
			self.tk.call('wm', 'aspect', self._w, 
				     minNumer, minDenom, 
				     maxNumer, maxDenom))
	aspect = wm_aspect
	def wm_client(self, name=None):
		return self.tk.call('wm', 'client', self._w, name)
	client = wm_client
	def wm_colormapwindows(self, *wlist):
		if len(wlist) > 1:
			wlist = (wlist,) # Tk needs a list of windows here
		args = ('wm', 'colormapwindows', self._w) + wlist
		return map(self._nametowidget, self.tk.call(args))
	colormapwindows = wm_colormapwindows
	def wm_command(self, value=None):
		return self.tk.call('wm', 'command', self._w, value)
	command = wm_command
	def wm_deiconify(self):
		return self.tk.call('wm', 'deiconify', self._w)
	deiconify = wm_deiconify
	def wm_focusmodel(self, model=None):
		return self.tk.call('wm', 'focusmodel', self._w, model)
	focusmodel = wm_focusmodel
	def wm_frame(self):
		return self.tk.call('wm', 'frame', self._w)
	frame = wm_frame
	def wm_geometry(self, newGeometry=None):
		return self.tk.call('wm', 'geometry', self._w, newGeometry)
	geometry = wm_geometry
	def wm_grid(self,
		 baseWidth=None, baseHeight=None, 
		 widthInc=None, heightInc=None):
		return self._getints(self.tk.call(
			'wm', 'grid', self._w,
			baseWidth, baseHeight, widthInc, heightInc))
	grid = wm_grid
	def wm_group(self, pathName=None):
		return self.tk.call('wm', 'group', self._w, pathName)
	group = wm_group
	def wm_iconbitmap(self, bitmap=None):
		return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
	iconbitmap = wm_iconbitmap
	def wm_iconify(self):
		return self.tk.call('wm', 'iconify', self._w)
	iconify = wm_iconify
	def wm_iconmask(self, bitmap=None):
		return self.tk.call('wm', 'iconmask', self._w, bitmap)
	iconmask = wm_iconmask
	def wm_iconname(self, newName=None):
		return self.tk.call('wm', 'iconname', self._w, newName)
	iconname = wm_iconname
	def wm_iconposition(self, x=None, y=None):
		return self._getints(self.tk.call(
			'wm', 'iconposition', self._w, x, y))
	iconposition = wm_iconposition
	def wm_iconwindow(self, pathName=None):
		return self.tk.call('wm', 'iconwindow', self._w, pathName)
	iconwindow = wm_iconwindow
	def wm_maxsize(self, width=None, height=None):
		return self._getints(self.tk.call(
			'wm', 'maxsize', self._w, width, height))
	maxsize = wm_maxsize
	def wm_minsize(self, width=None, height=None):
		return self._getints(self.tk.call(
			'wm', 'minsize', self._w, width, height))
	minsize = wm_minsize
	def wm_overrideredirect(self, boolean=None):
		return self._getboolean(self.tk.call(
			'wm', 'overrideredirect', self._w, boolean))
	overrideredirect = wm_overrideredirect
	def wm_positionfrom(self, who=None):
		return self.tk.call('wm', 'positionfrom', self._w, who)
	positionfrom = wm_positionfrom
	def wm_protocol(self, name=None, func=None):
		if callable(func):
			command = self._register(func)
		else:
			command = func
		return self.tk.call(
			'wm', 'protocol', self._w, name, command)
	protocol = wm_protocol
	def wm_resizable(self, width=None, height=None):
		return self.tk.call('wm', 'resizable', self._w, width, height)
	resizable = wm_resizable
	def wm_sizefrom(self, who=None):
		return self.tk.call('wm', 'sizefrom', self._w, who)
	sizefrom = wm_sizefrom
	def wm_state(self):
		return self.tk.call('wm', 'state', self._w)
	state = wm_state
	def wm_title(self, string=None):
		return self.tk.call('wm', 'title', self._w, string)
	title = wm_title
	def wm_transient(self, master=None):
		return self.tk.call('wm', 'transient', self._w, master)
	transient = wm_transient
	def wm_withdraw(self):
		return self.tk.call('wm', 'withdraw', self._w)
	withdraw = wm_withdraw


class Tk(Misc, Wm):
	_w = '.'
	def __init__(self, screenName=None, baseName=None, className='Tk'):
		global _default_root
		self.master = None
		self.children = {}
		if baseName is None:
			import sys, os
			baseName = os.path.basename(sys.argv[0])
			baseName, ext = os.path.splitext(baseName)
			if ext not in ('.py', '.pyc', '.pyo'):
				baseName = baseName + ext
		self.tk = _tkinter.create(screenName, baseName, className)
		if _MacOS:
			# Disable event scanning except for Command-Period
			_MacOS.SchedParams(1, 0)
			# Work around nasty MacTk bug
			# XXX Is this one still needed?
			self.update()
		# Version sanity checks
		tk_version = self.tk.getvar('tk_version')
		if tk_version != _tkinter.TK_VERSION:
		    raise RuntimeError, \
		    "tk.h version (%s) doesn't match libtk.a version (%s)" \
		    % (_tkinter.TK_VERSION, tk_version)
		tcl_version = self.tk.getvar('tcl_version')
		if tcl_version != _tkinter.TCL_VERSION:
		    raise RuntimeError, \
		    "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
		    % (_tkinter.TCL_VERSION, tcl_version)
		if TkVersion < 4.0:
			raise RuntimeError, \
			"Tk 4.0 or higher is required; found Tk %s" \
			% str(TkVersion)
		self.tk.createcommand('tkerror', _tkerror)
		self.tk.createcommand('exit', _exit)
		self.readprofile(baseName, className)
		if _support_default_root and not _default_root:
			_default_root = self
		self.protocol("WM_DELETE_WINDOW", self.destroy)
	def destroy(self):
		for c in self.children.values(): c.destroy()
		self.tk.call('destroy', self._w)
		Misc.destroy(self)
		global _default_root
		if _support_default_root and _default_root is self:
			_default_root = None
	def readprofile(self, baseName, className):
		import os
		if os.environ.has_key('HOME'): home = os.environ['HOME']
		else: home = os.curdir
		class_tcl = os.path.join(home, '.%s.tcl' % className)
		class_py = os.path.join(home, '.%s.py' % className)
		base_tcl = os.path.join(home, '.%s.tcl' % baseName)
		base_py = os.path.join(home, '.%s.py' % baseName)
		dir = {'self': self}
		exec 'from Tkinter import *' in dir
		if os.path.isfile(class_tcl):
			print 'source', `class_tcl`
			self.tk.call('source', class_tcl)
		if os.path.isfile(class_py):
			print 'execfile', `class_py`
			execfile(class_py, dir)
		if os.path.isfile(base_tcl):
			print 'source', `base_tcl`
			self.tk.call('source', base_tcl)
		if os.path.isfile(base_py):
			print 'execfile', `base_py`
			execfile(base_py, dir)
	def report_callback_exception(self, exc, val, tb):
		import traceback, sys
		sys.stderr.write("Exception in Tkinter callback\n")
		sys.last_type = exc
		sys.last_value = val
		sys.last_traceback = tb
		traceback.print_exception(exc, val, tb)

# Ideally, the classes Pack, Place and Grid disappear, the
# pack/place/grid methods are defined on the Widget class, and
# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
# ...), with pack(), place() and grid() being short for
# pack_configure(), place_configure() and grid_columnconfigure(), and
# forget() being short for pack_forget().  As a practical matter, I'm
# afraid that there is too much code out there that may be using the
# Pack, Place or Grid class, so I leave them intact -- but only as
# backwards compatibility features.  Also note that those methods that
# take a master as argument (e.g. pack_propagate) have been moved to
# the Misc class (which now incorporates all methods common between
# toplevel and interior widgets).  Again, for compatibility, these are
# copied into the Pack, Place or Grid class.

class Pack:
	def pack_configure(self, cnf={}, **kw):
		self.tk.call(
		      ('pack', 'configure', self._w) 
		      + self._options(cnf, kw))
	pack = configure = config = pack_configure
	def pack_forget(self):
		self.tk.call('pack', 'forget', self._w)
	forget = pack_forget
	def pack_info(self):
		words = self.tk.splitlist(
			self.tk.call('pack', 'info', self._w))
		dict = {}
		for i in range(0, len(words), 2):
			key = words[i][1:]
			value = words[i+1]
			if value[:1] == '.':
				value = self._nametowidget(value)
			dict[key] = value
		return dict
	info = pack_info
	propagate = pack_propagate = Misc.pack_propagate
	slaves = pack_slaves = Misc.pack_slaves

class Place:
	def place_configure(self, cnf={}, **kw):
		for k in ['in_']:
			if kw.has_key(k):
				kw[k[:-1]] = kw[k]
				del kw[k]
		self.tk.call(
		      ('place', 'configure', self._w) 
		      + self._options(cnf, kw))
	place = configure = config = place_configure
	def place_forget(self):
		self.tk.call('place', 'forget', self._w)
	forget = place_forget
	def place_info(self):
		words = self.tk.splitlist(
			self.tk.call('place', 'info', self._w))
		dict = {}
		for i in range(0, len(words), 2):
			key = words[i][1:]
			value = words[i+1]
			if value[:1] == '.':
				value = self._nametowidget(value)
			dict[key] = value
		return dict
	info = place_info
	slaves = place_slaves = Misc.place_slaves

class Grid:
	# Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
	def grid_configure(self, cnf={}, **kw):
		self.tk.call(
		      ('grid', 'configure', self._w) 
		      + self._options(cnf, kw))
	grid = configure = config = grid_configure
	bbox = grid_bbox = Misc.grid_bbox
	columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
	def grid_forget(self):
		self.tk.call('grid', 'forget', self._w)
	forget = grid_forget
	def grid_remove(self):
		self.tk.call('grid', 'remove', self._w)
	def grid_info(self):
		words = self.tk.splitlist(
			self.tk.call('grid', 'info', self._w))
		dict = {}
		for i in range(0, len(words), 2):
			key = words[i][1:]
			value = words[i+1]
			if value[:1] == '.':
				value = self._nametowidget(value)
			dict[key] = value
		return dict
	info = grid_info
	def grid_location(self, x, y):
		return self._getints(
			self.tk.call(
				'grid', 'location', self._w, x, y)) or None
	location = grid_location
	propagate = grid_propagate = Misc.grid_propagate
	rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
	size = grid_size = Misc.grid_size
	slaves = grid_slaves = Misc.grid_slaves

class BaseWidget(Misc):
	def _setup(self, master, cnf):
		if _support_default_root:
			global _default_root
			if not master:
				if not _default_root:
					_default_root = Tk()
				master = _default_root
		self.master = master
		self.tk = master.tk
		name = None
		if cnf.has_key('name'):
			name = cnf['name']
			del cnf['name']
		if not name:
			name = `id(self)`
		self._name = name
		if master._w=='.':
			self._w = '.' + name
		else:
			self._w = master._w + '.' + name
		self.children = {}
		if self.master.children.has_key(self._name):
			self.master.children[self._name].destroy()
		self.master.children[self._name] = self
	def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
		if kw:
			cnf = _cnfmerge((cnf, kw))
		self.widgetName = widgetName
		BaseWidget._setup(self, master, cnf)
		classes = []
		for k in cnf.keys():
			if type(k) is ClassType:
				classes.append((k, cnf[k]))
				del cnf[k]
		self.tk.call(
			(widgetName, self._w) + extra + self._options(cnf))
		for k, v in classes:
			k.configure(self, v)
	def destroy(self):
		for c in self.children.values(): c.destroy()
		if self.master.children.has_key(self._name):
			del self.master.children[self._name]
		self.tk.call('destroy', self._w)
		Misc.destroy(self)
	def _do(self, name, args=()):
		# XXX Obsolete -- better use self.tk.call directly!
		return self.tk.call((self._w, name) + args)

class Widget(BaseWidget, Pack, Place, Grid):
	pass

class Toplevel(BaseWidget, Wm):
	def __init__(self, master=None, cnf={}, **kw):
		if kw:
			cnf = _cnfmerge((cnf, kw))
		extra = ()
		for wmkey in ['screen', 'class_', 'class', 'visual',
			      'colormap']:
			if cnf.has_key(wmkey):
				val = cnf[wmkey]
				# TBD: a hack needed because some keys
				# are not valid as keyword arguments
				if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
				else: opt = '-'+wmkey
				extra = extra + (opt, val)
				del cnf[wmkey]
		BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
		root = self._root()
		self.iconname(root.iconname())
		self.title(root.title())
		self.protocol("WM_DELETE_WINDOW", self.destroy)

class Button(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'button', cnf, kw)
	def tkButtonEnter(self, *dummy):
		self.tk.call('tkButtonEnter', self._w)
	def tkButtonLeave(self, *dummy):
		self.tk.call('tkButtonLeave', self._w)
	def tkButtonDown(self, *dummy):
		self.tk.call('tkButtonDown', self._w)
	def tkButtonUp(self, *dummy):
		self.tk.call('tkButtonUp', self._w)
	def tkButtonInvoke(self, *dummy):
		self.tk.call('tkButtonInvoke', self._w)
	def flash(self):
		self.tk.call(self._w, 'flash')
	def invoke(self):
		return self.tk.call(self._w, 'invoke')

# Indices:
# XXX I don't like these -- take them away
def AtEnd():
	return 'end'
def AtInsert(*args):
	s = 'insert'
	for a in args:
		if a: s = s + (' ' + a)
	return s
def AtSelFirst():
	return 'sel.first'
def AtSelLast():
	return 'sel.last'
def At(x, y=None):
	if y is None:
		return '@' + `x`		
	else:
		return '@' + `x` + ',' + `y`

class Canvas(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'canvas', cnf, kw)
	def addtag(self, *args):
		self.tk.call((self._w, 'addtag') + args)
	def addtag_above(self, newtag, tagOrId):
		self.addtag(newtag, 'above', tagOrId)
	def addtag_all(self, newtag):
		self.addtag(newtag, 'all')
	def addtag_below(self, newtag, tagOrId):
		self.addtag(newtag, 'below', tagOrId)
	def addtag_closest(self, newtag, x, y, halo=None, start=None):
		self.addtag(newtag, 'closest', x, y, halo, start)
	def addtag_enclosed(self, newtag, x1, y1, x2, y2):
		self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
	def addtag_overlapping(self, newtag, x1, y1, x2, y2):
		self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
	def addtag_withtag(self, newtag, tagOrId):
		self.addtag(newtag, 'withtag', tagOrId)
	def bbox(self, *args):
		return self._getints(
			self.tk.call((self._w, 'bbox') + args)) or None
	def tag_unbind(self, tagOrId, sequence, funcid=None):
		self.tk.call(self._w, 'bind', tagOrId, sequence, '')
		if funcid:
			self.deletecommand(funcid)
	def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
		return self._bind((self._w, 'bind', tagOrId),
				  sequence, func, add)
	def canvasx(self, screenx, gridspacing=None):
		return getdouble(self.tk.call(
			self._w, 'canvasx', screenx, gridspacing))
	def canvasy(self, screeny, gridspacing=None):
		return getdouble(self.tk.call(
			self._w, 'canvasy', screeny, gridspacing))
	def coords(self, *args):
		# XXX Should use _flatten on args
		return map(getdouble,
                           self.tk.splitlist(
				   self.tk.call((self._w, 'coords') + args)))
	def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
		args = _flatten(args)
		cnf = args[-1]
		if type(cnf) in (DictionaryType, TupleType):
			args = args[:-1]
		else:
			cnf = {}
		return getint(apply(
			self.tk.call,
			(self._w, 'create', itemType) 
			+ args + self._options(cnf, kw)))
	def create_arc(self, *args, **kw):
		return self._create('arc', args, kw)
	def create_bitmap(self, *args, **kw):
		return self._create('bitmap', args, kw)
	def create_image(self, *args, **kw):
		return self._create('image', args, kw)
	def create_line(self, *args, **kw):
		return self._create('line', args, kw)
	def create_oval(self, *args, **kw):
		return self._create('oval', args, kw)
	def create_polygon(self, *args, **kw):
		return self._create('polygon', args, kw)
	def create_rectangle(self, *args, **kw):
		return self._create('rectangle', args, kw)
	def create_text(self, *args, **kw):
		return self._create('text', args, kw)
	def create_window(self, *args, **kw):
		return self._create('window', args, kw)
	def dchars(self, *args):
		self.tk.call((self._w, 'dchars') + args)
	def delete(self, *args):
		self.tk.call((self._w, 'delete') + args)
	def dtag(self, *args):
		self.tk.call((self._w, 'dtag') + args)
	def find(self, *args):
		return self._getints(
			self.tk.call((self._w, 'find') + args)) or ()
	def find_above(self, tagOrId):
		return self.find('above', tagOrId)
	def find_all(self):
		return self.find('all')
	def find_below(self, tagOrId):
		return self.find('below', tagOrId)
	def find_closest(self, x, y, halo=None, start=None):
		return self.find('closest', x, y, halo, start)
	def find_enclosed(self, x1, y1, x2, y2):
		return self.find('enclosed', x1, y1, x2, y2)
	def find_overlapping(self, x1, y1, x2, y2):
		return self.find('overlapping', x1, y1, x2, y2)
	def find_withtag(self, tagOrId):
		return self.find('withtag', tagOrId)
	def focus(self, *args):
		return self.tk.call((self._w, 'focus') + args)
	def gettags(self, *args):
		return self.tk.splitlist(
			self.tk.call((self._w, 'gettags') + args))
	def icursor(self, *args):
		self.tk.call((self._w, 'icursor') + args)
	def index(self, *args):
		return getint(self.tk.call((self._w, 'index') + args))
	def insert(self, *args):
		self.tk.call((self._w, 'insert') + args)
	def itemcget(self, tagOrId, option):
		return self.tk.call(
			(self._w, 'itemcget') + (tagOrId, '-'+option))
	def itemconfigure(self, tagOrId, cnf=None, **kw):
		if cnf is None and not kw:
			cnf = {}
			for x in self.tk.split(
				self.tk.call(self._w,
					     'itemconfigure', tagOrId)):
				cnf[x[0][1:]] = (x[0][1:],) + x[1:]
			return cnf
		if type(cnf) == StringType and not kw:
			x = self.tk.split(self.tk.call(
				self._w, 'itemconfigure', tagOrId, '-'+cnf))
			return (x[0][1:],) + x[1:]
		self.tk.call((self._w, 'itemconfigure', tagOrId) +
			     self._options(cnf, kw))
	itemconfig = itemconfigure
	# lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
	# so the preferred name for them is tag_lower, tag_raise
	# (similar to tag_bind, and similar to the Text widget);
	# unfortunately can't delete the old ones yet (maybe in 1.6)
	def tag_lower(self, *args):
		self.tk.call((self._w, 'lower') + args)
	lower = tag_lower
	def move(self, *args):
		self.tk.call((self._w, 'move') + args)
	def postscript(self, cnf={}, **kw):
		return self.tk.call((self._w, 'postscript') +
				    self._options(cnf, kw))
	def tag_raise(self, *args):
		self.tk.call((self._w, 'raise') + args)
	lift = tkraise = tag_raise
	def scale(self, *args):
		self.tk.call((self._w, 'scale') + args)
	def scan_mark(self, x, y):
		self.tk.call(self._w, 'scan', 'mark', x, y)
	def scan_dragto(self, x, y):
		self.tk.call(self._w, 'scan', 'dragto', x, y)
	def select_adjust(self, tagOrId, index):
		self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
	def select_clear(self):
		self.tk.call(self._w, 'select', 'clear')
	def select_from(self, tagOrId, index):
		self.tk.call(self._w, 'select', 'from', tagOrId, index)
	def select_item(self):
		self.tk.call(self._w, 'select', 'item')
	def select_to(self, tagOrId, index):
		self.tk.call(self._w, 'select', 'to', tagOrId, index)
	def type(self, tagOrId):
		return self.tk.call(self._w, 'type', tagOrId) or None
	def xview(self, *args):
		if not args:
			return self._getdoubles(self.tk.call(self._w, 'xview'))
		self.tk.call((self._w, 'xview') + args)
	def xview_moveto(self, fraction):
		self.tk.call(self._w, 'xview', 'moveto', fraction)
	def xview_scroll(self, number, what):
		self.tk.call(self._w, 'xview', 'scroll', number, what)
	def yview(self, *args):
		if not args:
			return self._getdoubles(self.tk.call(self._w, 'yview'))
		self.tk.call((self._w, 'yview') + args)
	def yview_moveto(self, fraction):
		self.tk.call(self._w, 'yview', 'moveto', fraction)
	def yview_scroll(self, number, what):
		self.tk.call(self._w, 'yview', 'scroll', number, what)

class Checkbutton(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'checkbutton', cnf, kw)
	def deselect(self):
		self.tk.call(self._w, 'deselect')
	def flash(self):
		self.tk.call(self._w, 'flash')
	def invoke(self):
		return self.tk.call(self._w, 'invoke')
	def select(self):
		self.tk.call(self._w, 'select')
	def toggle(self):
		self.tk.call(self._w, 'toggle')

class Entry(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'entry', cnf, kw)
	def delete(self, first, last=None):
		self.tk.call(self._w, 'delete', first, last)
	def get(self):
		return self.tk.call(self._w, 'get')
	def icursor(self, index):
		self.tk.call(self._w, 'icursor', index)
	def index(self, index):
		return getint(self.tk.call(
			self._w, 'index', index))
	def insert(self, index, string):
		self.tk.call(self._w, 'insert', index, string)
	def scan_mark(self, x):
		self.tk.call(self._w, 'scan', 'mark', x)
	def scan_dragto(self, x):
		self.tk.call(self._w, 'scan', 'dragto', x)
	def selection_adjust(self, index):
		self.tk.call(self._w, 'selection', 'adjust', index)
	select_adjust = selection_adjust
	def selection_clear(self):
		self.tk.call(self._w, 'selection', 'clear')
	select_clear = selection_clear
	def selection_from(self, index):
		self.tk.call(self._w, 'selection', 'from', index)
	select_from = selection_from
	def selection_present(self):
		return self.tk.getboolean(
			self.tk.call(self._w, 'selection', 'present'))
	select_present = selection_present
	def selection_range(self, start, end):
		self.tk.call(self._w, 'selection', 'range', start, end)
	select_range = selection_range
	def selection_to(self, index):
		self.tk.call(self._w, 'selection', 'to', index)
	select_to = selection_to
	def xview(self, index):
		self.tk.call(self._w, 'xview', index)
	def xview_moveto(self, fraction):
		self.tk.call(self._w, 'xview', 'moveto', fraction)
	def xview_scroll(self, number, what):
		self.tk.call(self._w, 'xview', 'scroll', number, what)

class Frame(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		cnf = _cnfmerge((cnf, kw))
		extra = ()
		if cnf.has_key('class_'):
			extra = ('-class', cnf['class_'])
			del cnf['class_']
		elif cnf.has_key('class'):
			extra = ('-class', cnf['class'])
			del cnf['class']
		Widget.__init__(self, master, 'frame', cnf, {}, extra)

class Label(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'label', cnf, kw)

class Listbox(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'listbox', cnf, kw)
	def activate(self, index):
		self.tk.call(self._w, 'activate', index)
	def bbox(self, *args):
		return self._getints(
			self.tk.call((self._w, 'bbox') + args)) or None
	def curselection(self):
		# XXX Ought to apply self._getints()...
		return self.tk.splitlist(self.tk.call(
			self._w, 'curselection'))
	def delete(self, first, last=None):
		self.tk.call(self._w, 'delete', first, last)
	def get(self, first, last=None):
		if last:
			return self.tk.splitlist(self.tk.call(
				self._w, 'get', first, last))
		else:
			return self.tk.call(self._w, 'get', first)
	def index(self, index):
		i = self.tk.call(self._w, 'index', index)
		if i == 'none': return None
		return getint(i)
	def insert(self, index, *elements):
		self.tk.call((self._w, 'insert', index) + elements)
	def nearest(self, y):
		return getint(self.tk.call(
			self._w, 'nearest', y))
	def scan_mark(self, x, y):
		self.tk.call(self._w, 'scan', 'mark', x, y)
	def scan_dragto(self, x, y):
		self.tk.call(self._w, 'scan', 'dragto', x, y)
	def see(self, index):
		self.tk.call(self._w, 'see', index)
	def selection_anchor(self, index):
		self.tk.call(self._w, 'selection', 'anchor', index)
	select_anchor = selection_anchor
	def selection_clear(self, first, last=None):
		self.tk.call(self._w,
			     'selection', 'clear', first, last)
	select_clear = selection_clear
	def selection_includes(self, index):
		return self.tk.getboolean(self.tk.call(
			self._w, 'selection', 'includes', index))
	select_includes = selection_includes
	def selection_set(self, first, last=None):
		self.tk.call(self._w, 'selection', 'set', first, last)
	select_set = selection_set
	def size(self):
		return getint(self.tk.call(self._w, 'size'))
	def xview(self, *what):
		if not what:
			return self._getdoubles(self.tk.call(self._w, 'xview'))
		self.tk.call((self._w, 'xview') + what)
	def xview_moveto(self, fraction):
		self.tk.call(self._w, 'xview', 'moveto', fraction)
	def xview_scroll(self, number, what):
		self.tk.call(self._w, 'xview', 'scroll', number, what)
	def yview(self, *what):
		if not what:
			return self._getdoubles(self.tk.call(self._w, 'yview'))
		self.tk.call((self._w, 'yview') + what)
	def yview_moveto(self, fraction):
		self.tk.call(self._w, 'yview', 'moveto', fraction)
	def yview_scroll(self, number, what):
		self.tk.call(self._w, 'yview', 'scroll', number, what)

class Menu(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'menu', cnf, kw)
	def tk_bindForTraversal(self):
		pass # obsolete since Tk 4.0
	def tk_mbPost(self):
		self.tk.call('tk_mbPost', self._w)
	def tk_mbUnpost(self):
		self.tk.call('tk_mbUnpost')
	def tk_traverseToMenu(self, char):
		self.tk.call('tk_traverseToMenu', self._w, char)
	def tk_traverseWithinMenu(self, char):
		self.tk.call('tk_traverseWithinMenu', self._w, char)
	def tk_getMenuButtons(self):
		return self.tk.call('tk_getMenuButtons', self._w)
	def tk_nextMenu(self, count):
		self.tk.call('tk_nextMenu', count)
	def tk_nextMenuEntry(self, count):
		self.tk.call('tk_nextMenuEntry', count)
	def tk_invokeMenu(self):
		self.tk.call('tk_invokeMenu', self._w)
	def tk_firstMenu(self):
		self.tk.call('tk_firstMenu', self._w)
	def tk_mbButtonDown(self):
		self.tk.call('tk_mbButtonDown', self._w)
	def tk_popup(self, x, y, entry=""):
		self.tk.call('tk_popup', self._w, x, y, entry)
	def activate(self, index):
		self.tk.call(self._w, 'activate', index)
	def add(self, itemType, cnf={}, **kw):
		self.tk.call((self._w, 'add', itemType) +
			     self._options(cnf, kw))
	def add_cascade(self, cnf={}, **kw):
		self.add('cascade', cnf or kw)
	def add_checkbutton(self, cnf={}, **kw):
		self.add('checkbutton', cnf or kw)
	def add_command(self, cnf={}, **kw):
		self.add('command', cnf or kw)
	def add_radiobutton(self, cnf={}, **kw):
		self.add('radiobutton', cnf or kw)
	def add_separator(self, cnf={}, **kw):
		self.add('separator', cnf or kw)
	def insert(self, index, itemType, cnf={}, **kw):
		self.tk.call((self._w, 'insert', index, itemType) +
			     self._options(cnf, kw))
	def insert_cascade(self, index, cnf={}, **kw):
		self.insert(index, 'cascade', cnf or kw)
	def insert_checkbutton(self, index, cnf={}, **kw):
		self.insert(index, 'checkbutton', cnf or kw)
	def insert_command(self, index, cnf={}, **kw):
		self.insert(index, 'command', cnf or kw)
	def insert_radiobutton(self, index, cnf={}, **kw):
		self.insert(index, 'radiobutton', cnf or kw)
	def insert_separator(self, index, cnf={}, **kw):
		self.insert(index, 'separator', cnf or kw)
	def delete(self, index1, index2=None):
		self.tk.call(self._w, 'delete', index1, index2)
	def entrycget(self, index, option):
		return self.tk.call(self._w, 'entrycget', index, '-' + option)
	def entryconfigure(self, index, cnf=None, **kw):
		if cnf is None and not kw:
			cnf = {}
			for x in self.tk.split(self.tk.call(
				(self._w, 'entryconfigure', index))):
				cnf[x[0][1:]] = (x[0][1:],) + x[1:]
			return cnf
		if type(cnf) == StringType and not kw:
			x = self.tk.split(self.tk.call(
				(self._w, 'entryconfigure', index, '-'+cnf)))
			return (x[0][1:],) + x[1:]
		self.tk.call((self._w, 'entryconfigure', index)
		      + self._options(cnf, kw))
	entryconfig = entryconfigure
	def index(self, index):
		i = self.tk.call(self._w, 'index', index)
		if i == 'none': return None
		return getint(i)
	def invoke(self, index):
		return self.tk.call(self._w, 'invoke', index)
	def post(self, x, y):
		self.tk.call(self._w, 'post', x, y)
	def type(self, index):
		return self.tk.call(self._w, 'type', index)
	def unpost(self):
		self.tk.call(self._w, 'unpost')
	def yposition(self, index):
		return getint(self.tk.call(
			self._w, 'yposition', index))

class Menubutton(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'menubutton', cnf, kw)

class Message(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'message', cnf, kw)

class Radiobutton(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'radiobutton', cnf, kw)
	def deselect(self):
		self.tk.call(self._w, 'deselect')
	def flash(self):
		self.tk.call(self._w, 'flash')
	def invoke(self):
		return self.tk.call(self._w, 'invoke')
	def select(self):
		self.tk.call(self._w, 'select')

class Scale(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'scale', cnf, kw)
	def get(self):
		value = self.tk.call(self._w, 'get')
		try:
			return getint(value)
		except ValueError:
			return getdouble(value)
	def set(self, value):
		self.tk.call(self._w, 'set', value)
	def coords(self, value=None):
		return self._getints(self.tk.call(self._w, 'coords', value))
	def identify(self, x, y):
		return self.tk.call(self._w, 'identify', x, y)

class Scrollbar(Widget):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'scrollbar', cnf, kw)
	def activate(self, index):
		self.tk.call(self._w, 'activate', index)
	def delta(self, deltax, deltay):
		return getdouble(
			self.tk.call(self._w, 'delta', deltax, deltay))
	def fraction(self, x, y):
		return getdouble(self.tk.call(self._w, 'fraction', x, y))
	def identify(self, x, y):
		return self.tk.call(self._w, 'identify', x, y)
	def get(self):
		return self._getdoubles(self.tk.call(self._w, 'get'))
	def set(self, *args):
		self.tk.call((self._w, 'set') + args)

class Text(Widget):
	# XXX Add dump()
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'text', cnf, kw)
	def bbox(self, *args):
		return self._getints(
			self.tk.call((self._w, 'bbox') + args)) or None
	def tk_textSelectTo(self, index):
		self.tk.call('tk_textSelectTo', self._w, index)
	def tk_textBackspace(self):
		self.tk.call('tk_textBackspace', self._w)
	def tk_textIndexCloser(self, a, b, c):
		self.tk.call('tk_textIndexCloser', self._w, a, b, c)
	def tk_textResetAnchor(self, index):
		self.tk.call('tk_textResetAnchor', self._w, index)
	def compare(self, index1, op, index2):
		return self.tk.getboolean(self.tk.call(
			self._w, 'compare', index1, op, index2))
	def debug(self, boolean=None):
		return self.tk.getboolean(self.tk.call(
			self._w, 'debug', boolean))
	def delete(self, index1, index2=None):
		self.tk.call(self._w, 'delete', index1, index2)
	def dlineinfo(self, index):
		return self._getints(self.tk.call(self._w, 'dlineinfo', index))
	def get(self, index1, index2=None):
		return self.tk.call(self._w, 'get', index1, index2)
	# (Image commands are new in 8.0)
	def image_cget(self, index, option):
		if option[:1] != "-":
			option = "-" + option
		if option[-1:] == "_":
			option = option[:-1]
		return self.tk.call(self._w, "image", "cget", index, option)
	def image_configure(self, index, cnf={}, **kw):
		if not cnf and not kw:
			cnf = {}
			for x in self.tk.split(
				    self.tk.call(
					self._w, "image", "configure", index)):
				cnf[x[0][1:]] = (x[0][1:],) + x[1:]
			return cnf
		apply(self.tk.call,
		      (self._w, "image", "configure", index)
		      + self._options(cnf, kw))
	def image_create(self, index, cnf={}, **kw):
		return apply(self.tk.call,
			     (self._w, "image", "create", index)
			     + self._options(cnf, kw))
	def image_names(self):
		return self.tk.call(self._w, "image", "names")
	def index(self, index):
		return self.tk.call(self._w, 'index', index)
	def insert(self, index, chars, *args):
		self.tk.call((self._w, 'insert', index, chars) + args)
	def mark_gravity(self, markName, direction=None):
		return self.tk.call(
			(self._w, 'mark', 'gravity', markName, direction))
	def mark_names(self):
		return self.tk.splitlist(self.tk.call(
			self._w, 'mark', 'names'))
	def mark_set(self, markName, index):
		self.tk.call(self._w, 'mark', 'set', markName, index)
	def mark_unset(self, *markNames):
		self.tk.call((self._w, 'mark', 'unset') + markNames)
	def mark_next(self, index):
		return self.tk.call(self._w, 'mark', 'next', index) or None
	def mark_previous(self, index):
		return self.tk.call(self._w, 'mark', 'previous', index) or None
	def scan_mark(self, x, y):
		self.tk.call(self._w, 'scan', 'mark', x, y)
	def scan_dragto(self, x, y):
		self.tk.call(self._w, 'scan', 'dragto', x, y)
	def search(self, pattern, index, stopindex=None,
		   forwards=None, backwards=None, exact=None,
		   regexp=None, nocase=None, count=None):
		args = [self._w, 'search']
		if forwards: args.append('-forwards')
		if backwards: args.append('-backwards')
		if exact: args.append('-exact')
		if regexp: args.append('-regexp')
		if nocase: args.append('-nocase')
		if count: args.append('-count'); args.append(count)
		if pattern[0] == '-': args.append('--')
		args.append(pattern)
		args.append(index)
		if stopindex: args.append(stopindex)
		return self.tk.call(tuple(args))
	def see(self, index):
		self.tk.call(self._w, 'see', index)
	def tag_add(self, tagName, index1, *args):
		self.tk.call(
			(self._w, 'tag', 'add', tagName, index1) + args)
	def tag_unbind(self, tagName, sequence, funcid=None):
		self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
		if funcid:
			self.deletecommand(funcid)
	def tag_bind(self, tagName, sequence, func, add=None):
		return self._bind((self._w, 'tag', 'bind', tagName),
				  sequence, func, add)
	def tag_cget(self, tagName, option):
		if option[:1] != '-':
			option = '-' + option
		if option[-1:] == '_':
			option = option[:-1]
		return self.tk.call(self._w, 'tag', 'cget', tagName, option)
	def tag_configure(self, tagName, cnf={}, **kw):
		if type(cnf) == StringType:
			x = self.tk.split(self.tk.call(
				self._w, 'tag', 'configure', tagName, '-'+cnf))
			return (x[0][1:],) + x[1:]
		self.tk.call(
		      (self._w, 'tag', 'configure', tagName)
		      + self._options(cnf, kw))
	tag_config = tag_configure
	def tag_delete(self, *tagNames):
		self.tk.call((self._w, 'tag', 'delete') + tagNames)
	def tag_lower(self, tagName, belowThis=None):
		self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
	def tag_names(self, index=None):
		return self.tk.splitlist(
			self.tk.call(self._w, 'tag', 'names', index))
	def tag_nextrange(self, tagName, index1, index2=None):
		return self.tk.splitlist(self.tk.call(
			self._w, 'tag', 'nextrange', tagName, index1, index2))
	def tag_prevrange(self, tagName, index1, index2=None):
		return self.tk.splitlist(self.tk.call(
			self._w, 'tag', 'prevrange', tagName, index1, index2))
	def tag_raise(self, tagName, aboveThis=None):
		self.tk.call(
			self._w, 'tag', 'raise', tagName, aboveThis)
	def tag_ranges(self, tagName):
		return self.tk.splitlist(self.tk.call(
			self._w, 'tag', 'ranges', tagName))
	def tag_remove(self, tagName, index1, index2=None):
		self.tk.call(
			self._w, 'tag', 'remove', tagName, index1, index2)
	def window_cget(self, index, option):
		if option[:1] != '-':
			option = '-' + option
		if option[-1:] == '_':
			option = option[:-1]
		return self.tk.call(self._w, 'window', 'cget', index, option)
	def window_configure(self, index, cnf={}, **kw):
		if type(cnf) == StringType:
			x = self.tk.split(self.tk.call(
				self._w, 'window', 'configure',
				index, '-'+cnf))
			return (x[0][1:],) + x[1:]
		self.tk.call(
		      (self._w, 'window', 'configure', index)
		      + self._options(cnf, kw))
	window_config = window_configure
	def window_create(self, index, cnf={}, **kw):
		self.tk.call(
		      (self._w, 'window', 'create', index)
		      + self._options(cnf, kw))
	def window_names(self):
		return self.tk.splitlist(
			self.tk.call(self._w, 'window', 'names'))
	def xview(self, *what):
		if not what:
			return self._getdoubles(self.tk.call(self._w, 'xview'))
		self.tk.call((self._w, 'xview') + what)
	def yview(self, *what):
		if not what:
			return self._getdoubles(self.tk.call(self._w, 'yview'))
		self.tk.call((self._w, 'yview') + what)
	def yview_pickplace(self, *what):
		self.tk.call((self._w, 'yview', '-pickplace') + what)

class _setit:
	def __init__(self, var, value, callback=None):
		self.__value = value
		self.__var = var
		self.__callback = callback
	def __call__(self, *args):
		self.__var.set(self.__value)
		if self.__callback:
			apply(self.__callback, (self.__value,)+args)

class OptionMenu(Menubutton):
	def __init__(self, master, variable, value, *values, **kwargs):
		kw = {"borderwidth": 2, "textvariable": variable,
		      "indicatoron": 1, "relief": RAISED, "anchor": "c",
		      "highlightthickness": 2}
		Widget.__init__(self, master, "menubutton", kw)
		self.widgetName = 'tk_optionMenu'
		menu = self.__menu = Menu(self, name="menu", tearoff=0)
		self.menuname = menu._w
		# 'command' is the only supported keyword
		callback = kwargs.get('command')
		if kwargs.has_key('command'):
			del kwargs['command']
		if kwargs:
			raise TclError, 'unknown option -'+kwargs.keys()[0]
		menu.add_command(label=value,
				 command=_setit(variable, value, callback))
		for v in values:
			menu.add_command(label=v,
					 command=_setit(variable, v, callback))
		self["menu"] = menu

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

	def destroy(self):
		Menubutton.destroy(self)
		self.__menu = None

class Image:
	def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
		self.name = None
		if not master:
			master = _default_root
			if not master:
				raise RuntimeError, 'Too early to create image'
		self.tk = master.tk
		if not name:
			name = `id(self)`
			# The following is needed for systems where id(x)
			# can return a negative number, such as Linux/m68k:
			if name[0] == '-': name = '_' + name[1:]
		if kw and cnf: cnf = _cnfmerge((cnf, kw))
		elif kw: cnf = kw
		options = ()
		for k, v in cnf.items():
			if callable(v):
				v = self._register(v)
			options = options + ('-'+k, v)
		self.tk.call(('image', 'create', imgtype, name,) + options)
		self.name = name
	def __str__(self): return self.name
	def __del__(self):
		if self.name:
			try:
				self.tk.call('image', 'delete', self.name)
			except TclError:
				# May happen if the root was destroyed
				pass
	def __setitem__(self, key, value):
		self.tk.call(self.name, 'configure', '-'+key, value)
	def __getitem__(self, key):
		return self.tk.call(self.name, 'configure', '-'+key)
	def configure(self, **kw):
		res = ()
		for k, v in _cnfmerge(kw).items():
			if v is not None:
				if k[-1] == '_': k = k[:-1]
				if callable(v):
					v = self._register(v)
				res = res + ('-'+k, v)
		self.tk.call((self.name, 'config') + res)
	config = configure
	def height(self):
		return getint(
			self.tk.call('image', 'height', self.name))
	def type(self):
		return self.tk.call('image', 'type', self.name)
	def width(self):
		return getint(
			self.tk.call('image', 'width', self.name))

class PhotoImage(Image):
	def __init__(self, name=None, cnf={}, master=None, **kw):
		apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
	def blank(self):
		self.tk.call(self.name, 'blank')
	def cget(self, option):
		return self.tk.call(self.name, 'cget', '-' + option)
	# XXX config
	def __getitem__(self, key):
		return self.tk.call(self.name, 'cget', '-' + key)
	# XXX copy -from, -to, ...?
	def copy(self):
		destImage = PhotoImage()
		self.tk.call(destImage, 'copy', self.name)
		return destImage
	def zoom(self,x,y=''):
		destImage = PhotoImage()
		if y=='': y=x
		self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
		return destImage
	def subsample(self,x,y=''):
		destImage = PhotoImage()
		if y=='': y=x
		self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
		return destImage
	def get(self, x, y):
		return self.tk.call(self.name, 'get', x, y)
	def put(self, data, to=None):
		args = (self.name, 'put', data)
		if to:
			if to[0] == '-to':
				to = to[1:]
			args = args + ('-to',) + tuple(to)
		self.tk.call(args)
	# XXX read
	def write(self, filename, format=None, from_coords=None):
		args = (self.name, 'write', filename)
		if format:
			args = args + ('-format', format)
		if from_coords:
			args = args + ('-from',) + tuple(from_coords)
		self.tk.call(args)

class BitmapImage(Image):
	def __init__(self, name=None, cnf={}, master=None, **kw):
		apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)

def image_names(): return _default_root.tk.call('image', 'names')
def image_types(): return _default_root.tk.call('image', 'types')

######################################################################
# Extensions:

class Studbutton(Button):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'studbutton', cnf, kw)
		self.bind('<Any-Enter>',       self.tkButtonEnter)
		self.bind('<Any-Leave>',       self.tkButtonLeave)
		self.bind('<1>',               self.tkButtonDown)
		self.bind('<ButtonRelease-1>', self.tkButtonUp)

class Tributton(Button):
	def __init__(self, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'tributton', cnf, kw)
		self.bind('<Any-Enter>',       self.tkButtonEnter)
		self.bind('<Any-Leave>',       self.tkButtonLeave)
		self.bind('<1>',               self.tkButtonDown)
		self.bind('<ButtonRelease-1>', self.tkButtonUp)
		self['fg']               = self['bg']
		self['activebackground'] = self['bg']

######################################################################
# Test:

def _test():
	root = Tk()
	text = "This is Tcl/Tk version %s" % TclVersion
	if TclVersion >= 8.1:
		text = text + u"\nThis should be a cedilla: \347"
	label = Label(root, text=text)
	label.pack()
	test = Button(root, text="Click me!",
		      command=lambda root=root: root.test.configure(
			      text="[%s]" % root.test['text']))
	test.pack()
	root.test = test
	quit = Button(root, text="QUIT", command=root.destroy)
	quit.pack()
	# The following three commands are needed so the window pops
	# up on top on Windows...
	root.iconify()
	root.update()
	root.deiconify()
	root.mainloop()

if __name__ == '__main__':
	_test()