Skip to content

Commit 2e2e2f6

Browse files
dogeekRedFantom
authored andcommitted
Extend newly added utilities unit tests
- Move widget with binding on parent - Coordinates in box fixed - Move widget with binding
1 parent 0cf97fa commit 2e2e2f6

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

tests/test_utilities.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88

99
class TestUtilities(BaseWidgetTest):
10+
def _dummy_bind(self, event):
11+
pass
12+
1013
def test_move_widget(self):
1114
label = ttk.Label(self.window)
1215
tl = tk.Toplevel(self.window)
@@ -41,24 +44,38 @@ def test_move_widget_place(self):
4144
self.assertIn(label, tl.place_slaves())
4245

4346
def test_move_widget_with_binding(self):
44-
raise NotImplementedError
47+
label = ttk.Label(self.window)
48+
label.bind('<Enter>', self._dummy_bind)
49+
label.pack()
50+
tl = tk.Toplevel(self.window)
51+
label = move_widget(label, tl)
52+
label.pack()
53+
self.assertTrue(label.winfo_parent() == '.' + tl.winfo_name())
54+
self.assertIn('<Enter>', label.bind())
4555

4656
def test_move_widget_with_binding_on_parent(self):
47-
raise NotImplementedError
57+
self.window.bind('<Enter>', self._dummy_bind)
58+
label = ttk.Label(self.window)
59+
label.pack()
60+
tl = tk.Toplevel(self.window)
61+
label = move_widget(label, tl)
62+
label.pack()
63+
self.assertTrue(label.winfo_parent() == '.' + tl.winfo_name())
64+
self.assertIn('<Enter>', tl.bind())
4865

4966
def test_parse_geometry(self):
5067
g = parse_geometry('1x1+1+1')
5168
self.assertEqual(g, (1, 1, 1, 1))
5269
g = parse_geometry('1x1-1-1')
53-
self.assertEqual(g, (1, 1, -1, -1))
70+
self.assertEqual(g, (-1, -1, 1, 1))
5471

5572
def test_coordinates_in_box(self):
5673
with self.assertRaises(ValueError):
5774
coords_in_box((1,), (1, 1, 3, 3))
5875

5976
with self.assertRaises(ValueError):
6077
coords_in_box((1, 1), (1, 1, 3, 3, 4))
61-
78+
6279
self.assertTrue(coords_in_box((1, 1), (0, 0, 2, 2)))
6380
self.assertFalse(coords_in_box((1, 1), (1, 1, 2, 2), include_edges=False))
64-
self.assertTrue(coords_in_box((0, 0), (-1, -1, 1, 1)))
81+
self.assertTrue(coords_in_box((0, 0), (-1, -1, 1, 1), bbox_is_x1y1x2y2=True))

ttkwidgets/utilities.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def parse_geometry(geometry):
101101
int(match.group(1)), int(match.group(2)))
102102

103103

104-
def coords_in_box(coords, bbox, include_edges=True):
104+
def coords_in_box(coords, bbox, include_edges=True, bbox_is_x1y1x2y2=False):
105105
"""
106106
Checks whether coords are inside bbox
107107
@@ -111,6 +111,9 @@ def coords_in_box(coords, bbox, include_edges=True):
111111
:type bbox: tuple
112112
:param include_edges: default True whether to include the edges
113113
:type include_edges: bool
114+
:param bbox_is_x1y1x2y2: default False whether the bbox is in
115+
(x, y, width, height) or (x1, y1, x2, y2) format
116+
:type bbox_is_x1y1x2y2: bool
114117
:returns: whether coords is inside bbox
115118
:rtype: bool
116119
:raises: ValueError if length of bbox or coords do not match the specifications
@@ -121,11 +124,13 @@ def coords_in_box(coords, bbox, include_edges=True):
121124
raise ValueError("Bbox argument is supposed to be of length 4")
122125

123126
x, y = coords
124-
xmin, ymin, width, height = bbox
125-
xmax, ymax = xmin + width, ymin + height
127+
if bbox_is_x1y1x2y2:
128+
xmin, ymin, xmax, ymax = bbox
129+
else:
130+
xmin, ymin, width, height = bbox
131+
xmax, ymax = xmin + width, ymin + height
132+
126133
if include_edges:
127-
xmin = max(xmin - 1, 0)
128-
xmax += 1
129-
ymin = max(ymin - 1, 0)
130-
ymax += 1
131-
return xmin < x < xmax and ymin < y < ymax
134+
return xmin <= x <= xmax and ymin <= y <= ymax
135+
else:
136+
return xmin < x < xmax and ymin < y < ymax

0 commit comments

Comments
 (0)