ยปCore Development>Code coverage>Lib/tkinter/test/test_tkinter/test_geometry_managers.py

Python code coverage for Lib/tkinter/test/test_tkinter/test_geometry_managers.py

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