• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2import re
3import Tkinter as tkinter
4from Tkinter import TclError
5from test.test_support import requires, run_unittest
6
7from test_ttk.support import pixels_conv, tcl_version, requires_tcl
8from widget_tests import AbstractWidgetTest, int_round
9
10requires('gui')
11
12
13class PackTest(AbstractWidgetTest, unittest.TestCase):
14
15    test_keys = None
16
17    def create2(self):
18        pack = tkinter.Toplevel(self.root, name='pack')
19        pack.wm_geometry('300x200+0+0')
20        pack.wm_minsize(1, 1)
21        a = tkinter.Frame(pack, name='a', width=20, height=40, bg='red')
22        b = tkinter.Frame(pack, name='b', width=50, height=30, bg='blue')
23        c = tkinter.Frame(pack, name='c', width=80, height=80, bg='green')
24        d = tkinter.Frame(pack, name='d', width=40, height=30, bg='yellow')
25        return pack, a, b, c, d
26
27    def test_pack_configure_after(self):
28        pack, a, b, c, d = self.create2()
29        with self.assertRaisesRegexp(TclError, 'window "%s" isn\'t packed' % b):
30            a.pack_configure(after=b)
31        with self.assertRaisesRegexp(TclError, 'bad window path name ".foo"'):
32            a.pack_configure(after='.foo')
33        a.pack_configure(side='top')
34        b.pack_configure(side='top')
35        c.pack_configure(side='top')
36        d.pack_configure(side='top')
37        self.assertEqual(pack.pack_slaves(), [a, b, c, d])
38        a.pack_configure(after=b)
39        self.assertEqual(pack.pack_slaves(), [b, a, c, d])
40        a.pack_configure(after=a)
41        self.assertEqual(pack.pack_slaves(), [b, a, c, d])
42
43    def test_pack_configure_anchor(self):
44        pack, a, b, c, d = self.create2()
45        def check(anchor, geom):
46            a.pack_configure(side='top', ipadx=5, padx=10, ipady=15, pady=20,
47                             expand=True, anchor=anchor)
48            self.root.update()
49            self.assertEqual(a.winfo_geometry(), geom)
50        check('n', '30x70+135+20')
51        check('ne', '30x70+260+20')
52        check('e', '30x70+260+65')
53        check('se', '30x70+260+110')
54        check('s', '30x70+135+110')
55        check('sw', '30x70+10+110')
56        check('w', '30x70+10+65')
57        check('nw', '30x70+10+20')
58        check('center', '30x70+135+65')
59
60    def test_pack_configure_before(self):
61        pack, a, b, c, d = self.create2()
62        with self.assertRaisesRegexp(TclError, 'window "%s" isn\'t packed' % b):
63            a.pack_configure(before=b)
64        with self.assertRaisesRegexp(TclError, 'bad window path name ".foo"'):
65            a.pack_configure(before='.foo')
66        a.pack_configure(side='top')
67        b.pack_configure(side='top')
68        c.pack_configure(side='top')
69        d.pack_configure(side='top')
70        self.assertEqual(pack.pack_slaves(), [a, b, c, d])
71        a.pack_configure(before=d)
72        self.assertEqual(pack.pack_slaves(), [b, c, a, d])
73        a.pack_configure(before=a)
74        self.assertEqual(pack.pack_slaves(), [b, c, a, d])
75
76    def test_pack_configure_expand(self):
77        pack, a, b, c, d = self.create2()
78        def check(*geoms):
79            self.root.update()
80            self.assertEqual(a.winfo_geometry(), geoms[0])
81            self.assertEqual(b.winfo_geometry(), geoms[1])
82            self.assertEqual(c.winfo_geometry(), geoms[2])
83            self.assertEqual(d.winfo_geometry(), geoms[3])
84        a.pack_configure(side='left')
85        b.pack_configure(side='top')
86        c.pack_configure(side='right')
87        d.pack_configure(side='bottom')
88        check('20x40+0+80', '50x30+135+0', '80x80+220+75', '40x30+100+170')
89        a.pack_configure(side='left', expand='yes')
90        b.pack_configure(side='top', expand='on')
91        c.pack_configure(side='right', expand=True)
92        d.pack_configure(side='bottom', expand=1)
93        check('20x40+40+80', '50x30+175+35', '80x80+180+110', '40x30+100+135')
94        a.pack_configure(side='left', expand='yes', fill='both')
95        b.pack_configure(side='top', expand='on', fill='both')
96        c.pack_configure(side='right', expand=True, fill='both')
97        d.pack_configure(side='bottom', expand=1, fill='both')
98        check('100x200+0+0', '200x100+100+0', '160x100+140+100', '40x100+100+100')
99
100    def test_pack_configure_in(self):
101        pack, a, b, c, d = self.create2()
102        a.pack_configure(side='top')
103        b.pack_configure(side='top')
104        c.pack_configure(side='top')
105        d.pack_configure(side='top')
106        a.pack_configure(in_=pack)
107        self.assertEqual(pack.pack_slaves(), [b, c, d, a])
108        a.pack_configure(in_=c)
109        self.assertEqual(pack.pack_slaves(), [b, c, d])
110        self.assertEqual(c.pack_slaves(), [a])
111        with self.assertRaisesRegexp(TclError,
112                                     'can\'t pack %s inside itself' % (a,)):
113            a.pack_configure(in_=a)
114        with self.assertRaisesRegexp(TclError, 'bad window path name ".foo"'):
115            a.pack_configure(in_='.foo')
116
117    def test_pack_configure_padx_ipadx_fill(self):
118        pack, a, b, c, d = self.create2()
119        def check(geom1, geom2, **kwargs):
120            a.pack_forget()
121            b.pack_forget()
122            a.pack_configure(**kwargs)
123            b.pack_configure(expand=True, fill='both')
124            self.root.update()
125            self.assertEqual(a.winfo_geometry(), geom1)
126            self.assertEqual(b.winfo_geometry(), geom2)
127        check('20x40+260+80', '240x200+0+0', side='right', padx=20)
128        check('20x40+250+80', '240x200+0+0', side='right', padx=(10, 30))
129        check('60x40+240+80', '240x200+0+0', side='right', ipadx=20)
130        check('30x40+260+80', '250x200+0+0', side='right', ipadx=5, padx=10)
131        check('20x40+260+80', '240x200+0+0', side='right', padx=20, fill='x')
132        check('20x40+249+80', '240x200+0+0',
133              side='right', padx=(9, 31), fill='x')
134        check('60x40+240+80', '240x200+0+0', side='right', ipadx=20, fill='x')
135        check('30x40+260+80', '250x200+0+0',
136              side='right', ipadx=5, padx=10, fill='x')
137        check('30x40+255+80', '250x200+0+0',
138              side='right', ipadx=5, padx=(5, 15), fill='x')
139        check('20x40+140+0', '300x160+0+40', side='top', padx=20)
140        check('20x40+120+0', '300x160+0+40', side='top', padx=(0, 40))
141        check('60x40+120+0', '300x160+0+40', side='top', ipadx=20)
142        check('30x40+135+0', '300x160+0+40', side='top', ipadx=5, padx=10)
143        check('30x40+130+0', '300x160+0+40', side='top', ipadx=5, padx=(5, 15))
144        check('260x40+20+0', '300x160+0+40', side='top', padx=20, fill='x')
145        check('260x40+25+0', '300x160+0+40',
146              side='top', padx=(25, 15), fill='x')
147        check('300x40+0+0', '300x160+0+40', side='top', ipadx=20, fill='x')
148        check('280x40+10+0', '300x160+0+40',
149              side='top', ipadx=5, padx=10, fill='x')
150        check('280x40+5+0', '300x160+0+40',
151              side='top', ipadx=5, padx=(5, 15), fill='x')
152        a.pack_configure(padx='1c')
153        self.assertEqual(a.pack_info()['padx'],
154                         self._str(pack.winfo_pixels('1c')))
155        a.pack_configure(ipadx='1c')
156        self.assertEqual(a.pack_info()['ipadx'],
157                         self._str(pack.winfo_pixels('1c')))
158
159    def test_pack_configure_pady_ipady_fill(self):
160        pack, a, b, c, d = self.create2()
161        def check(geom1, geom2, **kwargs):
162            a.pack_forget()
163            b.pack_forget()
164            a.pack_configure(**kwargs)
165            b.pack_configure(expand=True, fill='both')
166            self.root.update()
167            self.assertEqual(a.winfo_geometry(), geom1)
168            self.assertEqual(b.winfo_geometry(), geom2)
169        check('20x40+280+80', '280x200+0+0', side='right', pady=20)
170        check('20x40+280+70', '280x200+0+0', side='right', pady=(10, 30))
171        check('20x80+280+60', '280x200+0+0', side='right', ipady=20)
172        check('20x50+280+75', '280x200+0+0', side='right', ipady=5, pady=10)
173        check('20x40+280+80', '280x200+0+0', side='right', pady=20, fill='x')
174        check('20x40+280+69', '280x200+0+0',
175              side='right', pady=(9, 31), fill='x')
176        check('20x80+280+60', '280x200+0+0', side='right', ipady=20, fill='x')
177        check('20x50+280+75', '280x200+0+0',
178              side='right', ipady=5, pady=10, fill='x')
179        check('20x50+280+70', '280x200+0+0',
180              side='right', ipady=5, pady=(5, 15), fill='x')
181        check('20x40+140+20', '300x120+0+80', side='top', pady=20)
182        check('20x40+140+0', '300x120+0+80', side='top', pady=(0, 40))
183        check('20x80+140+0', '300x120+0+80', side='top', ipady=20)
184        check('20x50+140+10', '300x130+0+70', side='top', ipady=5, pady=10)
185        check('20x50+140+5', '300x130+0+70', side='top', ipady=5, pady=(5, 15))
186        check('300x40+0+20', '300x120+0+80', side='top', pady=20, fill='x')
187        check('300x40+0+25', '300x120+0+80',
188              side='top', pady=(25, 15), fill='x')
189        check('300x80+0+0', '300x120+0+80', side='top', ipady=20, fill='x')
190        check('300x50+0+10', '300x130+0+70',
191              side='top', ipady=5, pady=10, fill='x')
192        check('300x50+0+5', '300x130+0+70',
193              side='top', ipady=5, pady=(5, 15), fill='x')
194        a.pack_configure(pady='1c')
195        self.assertEqual(a.pack_info()['pady'],
196                         self._str(pack.winfo_pixels('1c')))
197        a.pack_configure(ipady='1c')
198        self.assertEqual(a.pack_info()['ipady'],
199                         self._str(pack.winfo_pixels('1c')))
200
201    def test_pack_configure_side(self):
202        pack, a, b, c, d = self.create2()
203        def check(side, geom1, geom2):
204            a.pack_configure(side=side)
205            self.assertEqual(a.pack_info()['side'], side)
206            b.pack_configure(expand=True, fill='both')
207            self.root.update()
208            self.assertEqual(a.winfo_geometry(), geom1)
209            self.assertEqual(b.winfo_geometry(), geom2)
210        check('top', '20x40+140+0', '300x160+0+40')
211        check('bottom', '20x40+140+160', '300x160+0+0')
212        check('left', '20x40+0+80', '280x200+20+0')
213        check('right', '20x40+280+80', '280x200+0+0')
214
215    def test_pack_forget(self):
216        pack, a, b, c, d = self.create2()
217        a.pack_configure()
218        b.pack_configure()
219        c.pack_configure()
220        self.assertEqual(pack.pack_slaves(), [a, b, c])
221        b.pack_forget()
222        self.assertEqual(pack.pack_slaves(), [a, c])
223        b.pack_forget()
224        self.assertEqual(pack.pack_slaves(), [a, c])
225        d.pack_forget()
226
227    def test_pack_info(self):
228        pack, a, b, c, d = self.create2()
229        with self.assertRaisesRegexp(TclError, 'window "%s" isn\'t packed' % a):
230            a.pack_info()
231        a.pack_configure()
232        b.pack_configure(side='right', in_=a, anchor='s', expand=True, fill='x',
233                         ipadx=5, padx=10, ipady=2, pady=(5, 15))
234        info = a.pack_info()
235        self.assertIsInstance(info, dict)
236        self.assertEqual(info['anchor'], 'center')
237        self.assertEqual(info['expand'], self._str(0))
238        self.assertEqual(info['fill'], 'none')
239        self.assertEqual(info['in'], pack)
240        self.assertEqual(info['ipadx'], self._str(0))
241        self.assertEqual(info['ipady'], self._str(0))
242        self.assertEqual(info['padx'], self._str(0))
243        self.assertEqual(info['pady'], self._str(0))
244        self.assertEqual(info['side'], 'top')
245        info = b.pack_info()
246        self.assertIsInstance(info, dict)
247        self.assertEqual(info['anchor'], 's')
248        self.assertEqual(info['expand'], self._str(1))
249        self.assertEqual(info['fill'], 'x')
250        self.assertEqual(info['in'], a)
251        self.assertEqual(info['ipadx'], self._str(5))
252        self.assertEqual(info['ipady'], self._str(2))
253        self.assertEqual(info['padx'], self._str(10))
254        self.assertEqual(info['pady'], self._str((5, 15)))
255        self.assertEqual(info['side'], 'right')
256
257    def test_pack_propagate(self):
258        pack, a, b, c, d = self.create2()
259        pack.configure(width=300, height=200)
260        a.pack_configure()
261        pack.pack_propagate(False)
262        self.root.update()
263        self.assertEqual(pack.winfo_reqwidth(), 300)
264        self.assertEqual(pack.winfo_reqheight(), 200)
265        pack.pack_propagate(True)
266        self.root.update()
267        self.assertEqual(pack.winfo_reqwidth(), 20)
268        self.assertEqual(pack.winfo_reqheight(), 40)
269
270    def test_pack_slaves(self):
271        pack, a, b, c, d = self.create2()
272        self.assertEqual(pack.pack_slaves(), [])
273        a.pack_configure()
274        self.assertEqual(pack.pack_slaves(), [a])
275        b.pack_configure()
276        self.assertEqual(pack.pack_slaves(), [a, b])
277
278
279class PlaceTest(AbstractWidgetTest, unittest.TestCase):
280
281    test_keys = None
282
283    def create2(self):
284        t = tkinter.Toplevel(self.root, width=300, height=200, bd=0)
285        t.wm_geometry('300x200+0+0')
286        f = tkinter.Frame(t, width=154, height=84, bd=2, relief='raised')
287        f.place_configure(x=48, y=38)
288        f2 = tkinter.Frame(t, width=30, height=60, bd=2, relief='raised')
289        self.root.update()
290        return t, f, f2
291
292    def test_place_configure_in(self):
293        t, f, f2 = self.create2()
294        self.assertEqual(f2.winfo_manager(), '')
295        with self.assertRaisesRegexp(TclError, "can't place %s relative to "
296                                     "itself" % re.escape(str(f2))):
297            f2.place_configure(in_=f2)
298        if tcl_version >= (8, 5):
299            self.assertEqual(f2.winfo_manager(), '')
300        with self.assertRaisesRegexp(TclError, 'bad window path name'):
301            f2.place_configure(in_='spam')
302        f2.place_configure(in_=f)
303        self.assertEqual(f2.winfo_manager(), 'place')
304
305    def test_place_configure_x(self):
306        t, f, f2 = self.create2()
307        f2.place_configure(in_=f)
308        self.assertEqual(f2.place_info()['x'], '0')
309        self.root.update()
310        self.assertEqual(f2.winfo_x(), 50)
311        f2.place_configure(x=100)
312        self.assertEqual(f2.place_info()['x'], '100')
313        self.root.update()
314        self.assertEqual(f2.winfo_x(), 150)
315        f2.place_configure(x=-10, relx=1)
316        self.assertEqual(f2.place_info()['x'], '-10')
317        self.root.update()
318        self.assertEqual(f2.winfo_x(), 190)
319        with self.assertRaisesRegexp(TclError, 'bad screen distance "spam"'):
320            f2.place_configure(in_=f, x='spam')
321
322    def test_place_configure_y(self):
323        t, f, f2 = self.create2()
324        f2.place_configure(in_=f)
325        self.assertEqual(f2.place_info()['y'], '0')
326        self.root.update()
327        self.assertEqual(f2.winfo_y(), 40)
328        f2.place_configure(y=50)
329        self.assertEqual(f2.place_info()['y'], '50')
330        self.root.update()
331        self.assertEqual(f2.winfo_y(), 90)
332        f2.place_configure(y=-10, rely=1)
333        self.assertEqual(f2.place_info()['y'], '-10')
334        self.root.update()
335        self.assertEqual(f2.winfo_y(), 110)
336        with self.assertRaisesRegexp(TclError, 'bad screen distance "spam"'):
337            f2.place_configure(in_=f, y='spam')
338
339    def test_place_configure_relx(self):
340        t, f, f2 = self.create2()
341        f2.place_configure(in_=f)
342        self.assertEqual(f2.place_info()['relx'], '0')
343        self.root.update()
344        self.assertEqual(f2.winfo_x(), 50)
345        f2.place_configure(relx=0.5)
346        self.assertEqual(f2.place_info()['relx'], '0.5')
347        self.root.update()
348        self.assertEqual(f2.winfo_x(), 125)
349        f2.place_configure(relx=1)
350        self.assertEqual(f2.place_info()['relx'], '1')
351        self.root.update()
352        self.assertEqual(f2.winfo_x(), 200)
353        with self.assertRaisesRegexp(TclError, 'expected floating-point number '
354                                     'but got "spam"'):
355            f2.place_configure(in_=f, relx='spam')
356
357    def test_place_configure_rely(self):
358        t, f, f2 = self.create2()
359        f2.place_configure(in_=f)
360        self.assertEqual(f2.place_info()['rely'], '0')
361        self.root.update()
362        self.assertEqual(f2.winfo_y(), 40)
363        f2.place_configure(rely=0.5)
364        self.assertEqual(f2.place_info()['rely'], '0.5')
365        self.root.update()
366        self.assertEqual(f2.winfo_y(), 80)
367        f2.place_configure(rely=1)
368        self.assertEqual(f2.place_info()['rely'], '1')
369        self.root.update()
370        self.assertEqual(f2.winfo_y(), 120)
371        with self.assertRaisesRegexp(TclError, 'expected floating-point number '
372                                     'but got "spam"'):
373            f2.place_configure(in_=f, rely='spam')
374
375    def test_place_configure_anchor(self):
376        f = tkinter.Frame(self.root)
377        with self.assertRaisesRegexp(TclError, 'bad anchor "j"'):
378            f.place_configure(anchor='j')
379        with self.assertRaisesRegexp(TclError, 'ambiguous anchor ""'):
380            f.place_configure(anchor='')
381        for value in 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'center':
382            f.place_configure(anchor=value)
383            self.assertEqual(f.place_info()['anchor'], value)
384
385    def test_place_configure_width(self):
386        t, f, f2 = self.create2()
387        f2.place_configure(in_=f, width=120)
388        self.root.update()
389        self.assertEqual(f2.winfo_width(), 120)
390        f2.place_configure(width='')
391        self.root.update()
392        self.assertEqual(f2.winfo_width(), 30)
393        with self.assertRaisesRegexp(TclError, 'bad screen distance "abcd"'):
394            f2.place_configure(width='abcd')
395
396    def test_place_configure_height(self):
397        t, f, f2 = self.create2()
398        f2.place_configure(in_=f, height=120)
399        self.root.update()
400        self.assertEqual(f2.winfo_height(), 120)
401        f2.place_configure(height='')
402        self.root.update()
403        self.assertEqual(f2.winfo_height(), 60)
404        with self.assertRaisesRegexp(TclError, 'bad screen distance "abcd"'):
405            f2.place_configure(height='abcd')
406
407    def test_place_configure_relwidth(self):
408        t, f, f2 = self.create2()
409        f2.place_configure(in_=f, relwidth=0.5)
410        self.root.update()
411        self.assertEqual(f2.winfo_width(), 75)
412        f2.place_configure(relwidth='')
413        self.root.update()
414        self.assertEqual(f2.winfo_width(), 30)
415        with self.assertRaisesRegexp(TclError, 'expected floating-point number '
416                                     'but got "abcd"'):
417            f2.place_configure(relwidth='abcd')
418
419    def test_place_configure_relheight(self):
420        t, f, f2 = self.create2()
421        f2.place_configure(in_=f, relheight=0.5)
422        self.root.update()
423        self.assertEqual(f2.winfo_height(), 40)
424        f2.place_configure(relheight='')
425        self.root.update()
426        self.assertEqual(f2.winfo_height(), 60)
427        with self.assertRaisesRegexp(TclError, 'expected floating-point number '
428                                     'but got "abcd"'):
429            f2.place_configure(relheight='abcd')
430
431    def test_place_configure_bordermode(self):
432        f = tkinter.Frame(self.root)
433        with self.assertRaisesRegexp(TclError, 'bad bordermode "j"'):
434            f.place_configure(bordermode='j')
435        with self.assertRaisesRegexp(TclError, 'ambiguous bordermode ""'):
436            f.place_configure(bordermode='')
437        for value in 'inside', 'outside', 'ignore':
438            f.place_configure(bordermode=value)
439            self.assertEqual(f.place_info()['bordermode'], value)
440
441    def test_place_forget(self):
442        foo = tkinter.Frame(self.root)
443        foo.place_configure(width=50, height=50)
444        self.root.update()
445        foo.place_forget()
446        self.root.update()
447        self.assertFalse(foo.winfo_ismapped())
448        with self.assertRaises(TypeError):
449            foo.place_forget(0)
450
451    def test_place_info(self):
452        t, f, f2 = self.create2()
453        f2.place_configure(in_=f, x=1, y=2, width=3, height=4,
454                           relx=0.1, rely=0.2, relwidth=0.3, relheight=0.4,
455                           anchor='se', bordermode='outside')
456        info = f2.place_info()
457        self.assertIsInstance(info, dict)
458        self.assertEqual(info['x'], '1')
459        self.assertEqual(info['y'], '2')
460        self.assertEqual(info['width'], '3')
461        self.assertEqual(info['height'], '4')
462        self.assertEqual(info['relx'], '0.1')
463        self.assertEqual(info['rely'], '0.2')
464        self.assertEqual(info['relwidth'], '0.3')
465        self.assertEqual(info['relheight'], '0.4')
466        self.assertEqual(info['anchor'], 'se')
467        self.assertEqual(info['bordermode'], 'outside')
468        self.assertEqual(info['x'], '1')
469        self.assertEqual(info['x'], '1')
470        with self.assertRaises(TypeError):
471            f2.place_info(0)
472
473    def test_place_slaves(self):
474        foo = tkinter.Frame(self.root)
475        bar = tkinter.Frame(self.root)
476        self.assertEqual(foo.place_slaves(), [])
477        bar.place_configure(in_=foo)
478        self.assertEqual(foo.place_slaves(), [bar])
479        with self.assertRaises(TypeError):
480            foo.place_slaves(0)
481
482
483class GridTest(AbstractWidgetTest, unittest.TestCase):
484
485    test_keys = None
486
487    def tearDown(self):
488        cols, rows = self.root.grid_size()
489        for i in range(cols + 1):
490            self.root.grid_columnconfigure(i, weight=0, minsize=0, pad=0, uniform='')
491        for i in range(rows + 1):
492            self.root.grid_rowconfigure(i, weight=0, minsize=0, pad=0, uniform='')
493        self.root.grid_propagate(1)
494        super(GridTest, self).tearDown()
495
496    def test_grid_configure(self):
497        b = tkinter.Button(self.root)
498        self.assertEqual(b.grid_info(), {})
499        b.grid_configure()
500        self.assertEqual(b.grid_info()['in'], self.root)
501        self.assertEqual(b.grid_info()['column'], self._str(0))
502        self.assertEqual(b.grid_info()['row'], self._str(0))
503        b.grid_configure({'column': 1}, row=2)
504        self.assertEqual(b.grid_info()['column'], self._str(1))
505        self.assertEqual(b.grid_info()['row'], self._str(2))
506
507    def test_grid_configure_column(self):
508        b = tkinter.Button(self.root)
509        with self.assertRaisesRegexp(TclError, 'bad column value "-1": '
510                                     'must be a non-negative integer'):
511            b.grid_configure(column=-1)
512        b.grid_configure(column=2)
513        self.assertEqual(b.grid_info()['column'], self._str(2))
514
515    def test_grid_configure_columnspan(self):
516        b = tkinter.Button(self.root)
517        with self.assertRaisesRegexp(TclError, 'bad columnspan value "0": '
518                                     'must be a positive integer'):
519            b.grid_configure(columnspan=0)
520        b.grid_configure(columnspan=2)
521        self.assertEqual(b.grid_info()['columnspan'], self._str(2))
522
523    def test_grid_configure_in(self):
524        f = tkinter.Frame(self.root)
525        b = tkinter.Button(self.root)
526        self.assertEqual(b.grid_info(), {})
527        b.grid_configure()
528        self.assertEqual(b.grid_info()['in'], self.root)
529        b.grid_configure(in_=f)
530        self.assertEqual(b.grid_info()['in'], f)
531        b.grid_configure({'in': self.root})
532        self.assertEqual(b.grid_info()['in'], self.root)
533
534    def test_grid_configure_ipadx(self):
535        b = tkinter.Button(self.root)
536        with self.assertRaisesRegexp(TclError, 'bad ipadx value "-1": '
537                                     'must be positive screen distance'):
538            b.grid_configure(ipadx=-1)
539        b.grid_configure(ipadx=1)
540        self.assertEqual(b.grid_info()['ipadx'], self._str(1))
541        b.grid_configure(ipadx='.5c')
542        self.assertEqual(b.grid_info()['ipadx'],
543                self._str(int_round(pixels_conv('.5c') * self.scaling)))
544
545    def test_grid_configure_ipady(self):
546        b = tkinter.Button(self.root)
547        with self.assertRaisesRegexp(TclError, 'bad ipady value "-1": '
548                                     'must be positive screen distance'):
549            b.grid_configure(ipady=-1)
550        b.grid_configure(ipady=1)
551        self.assertEqual(b.grid_info()['ipady'], self._str(1))
552        b.grid_configure(ipady='.5c')
553        self.assertEqual(b.grid_info()['ipady'],
554                self._str(int_round(pixels_conv('.5c') * self.scaling)))
555
556    def test_grid_configure_padx(self):
557        b = tkinter.Button(self.root)
558        with self.assertRaisesRegexp(TclError, 'bad pad value "-1": '
559                                     'must be positive screen distance'):
560            b.grid_configure(padx=-1)
561        b.grid_configure(padx=1)
562        self.assertEqual(b.grid_info()['padx'], self._str(1))
563        b.grid_configure(padx=(10, 5))
564        self.assertEqual(b.grid_info()['padx'], self._str((10, 5)))
565        b.grid_configure(padx='.5c')
566        self.assertEqual(b.grid_info()['padx'],
567                self._str(int_round(pixels_conv('.5c') * self.scaling)))
568
569    def test_grid_configure_pady(self):
570        b = tkinter.Button(self.root)
571        with self.assertRaisesRegexp(TclError, 'bad pad value "-1": '
572                                     'must be positive screen distance'):
573            b.grid_configure(pady=-1)
574        b.grid_configure(pady=1)
575        self.assertEqual(b.grid_info()['pady'], self._str(1))
576        b.grid_configure(pady=(10, 5))
577        self.assertEqual(b.grid_info()['pady'], self._str((10, 5)))
578        b.grid_configure(pady='.5c')
579        self.assertEqual(b.grid_info()['pady'],
580                self._str(int_round(pixels_conv('.5c') * self.scaling)))
581
582    def test_grid_configure_row(self):
583        b = tkinter.Button(self.root)
584        with self.assertRaisesRegexp(TclError, 'bad (row|grid) value "-1": '
585                                     'must be a non-negative integer'):
586            b.grid_configure(row=-1)
587        b.grid_configure(row=2)
588        self.assertEqual(b.grid_info()['row'], self._str(2))
589
590    def test_grid_configure_rownspan(self):
591        b = tkinter.Button(self.root)
592        with self.assertRaisesRegexp(TclError, 'bad rowspan value "0": '
593                                     'must be a positive integer'):
594            b.grid_configure(rowspan=0)
595        b.grid_configure(rowspan=2)
596        self.assertEqual(b.grid_info()['rowspan'], self._str(2))
597
598    def test_grid_configure_sticky(self):
599        f = tkinter.Frame(self.root, bg='red')
600        with self.assertRaisesRegexp(TclError, 'bad stickyness value "glue"'):
601            f.grid_configure(sticky='glue')
602        f.grid_configure(sticky='ne')
603        self.assertEqual(f.grid_info()['sticky'], 'ne')
604        f.grid_configure(sticky='n,s,e,w')
605        self.assertEqual(f.grid_info()['sticky'], 'nesw')
606
607    def test_grid_columnconfigure(self):
608        with self.assertRaises(TypeError):
609            self.root.grid_columnconfigure()
610        self.assertEqual(self.root.grid_columnconfigure(0),
611                         {'minsize': 0, 'pad': 0, 'uniform': None, 'weight': 0})
612        with self.assertRaisesRegexp(TclError, 'bad option "-foo"'):
613            self.root.grid_columnconfigure(0, 'foo')
614        self.root.grid_columnconfigure((0, 3), weight=2)
615        with self.assertRaisesRegexp(TclError,
616                                     'must specify a single element on retrieval'):
617            self.root.grid_columnconfigure((0, 3))
618        b = tkinter.Button(self.root)
619        b.grid_configure(column=0, row=0)
620        if tcl_version >= (8, 5):
621            self.root.grid_columnconfigure('all', weight=3)
622            with self.assertRaisesRegexp(TclError, 'expected integer but got "all"'):
623                self.root.grid_columnconfigure('all')
624            self.assertEqual(self.root.grid_columnconfigure(0, 'weight'), 3)
625        self.assertEqual(self.root.grid_columnconfigure(3, 'weight'), 2)
626        self.assertEqual(self.root.grid_columnconfigure(265, 'weight'), 0)
627        if tcl_version >= (8, 5):
628            self.root.grid_columnconfigure(b, weight=4)
629            self.assertEqual(self.root.grid_columnconfigure(0, 'weight'), 4)
630
631    def test_grid_columnconfigure_minsize(self):
632        with self.assertRaisesRegexp(TclError, 'bad screen distance "foo"'):
633            self.root.grid_columnconfigure(0, minsize='foo')
634        self.root.grid_columnconfigure(0, minsize=10)
635        self.assertEqual(self.root.grid_columnconfigure(0, 'minsize'), 10)
636        self.assertEqual(self.root.grid_columnconfigure(0)['minsize'], 10)
637
638    def test_grid_columnconfigure_weight(self):
639        with self.assertRaisesRegexp(TclError, 'expected integer but got "bad"'):
640            self.root.grid_columnconfigure(0, weight='bad')
641        with self.assertRaisesRegexp(TclError, 'invalid arg "-weight": '
642                                     'should be non-negative'):
643            self.root.grid_columnconfigure(0, weight=-3)
644        self.root.grid_columnconfigure(0, weight=3)
645        self.assertEqual(self.root.grid_columnconfigure(0, 'weight'), 3)
646        self.assertEqual(self.root.grid_columnconfigure(0)['weight'], 3)
647
648    def test_grid_columnconfigure_pad(self):
649        with self.assertRaisesRegexp(TclError, 'bad screen distance "foo"'):
650            self.root.grid_columnconfigure(0, pad='foo')
651        with self.assertRaisesRegexp(TclError, 'invalid arg "-pad": '
652                                     'should be non-negative'):
653            self.root.grid_columnconfigure(0, pad=-3)
654        self.root.grid_columnconfigure(0, pad=3)
655        self.assertEqual(self.root.grid_columnconfigure(0, 'pad'), 3)
656        self.assertEqual(self.root.grid_columnconfigure(0)['pad'], 3)
657
658    def test_grid_columnconfigure_uniform(self):
659        self.root.grid_columnconfigure(0, uniform='foo')
660        self.assertEqual(self.root.grid_columnconfigure(0, 'uniform'), 'foo')
661        self.assertEqual(self.root.grid_columnconfigure(0)['uniform'], 'foo')
662
663    def test_grid_rowconfigure(self):
664        with self.assertRaises(TypeError):
665            self.root.grid_rowconfigure()
666        self.assertEqual(self.root.grid_rowconfigure(0),
667                         {'minsize': 0, 'pad': 0, 'uniform': None, 'weight': 0})
668        with self.assertRaisesRegexp(TclError, 'bad option "-foo"'):
669            self.root.grid_rowconfigure(0, 'foo')
670        self.root.grid_rowconfigure((0, 3), weight=2)
671        with self.assertRaisesRegexp(TclError,
672                                     'must specify a single element on retrieval'):
673            self.root.grid_rowconfigure((0, 3))
674        b = tkinter.Button(self.root)
675        b.grid_configure(column=0, row=0)
676        if tcl_version >= (8, 5):
677            self.root.grid_rowconfigure('all', weight=3)
678            with self.assertRaisesRegexp(TclError, 'expected integer but got "all"'):
679                self.root.grid_rowconfigure('all')
680            self.assertEqual(self.root.grid_rowconfigure(0, 'weight'), 3)
681        self.assertEqual(self.root.grid_rowconfigure(3, 'weight'), 2)
682        self.assertEqual(self.root.grid_rowconfigure(265, 'weight'), 0)
683        if tcl_version >= (8, 5):
684            self.root.grid_rowconfigure(b, weight=4)
685            self.assertEqual(self.root.grid_rowconfigure(0, 'weight'), 4)
686
687    def test_grid_rowconfigure_minsize(self):
688        with self.assertRaisesRegexp(TclError, 'bad screen distance "foo"'):
689            self.root.grid_rowconfigure(0, minsize='foo')
690        self.root.grid_rowconfigure(0, minsize=10)
691        self.assertEqual(self.root.grid_rowconfigure(0, 'minsize'), 10)
692        self.assertEqual(self.root.grid_rowconfigure(0)['minsize'], 10)
693
694    def test_grid_rowconfigure_weight(self):
695        with self.assertRaisesRegexp(TclError, 'expected integer but got "bad"'):
696            self.root.grid_rowconfigure(0, weight='bad')
697        with self.assertRaisesRegexp(TclError, 'invalid arg "-weight": '
698                                     'should be non-negative'):
699            self.root.grid_rowconfigure(0, weight=-3)
700        self.root.grid_rowconfigure(0, weight=3)
701        self.assertEqual(self.root.grid_rowconfigure(0, 'weight'), 3)
702        self.assertEqual(self.root.grid_rowconfigure(0)['weight'], 3)
703
704    def test_grid_rowconfigure_pad(self):
705        with self.assertRaisesRegexp(TclError, 'bad screen distance "foo"'):
706            self.root.grid_rowconfigure(0, pad='foo')
707        with self.assertRaisesRegexp(TclError, 'invalid arg "-pad": '
708                                     'should be non-negative'):
709            self.root.grid_rowconfigure(0, pad=-3)
710        self.root.grid_rowconfigure(0, pad=3)
711        self.assertEqual(self.root.grid_rowconfigure(0, 'pad'), 3)
712        self.assertEqual(self.root.grid_rowconfigure(0)['pad'], 3)
713
714    def test_grid_rowconfigure_uniform(self):
715        self.root.grid_rowconfigure(0, uniform='foo')
716        self.assertEqual(self.root.grid_rowconfigure(0, 'uniform'), 'foo')
717        self.assertEqual(self.root.grid_rowconfigure(0)['uniform'], 'foo')
718
719    def test_grid_forget(self):
720        b = tkinter.Button(self.root)
721        c = tkinter.Button(self.root)
722        b.grid_configure(row=2, column=2, rowspan=2, columnspan=2,
723                         padx=3, pady=4, sticky='ns')
724        self.assertEqual(self.root.grid_slaves(), [b])
725        b.grid_forget()
726        c.grid_forget()
727        self.assertEqual(self.root.grid_slaves(), [])
728        self.assertEqual(b.grid_info(), {})
729        b.grid_configure(row=0, column=0)
730        info = b.grid_info()
731        self.assertEqual(info['row'], self._str(0))
732        self.assertEqual(info['column'], self._str(0))
733        self.assertEqual(info['rowspan'], self._str(1))
734        self.assertEqual(info['columnspan'], self._str(1))
735        self.assertEqual(info['padx'], self._str(0))
736        self.assertEqual(info['pady'], self._str(0))
737        self.assertEqual(info['sticky'], '')
738
739    def test_grid_remove(self):
740        b = tkinter.Button(self.root)
741        c = tkinter.Button(self.root)
742        b.grid_configure(row=2, column=2, rowspan=2, columnspan=2,
743                         padx=3, pady=4, sticky='ns')
744        self.assertEqual(self.root.grid_slaves(), [b])
745        b.grid_remove()
746        c.grid_remove()
747        self.assertEqual(self.root.grid_slaves(), [])
748        self.assertEqual(b.grid_info(), {})
749        b.grid_configure(row=0, column=0)
750        info = b.grid_info()
751        self.assertEqual(info['row'], self._str(0))
752        self.assertEqual(info['column'], self._str(0))
753        self.assertEqual(info['rowspan'], self._str(2))
754        self.assertEqual(info['columnspan'], self._str(2))
755        self.assertEqual(info['padx'], self._str(3))
756        self.assertEqual(info['pady'], self._str(4))
757        self.assertEqual(info['sticky'], 'ns')
758
759    def test_grid_info(self):
760        b = tkinter.Button(self.root)
761        self.assertEqual(b.grid_info(), {})
762        b.grid_configure(row=2, column=2, rowspan=2, columnspan=2,
763                         padx=3, pady=4, sticky='ns')
764        info = b.grid_info()
765        self.assertIsInstance(info, dict)
766        self.assertEqual(info['in'], self.root)
767        self.assertEqual(info['row'], self._str(2))
768        self.assertEqual(info['column'], self._str(2))
769        self.assertEqual(info['rowspan'], self._str(2))
770        self.assertEqual(info['columnspan'], self._str(2))
771        self.assertEqual(info['padx'], self._str(3))
772        self.assertEqual(info['pady'], self._str(4))
773        self.assertEqual(info['sticky'], 'ns')
774
775    def test_grid_bbox(self):
776        self.assertEqual(self.root.grid_bbox(), (0, 0, 0, 0))
777        self.assertEqual(self.root.grid_bbox(0, 0), (0, 0, 0, 0))
778        self.assertEqual(self.root.grid_bbox(0, 0, 1, 1), (0, 0, 0, 0))
779        with self.assertRaisesRegexp(TclError, 'expected integer but got "x"'):
780            self.root.grid_bbox('x', 0)
781        with self.assertRaisesRegexp(TclError, 'expected integer but got "x"'):
782            self.root.grid_bbox(0, 'x')
783        with self.assertRaisesRegexp(TclError, 'expected integer but got "x"'):
784            self.root.grid_bbox(0, 0, 'x', 0)
785        with self.assertRaisesRegexp(TclError, 'expected integer but got "x"'):
786            self.root.grid_bbox(0, 0, 0, 'x')
787        with self.assertRaises(TypeError):
788            self.root.grid_bbox(0, 0, 0, 0, 0)
789        t = self.root
790        # de-maximize
791        t.wm_geometry('1x1+0+0')
792        t.wm_geometry('')
793        f1 = tkinter.Frame(t, width=75, height=75, bg='red')
794        f2 = tkinter.Frame(t, width=90, height=90, bg='blue')
795        f1.grid_configure(row=0, column=0)
796        f2.grid_configure(row=1, column=1)
797        self.root.update()
798        self.assertEqual(t.grid_bbox(), (0, 0, 165, 165))
799        self.assertEqual(t.grid_bbox(0, 0), (0, 0, 75, 75))
800        self.assertEqual(t.grid_bbox(0, 0, 1, 1), (0, 0, 165, 165))
801        self.assertEqual(t.grid_bbox(1, 1), (75, 75, 90, 90))
802        self.assertEqual(t.grid_bbox(10, 10, 0, 0), (0, 0, 165, 165))
803        self.assertEqual(t.grid_bbox(-2, -2, -1, -1), (0, 0, 0, 0))
804        self.assertEqual(t.grid_bbox(10, 10, 12, 12), (165, 165, 0, 0))
805
806    def test_grid_location(self):
807        with self.assertRaises(TypeError):
808            self.root.grid_location()
809        with self.assertRaises(TypeError):
810            self.root.grid_location(0)
811        with self.assertRaises(TypeError):
812            self.root.grid_location(0, 0, 0)
813        with self.assertRaisesRegexp(TclError, 'bad screen distance "x"'):
814            self.root.grid_location('x', 'y')
815        with self.assertRaisesRegexp(TclError, 'bad screen distance "y"'):
816            self.root.grid_location('1c', 'y')
817        t = self.root
818        # de-maximize
819        t.wm_geometry('1x1+0+0')
820        t.wm_geometry('')
821        f = tkinter.Frame(t, width=200, height=100,
822                          highlightthickness=0, bg='red')
823        self.assertEqual(f.grid_location(10, 10), (-1, -1))
824        f.grid_configure()
825        self.root.update()
826        self.assertEqual(t.grid_location(-10, -10), (-1, -1))
827        self.assertEqual(t.grid_location(-10, 0), (-1, 0))
828        self.assertEqual(t.grid_location(-1, 0), (-1, 0))
829        self.assertEqual(t.grid_location(0, -10), (0, -1))
830        self.assertEqual(t.grid_location(0, -1), (0, -1))
831        self.assertEqual(t.grid_location(0, 0), (0, 0))
832        self.assertEqual(t.grid_location(200, 0), (0, 0))
833        self.assertEqual(t.grid_location(201, 0), (1, 0))
834        self.assertEqual(t.grid_location(0, 100), (0, 0))
835        self.assertEqual(t.grid_location(0, 101), (0, 1))
836        self.assertEqual(t.grid_location(201, 101), (1, 1))
837
838    def test_grid_propagate(self):
839        self.assertEqual(self.root.grid_propagate(), True)
840        with self.assertRaises(TypeError):
841            self.root.grid_propagate(False, False)
842        self.root.grid_propagate(False)
843        self.assertFalse(self.root.grid_propagate())
844        f = tkinter.Frame(self.root, width=100, height=100, bg='red')
845        f.grid_configure(row=0, column=0)
846        self.root.update()
847        self.assertEqual(f.winfo_width(), 100)
848        self.assertEqual(f.winfo_height(), 100)
849        f.grid_propagate(False)
850        g = tkinter.Frame(self.root, width=75, height=85, bg='green')
851        g.grid_configure(in_=f, row=0, column=0)
852        self.root.update()
853        self.assertEqual(f.winfo_width(), 100)
854        self.assertEqual(f.winfo_height(), 100)
855        f.grid_propagate(True)
856        self.root.update()
857        self.assertEqual(f.winfo_width(), 75)
858        self.assertEqual(f.winfo_height(), 85)
859
860    def test_grid_size(self):
861        with self.assertRaises(TypeError):
862            self.root.grid_size(0)
863        self.assertEqual(self.root.grid_size(), (0, 0))
864        f = tkinter.Scale(self.root)
865        f.grid_configure(row=0, column=0)
866        self.assertEqual(self.root.grid_size(), (1, 1))
867        f.grid_configure(row=4, column=5)
868        self.assertEqual(self.root.grid_size(), (6, 5))
869
870    def test_grid_slaves(self):
871        self.assertEqual(self.root.grid_slaves(), [])
872        a = tkinter.Label(self.root)
873        a.grid_configure(row=0, column=1)
874        b = tkinter.Label(self.root)
875        b.grid_configure(row=1, column=0)
876        c = tkinter.Label(self.root)
877        c.grid_configure(row=1, column=1)
878        d = tkinter.Label(self.root)
879        d.grid_configure(row=1, column=1)
880        self.assertEqual(self.root.grid_slaves(), [d, c, b, a])
881        self.assertEqual(self.root.grid_slaves(row=0), [a])
882        self.assertEqual(self.root.grid_slaves(row=1), [d, c, b])
883        self.assertEqual(self.root.grid_slaves(column=0), [b])
884        self.assertEqual(self.root.grid_slaves(column=1), [d, c, a])
885        self.assertEqual(self.root.grid_slaves(row=1, column=1), [d, c])
886
887
888tests_gui = (
889    PackTest, PlaceTest, GridTest,
890)
891
892if __name__ == '__main__':
893    run_unittest(*tests_gui)
894