FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-151878: Add tkinter Entry and Spinbox validate methods by serhiy-storchaka · Pull Request #151879 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 16 additions & 0 deletions Doc/library/tkinter.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4254,6 +4254,14 @@ Widget classes
Typically associated with mouse motion events, to produce the effect of
dragging the entry at high speed through the window.

.. method:: validate()

Force an evaluation of the command given by the *validatecommand* option,
independently of the conditions specified by the *validate* option, and
return whether the value is considered valid.

.. versionadded:: next


.. class:: Frame(master=None, cnf={}, **kw)

Expand Down Expand Up @@ -5206,6 +5214,14 @@ Widget classes

.. versionadded:: 3.8

.. method:: validate()

Force an evaluation of the command given by the *validatecommand* option,
independently of the conditions specified by the *validate* option, and
return whether the value is considered valid.

.. versionadded:: next



.. class:: Text(master=None, cnf={}, **kw)
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.16.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ tkinter
synchronization of the displayed view with the underlying text.
(Contributed by Serhiy Storchaka in :gh:`151675`.)

* Added a :meth:`!validate` method to the :class:`!tkinter.Entry` and
:class:`!tkinter.Spinbox` widgets, which forces an evaluation of the
validation command.
(Contributed by Serhiy Storchaka in :gh:`151878`.)

* Added new window-management methods :meth:`~tkinter.Misc.winfo_isdark`
(dark mode detection), :meth:`~tkinter.Wm.wm_iconbadge` (application icon
badge) and :meth:`~tkinter.Wm.wm_stackorder` (toplevel stacking order).
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_tkinter/test_widgets.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,25 @@ def test_select_aliases(self):
self.assertRaisesRegex(TclError, 'bad entry index "xyz"',
widget.select_range, 'xyz', 'end')

def test_validate(self):
calls = []
def validatecommand(value):
calls.append(value)
return value.isdigit()
# validate='none' means validation is never triggered automatically,
# so validate() exercises the forced evaluation.
widget = self.create(validate='none',
validatecommand=(self.root.register(validatecommand), '%P'))
widget.insert(0, '123')
result = widget.validate()
self.assertIs(result, True)
self.assertEqual(calls, ['123'])
widget.delete(0, 'end')
widget.insert(0, 'abc')
calls.clear()
self.assertIs(widget.validate(), False)
self.assertEqual(calls, ['abc'])


@add_configure_tests(StandardOptionsTests)
class SpinboxTest(EntryTest, unittest.TestCase):
Expand Down
16 changes: 16 additions & 0 deletions Lib/tkinter/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3445,6 +3445,14 @@ def selection_to(self, index):

select_to = selection_to

def validate(self):
"""Force an evaluation of the validation command.

This evaluates the command given by the validatecommand option,
independently of the conditions specified by the validate option.
Return whether the value is considered valid."""
return self.tk.getboolean(self.tk.call(self._w, 'validate'))


class Frame(Widget):
"""Frame widget which may contain other widgets and can have a 3D border."""
Expand Down Expand Up @@ -4957,6 +4965,14 @@ def selection_to(self, index):
"""Set the variable end of a selection to INDEX."""
self.selection('to', index)

def validate(self):
"""Force an evaluation of the validation command.

This evaluates the command given by the validatecommand option,
independently of the conditions specified by the validate option.
Return whether the value is considered valid."""
return self.tk.getboolean(self.tk.call(self._w, 'validate'))

###########################################################################


Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add the :meth:`!validate` method to the :class:`tkinter.Entry` and
:class:`tkinter.Spinbox` widgets, forcing an evaluation of the validation
command.
Loading

Back | FazBrowse Home | New Git URL