Skip to content

Commit e653387

Browse files
gh-153716: Scan for the ttk Spinbox and Combobox arrow, not a fixed pixel
The arrow-click test helpers probed a fixed x = width - 5, which at a high display scaling on Tk 9.1 lands in the arrow element's right-hand border (the border scales with the DPI), so identify() there returns 'field' and the test fails. Scan inward from the right edge for the arrow element instead. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0f3b424 commit e653387

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

Lib/test/test_ttk/test_widgets.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ def setUp(self):
351351
def create(self, **kwargs):
352352
return ttk.Entry(self.root, **kwargs)
353353

354+
def _arrow_x(self, widget, y, arrow):
355+
# Return an x coordinate on the up/down *arrow* element in row y,
356+
# found by scanning inward from the right edge of the widget. A
357+
# fixed inset such as width - 5 lands in the arrow element's
358+
# right-hand border, which scales with the display scaling, so at a
359+
# high DPI identify() there returns 'field' instead of the arrow.
360+
self.assertTrue(wait_until_mapped(widget, full_size=True))
361+
width = widget.winfo_width()
362+
for x in range(width - 1, width // 2, -1):
363+
if widget.identify(x, y).endswith(arrow):
364+
return x
365+
self.fail(f'no {arrow} found in row {y}')
366+
354367
def test_configure_invalidcommand(self):
355368
widget = self.create()
356369
self.checkCommandParam(widget, 'invalidcommand')
@@ -491,10 +504,11 @@ def test_configure_height(self):
491504
self.checkParams(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i')
492505

493506
def _show_drop_down_listbox(self):
494-
width = self.combo.winfo_width()
495-
x, y = width - 5, 5
507+
y = 5
496508
if sys.platform != 'darwin': # there's no down arrow on macOS
497-
self.assertRegex(self.combo.identify(x, y), r'.*downarrow\z')
509+
x = self._arrow_x(self.combo, y, 'downarrow')
510+
else:
511+
x = self.combo.winfo_width() - 5
498512
self.combo.event_generate('<Button-1>', x=x, y=y)
499513
self.combo.event_generate('<ButtonRelease-1>', x=x, y=y)
500514

@@ -1300,22 +1314,16 @@ def create(self, **kwargs):
13001314

13011315
def _click_increment_arrow(self):
13021316
self.require_mapped(self.spin)
1303-
width = self.spin.winfo_width()
1304-
height = self.spin.winfo_height()
1305-
x = width - 5
1306-
y = height//2 - 5
1307-
self.assertRegex(self.spin.identify(x, y), r'.*uparrow\z')
1317+
y = self.spin.winfo_height()//2 - 5
1318+
x = self._arrow_x(self.spin, y, 'uparrow')
13081319
self.spin.event_generate('<ButtonPress-1>', x=x, y=y)
13091320
self.spin.event_generate('<ButtonRelease-1>', x=x, y=y)
13101321
self.spin.update_idletasks()
13111322

13121323
def _click_decrement_arrow(self):
13131324
self.require_mapped(self.spin)
1314-
width = self.spin.winfo_width()
1315-
height = self.spin.winfo_height()
1316-
x = width - 5
1317-
y = height//2 + 4
1318-
self.assertRegex(self.spin.identify(x, y), r'.*downarrow\z')
1325+
y = self.spin.winfo_height()//2 + 4
1326+
x = self._arrow_x(self.spin, y, 'downarrow')
13191327
self.spin.event_generate('<ButtonPress-1>', x=x, y=y)
13201328
self.spin.event_generate('<ButtonRelease-1>', x=x, y=y)
13211329
self.spin.update_idletasks()

0 commit comments

Comments
 (0)