1# Tests for basic Tkinter widgets.
2
3import tkinter
4import Test
5
6Test.initialise()
7testData = ()
8
9if tkinter.TkVersion >= 8.0:
10    button_num = 31
11    frame_num = 16
12    menu_num = 20
13    menubutton_num = 32
14else:
15    button_num = 30
16    frame_num = 15
17    menu_num = 19
18    menubutton_num = 31
19
20c = tkinter.Button
21tests = (
22  (c.pack, ()),
23  (Test.num_options, (), button_num),
24  ('text', 'Hello World'),
25  ('background', 'lightsteelblue1'),
26  ('foreground', 'seagreen4'),
27  ('command', Test.callback),
28  (c.flash, ()),
29  (c.invoke, (), '1'),
30)
31testData = testData + ((c, ((tests, {}),)),)
32
33c = tkinter.Canvas
34tests = (
35  (c.pack, ()),
36  (Test.num_options, (), 27),
37  ('background', 'aliceblue'),
38  (c.create_oval, (100, 100, 200, 200),
39      {'fill' : 'lightsteelblue1', 'tags' : 'circle'}, 1),
40  (c.create_rectangle, (200, 100, 300, 200),
41      {'fill' : 'lightsteelblue2', 'tags' : 'square'}, 2),
42  (c.create_text, (0, 200),
43      {'text' : 'Hello, world', 'tags' : 'words', 'anchor' : 'w'}, 3),
44  (c.addtag_withtag, ('lightsteelblue1', 'circle')),
45  (c.bbox, ('circle', 'square'), (99, 99, 301, 201)),
46  (c.tag_bind, ('circle', '<1>', Test.callback)),
47  (c.tag_bind, 'circle', '<Button-1>'),
48  (c.tag_unbind, ('circle', '<1>')),
49  (c.canvasx, 100, 100.0),
50  (c.canvasy, 100, 100.0),
51  (c.coords, 'circle', [100.0, 100.0, 200.0, 200.0]),
52  (c.coords, ('circle', 0, 0, 300, 300), []),
53  (c.coords, 'circle', [0.0, 0.0, 300.0, 300.0]),
54  (c.find_withtag, 'lightsteelblue1', (1,)),
55  (c.focus, 'circle', ''),
56  (c.gettags, 'circle', ('circle', 'lightsteelblue1')),
57  (c.icursor, ('words', 7)),
58  (c.index, ('words', 'insert'), 7),
59  (c.insert, ('words', 'insert', 'cruel ')),
60  (c.itemconfigure, 'circle', {'fill': 'seagreen4'}),
61  (c.itemcget, ('circle', 'fill'), 'seagreen4'),
62  (c.lower, 'words'),
63  (c.move, ('square', -50, -50)),
64  (c.tkraise, ('words', 'circle')),
65  (c.scale, ('circle', 150, 150, 1.0, 0.5)),
66  (c.select_from, ('words', 0)),
67  (c.select_to, ('words', 'end')),
68  (c.delete, 'square'),
69  (c.type, 'circle', 'oval'),
70  (c.dtag, 'lightsteelblue1'),
71)
72testData = testData + ((c, ((tests, {}),)),)
73
74c = tkinter.Checkbutton
75tests = (
76  (c.pack, ()),
77  (Test.num_options, (), 36),
78  ('text', 'Hello World'),
79  ('background', 'lightsteelblue1'),
80  ('foreground', 'seagreen4'),
81  ('command', Test.callback),
82  (c.flash, ()),
83  (c.invoke, (), '1'),
84)
85testData = testData + ((c, ((tests, {}),)),)
86
87c = tkinter.Entry
88tests = (
89  (c.pack, ()),
90  (Test.num_options, (), 28),
91  ('background', 'lightsteelblue1'),
92  (c.insert, ('insert', 'Hello, Brian!')),
93  (c.delete, (7, 12)),
94  (c.icursor, 7),
95  (c.insert, ('insert', 'world')),
96  (c.get, (), 'Hello, world!'),
97  (c.index, 'insert', 12),
98  (c.selection_from, 7),
99  (c.selection_to, '12'),
100)
101testData = testData + ((c, ((tests, {}),)),)
102
103c = tkinter.Frame
104tests = (
105  (c.pack, ()),
106  (Test.num_options, (), frame_num),
107  ('background', 'lightsteelblue1'),
108  ('width', 300),
109  ('height', 50),
110  ('background', 'lightsteelblue1'),
111)
112testData = testData + ((c, ((tests, {}),)),)
113
114c = tkinter.Label
115tests = (
116  (c.pack, ()),
117  (Test.num_options, (), 25),
118  ('text', 'Hello World'),
119  ('background', 'lightsteelblue1'),
120  ('foreground', 'seagreen4'),
121  ('image', Test.earthris),
122)
123testData = testData + ((c, ((tests, {}),)),)
124
125c = tkinter.Listbox
126tests = (
127  (c.pack, ()),
128  (Test.num_options, (), 23),
129  ('background', 'lightsteelblue1'),
130  ('foreground', 'seagreen4'),
131  (c.insert, (0, 'ABC', 'DEF', 'GHI', 'XXXXXXXXXXXX')),
132  (c.activate, 1),
133  (c.select_set, (2, 3)),
134  (c.curselection, (), ('2', '3')),
135  (c.delete, 1),
136  (c.get, 1, 'GHI'),
137  (c.get, (0, 1), ('ABC', 'GHI')),
138  (c.index, 'end', 3),
139  (c.nearest, 1, 0),
140  (c.see, 1),
141  (c.size, (), 3),
142)
143testData = testData + ((c, ((tests, {}),)),)
144
145c = tkinter.Menu
146tests = (
147  (Test.num_options, (), menu_num),
148  ('background', 'lightsteelblue1'),
149  ('foreground', 'seagreen4'),
150  (c.add_command, (),
151      {'background': 'lightsteelblue2', 'label': 'Hello World'}),
152  (c.add_checkbutton, (),
153      {'background': 'lightsteelblue2', 'label': 'Charm'}),
154  (c.post, (100, 100)),
155  (c.activate, 1),
156  (c.entryconfigure, 'Hello World', {'background': 'aliceblue'}),
157  (c.entrycget, ('Hello World', 'background'), 'aliceblue'),
158  (c.index, 'end', 2),
159  ('tearoff', 0),
160  (c.index, 'end', 1),
161  (c.insert_radiobutton, 'Charm',
162      {'background': 'lightsteelblue2', 'label': 'Niceness',
163          'command': Test.callback}),
164  (c.invoke, 'Niceness', '1'),
165  (c.delete, 'Charm'),
166  (c.type, 'Hello World', 'command'),
167  (c.yposition, 'Hello World', 2),
168  (c.unpost, ()),
169)
170testData = testData + ((c, ((tests, {}),)),)
171
172c = tkinter.Menubutton
173tests = (
174  (c.pack, ()),
175  (Test.num_options, (), menubutton_num),
176  ('text', 'Hello World'),
177  ('background', 'lightsteelblue1'),
178  ('foreground', 'seagreen4'),
179)
180testData = testData + ((c, ((tests, {}),)),)
181
182c = tkinter.Message
183tests = (
184  (c.pack, ()),
185  (Test.num_options, (), 21),
186  ('text', 'Hello World'),
187  ('background', 'lightsteelblue1'),
188  ('foreground', 'seagreen4'),
189  ('text', 'Hello\nCruel Cruel World'),
190  ('borderwidth', 100),
191  ('justify', 'center'),
192  ('justify', 'right'),
193  ('justify', 'left'),
194)
195testData = testData + ((c, ((tests, {}),)),)
196
197c = tkinter.Radiobutton
198tests = (
199  (c.pack, ()),
200  (Test.num_options, (), 35),
201  ('text', 'Hello World'),
202  ('value', 'Foo Bar'),
203  ('variable', Test.stringvar),
204  ('background', 'lightsteelblue1'),
205  ('foreground', 'seagreen4'),
206  ('text', 'Hello\nCruel Cruel World'),
207  ('command', Test.callback),
208  (c.select, ()),
209  (Test.stringvar.get, (), 'Foo Bar'),
210  (c.flash, ()),
211  (c.invoke, (), '1'),
212  (c.deselect, ()),
213  (Test.stringvar.get, (), ''),
214)
215testData = testData + ((c, ((tests, {}),)),)
216
217c = tkinter.Scale
218tests = (
219  (c.pack, ()),
220  (Test.num_options, (), 33),
221  ('showvalue', 1),
222  ('orient', 'horizontal'),
223  ('from', 100.0),
224  ('to', 200.0),
225  ('variable', Test.floatvar),
226  ('background', 'lightsteelblue1'),
227  ('foreground', 'seagreen4'),
228  ('command', Test.callback1),
229  (c.set, 150.0),
230  (c.get, (), 150.0),
231  (c.get, 123, 'TypeError: too many arguments; expected 1, got 2'),
232)
233testData = testData + ((c, ((tests, {}),)),)
234
235c = tkinter.Scrollbar
236tests = (
237  (c.pack, (), {'fill': 'x'}),
238  (Test.num_options, (), 20),
239  ('orient', 'horizontal'),
240  (Test.set_geom, (300, 50)),
241  (c.set, (0.3, 0.7)),
242  ('background', 'lightsteelblue1'),
243  ('troughcolor', 'aliceblue'),
244  (c.get, (), (0.3, 0.7)),
245  (c.activate, 'slider'),
246  (c.set, (0.5, 0.9)),
247  (c.delta, (0, 0), 0),
248  (c.fraction, (0, 0), 0),
249)
250testData = testData + ((c, ((tests, {}),)),)
251
252c = tkinter.Text
253tests = (
254  (c.pack, ()),
255  (Test.num_options, (), 35),
256  ('background', 'lightsteelblue1'),
257  (c.insert, ('end', 'This little piggy is bold.', 'bold', '\n')),
258  (c.insert, ('end', 'This little piggy is in green.', 'green', '\n')),
259  (c.insert, ('end', 'This line is a mistake.\n')),
260  (c.insert, ('end', 'This little piggy is crossed out.', 'overstrike', '\n')),
261  (c.insert, ('end', 'This little piggy is raised.', 'raised', '\n')),
262  (c.insert, ('end', 'This little piggy is underlined.', 'underline', '\n')),
263  (c.tag_configure, 'bold', {'font': Test.font['variable']}),
264  (c.tag_configure, 'green', {'background': 'seagreen1'}),
265  (c.tag_configure, 'overstrike', {'overstrike': 1}),
266  (c.tag_configure, 'raised',
267      {'background': 'aliceblue', 'borderwidth': 2, 'relief': 'raised'}),
268  (c.tag_configure, 'underline', {'underline': 1}),
269  (c.compare, ('2.0', '<', 'end'), 1),
270  (c.delete, ('3.0', '4.0')),
271  (c.get, ('1.0', '1.4'), 'This'),
272  (c.index, 'end', '7.0'),
273  (c.mark_set, ('my_mark', '4.9')),
274  (c.mark_gravity, ('my_mark', 'right'), ''),
275  (c.mark_gravity, 'my_mark', 'right'),
276  (c.mark_names, (), ('my_mark', 'insert', 'current')),
277  (c.mark_unset, 'my_mark'),
278  (c.insert, ('end', '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n')),
279  (c.insert, ('end', 'This is the last line.')),
280  (c.scan_mark, (0, 20)),
281  (c.scan_dragto, (0, 0)),
282  (c.scan_dragto, (0, 20)),
283  (c.tag_add, ('green', '1.0', '1.4')),
284  (c.tag_cget, ('raised', 'background'), 'aliceblue'),
285  (c.tag_lower, 'green'),
286  (c.tag_names, (),
287      ('green', 'sel', 'bold', 'overstrike', 'raised', 'underline')),
288  (c.tag_nextrange, ('raised', '0.0'), ('4.0', '4.28')),
289  (c.tag_raise, 'green'),
290  (c.tag_ranges, 'green', ('1.0', '1.4', '2.0', '2.30')),
291  (c.tag_remove, ('green', '1.0', '1.4')),
292  (c.tag_ranges, 'green', ('2.0', '2.30')),
293  (c.tag_delete, 'green'),
294  (c.search, ('Gre.n', '0.0'), {'regexp': 1, 'nocase': 1}, '2.24'),
295  (c.search, ('Gre.n', '3.0', 'end'), {'regexp': 1, 'nocase': 1}, ''),
296  (c.see, 'end'),
297  (c.see, '0.0'),
298)
299testData = testData + ((c, ((tests, {}),)),)
300
301#=============================================================================
302
303# Grid command
304
305def _makeGridButtons():
306    w = Test.currentWidget()
307    b1 = tkinter.Button(w, text = 'Button 1')
308    b2 = tkinter.Button(w, text = 'Button 2')
309    b3 = tkinter.Button(w, text = 'Button 3')
310    b4 = tkinter.Button(w, text = 'Button 4')
311    b5 = tkinter.Button(w, text = 'Button 5')
312    b6 = tkinter.Button(w, text = 'Button 6')
313    b7 = tkinter.Button(w, text = 'Button 7')
314    b8 = tkinter.Button(w, text = 'Button 8')
315
316    b1.grid(column=0, row=0)
317    b2.grid(column=1, row=0)
318    b3.grid(column=2, row=0, ipadx=50, ipady=50, padx=50, pady=50, sticky='nsew')
319    b4.grid(column=3, row=0)
320    b5.grid(column=0, row=1)
321    b6.grid(column=2, row=1, columnspan=2, rowspan=2, sticky='nsew')
322    b7.grid(column=0, row=2)
323    b8.grid(column=0, row=3, columnspan=4, padx=50, sticky='ew')
324
325def _checkGridSlaves():
326    w = Test.currentWidget()
327    return len(w.grid_slaves())
328
329def _checkGridInfo():
330    w = Test.currentWidget()
331    b8 = w.grid_slaves(column=0, row=3)[0]
332    info = b8.grid_info()
333    if info['in'] == w:
334        rtn = {}
335        for key, value in list(info.items()):
336            if key != 'in':
337                rtn[key] = value
338        return rtn
339    return 'BAD'
340
341def _checkGridForget():
342    w = Test.currentWidget()
343    b8 = w.grid_slaves(column=0, row=3)[0]
344    b8.grid_forget()
345    return w.grid_size()
346
347# The -pad grid option was added in Tk 4.2.
348# Could not do columnconfigure(0) before Tk 4.2.
349if tkinter.TkVersion >= 4.2:
350    padTest = {'pad': 25}
351    colTest = {'minsize': 100, 'pad': 25, 'weight': 1}
352    rowTest = {'minsize': 100, 'pad': 0, 'weight': 1}
353else:
354    padTest = {'minsize': 100}
355    colTest = 'TclError: wrong # args: should be "grid columnconfigure master index ?-option value...?"'
356    rowTest = 'TclError: wrong # args: should be "grid rowconfigure master index ?-option value...?"'
357
358c = tkinter.Frame
359tests = (
360  (c.pack, (), {'fill': 'both', 'expand': 1}),
361  (_makeGridButtons, ()),
362  # (c.grid_bbox, (1, 2), (85, 268, 85, 34)),
363  (c.grid_columnconfigure, (0, 'minsize'), 0),
364  (c.grid_columnconfigure, (0, 'weight'), 0),
365  (c.grid_columnconfigure, 0, {'minsize': 100, 'weight': 1}),
366  (c.grid_columnconfigure, 0, padTest),
367  (c.grid_columnconfigure, 0, {}, colTest),
368  (c.grid_columnconfigure, (0, 'minsize'), 100),
369  (c.grid_columnconfigure, (0, 'weight'), 1),
370  (c.location, (200, 100), (2, 0)),
371  (c.grid_propagate, (), 1),
372  (c.grid_propagate, 0),
373  (c.grid_propagate, (), 0),
374  (c.grid_rowconfigure, (0, 'minsize'), 0),
375  (c.grid_rowconfigure, (0, 'weight'), 0),
376  (c.grid_rowconfigure, 0, {'minsize': 100, 'weight': 1}),
377  (c.grid_rowconfigure, 0, {}, rowTest),
378  (c.grid_size, (), (4, 4)),
379  (_checkGridSlaves, (), 8),
380  (_checkGridInfo, (), {}, {'column': '0', 'columnspan': '4',
381    'ipadx': '0', 'ipady': '0', 'padx': '50', 'pady': '0',
382    'row': '3', 'rowspan': '1', 'sticky': 'ew',
383  }),
384  (_checkGridForget, (), (4, 3)),
385  (_checkGridSlaves, (), 7),
386)
387
388testData = testData + ((c, ((tests, {}),)),)
389
390if __name__ == '__main__':
391        #Test.setverbose(1)
392        #Test.setdelay(1000)
393    Test.runTests(testData)
394