Skip to content

Commit 6cbb225

Browse files
gh-151886: Add tkinter Misc.tk_appname, tk_useinputmethods and tk_caret (GH-151887)
Wrap three long-standing Tk commands that had no tkinter wrapper: * Misc.tk_appname() queries or sets the name used to communicate with the application through the send command (Tk 8.5). * Misc.tk_useinputmethods() queries or sets whether Tk uses the X Input Methods (XIM) for filtering events (Tk 8.5). * Misc.tk_caret() sets or queries the per-display caret location used for accessibility and for placing the input method window (Tk 8.5). Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 34eca5a commit 6cbb225

5 files changed

Lines changed: 105 additions & 0 deletions

File tree

Doc/library/tkinter.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,37 @@ Base and mixin classes
19551955
color change when the mouse passes over a slider).
19561956
Return the resulting setting.
19571957

1958+
.. method:: tk_appname(name=None)
1959+
1960+
Query or set the name used to communicate with this application through
1961+
the ``send`` command.
1962+
With no argument, return the current name; otherwise change it to *name*
1963+
and return the actual name set, which may have a suffix appended to keep
1964+
it unique among the applications on the display.
1965+
1966+
.. versionadded:: next
1967+
1968+
.. method:: tk_useinputmethods(boolean=None, *, displayof=0)
1969+
1970+
Query or set whether Tk uses the X Input Methods (XIM) for filtering
1971+
events, and return the resulting state.
1972+
This is significant only on X11; if XIM support is not available it
1973+
always returns ``False``.
1974+
1975+
.. versionadded:: next
1976+
1977+
.. method:: tk_caret(*, x=None, y=None, height=None)
1978+
1979+
Set or query the caret location for the widget's display.
1980+
The caret is the per-display insertion position used for global focus
1981+
indication (for accessibility) and for placing the input method
1982+
(XIM or IME) window.
1983+
With no argument, return the current location as a dictionary with the
1984+
keys ``'x'``, ``'y'`` and ``'height'``; otherwise update the given
1985+
coordinates.
1986+
1987+
.. versionadded:: next
1988+
19581989
.. method:: tk_scaling(number=None, *, displayof=0)
19591990

19601991
Query or set the scaling factor used by Tk to convert between physical

Doc/whatsnew/3.16.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ tkinter
194194
badge) and :meth:`~tkinter.Wm.wm_stackorder` (toplevel stacking order).
195195
(Contributed by Serhiy Storchaka in :gh:`151874`.)
196196

197+
* Added the :meth:`~tkinter.Misc.tk_appname`,
198+
:meth:`~tkinter.Misc.tk_useinputmethods` and :meth:`~tkinter.Misc.tk_caret`
199+
methods, exposing the application send name, the X Input Methods state and
200+
the input method caret location.
201+
(Contributed by Serhiy Storchaka in :gh:`151886`.)
202+
197203
* Added support for more options in :class:`!tkinter.PhotoImage` methods: the
198204
*format* parameter of :meth:`~tkinter.PhotoImage.put`, the *metadata*
199205
parameter of :meth:`~tkinter.PhotoImage.put`, :meth:`~tkinter.PhotoImage.read`,

Lib/test/test_tkinter/test_misc.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,29 @@ def test_tk_bisque(self):
463463
self.assertEqual(root['background'], '#ffe4c4')
464464
self.assertRaises(TypeError, root.tk_bisque, 'x')
465465

466+
def test_tk_appname(self):
467+
old = self.root.tk_appname()
468+
self.assertIsInstance(old, str)
469+
self.addCleanup(self.root.tk_appname, old)
470+
# Setting the name returns the actual name (possibly with a suffix
471+
# appended to keep it unique).
472+
new = self.root.tk_appname('PythonTkTest')
473+
self.assertIsInstance(new, str)
474+
self.assertEqual(self.root.tk_appname(), new)
475+
476+
def test_tk_useinputmethods(self):
477+
old = self.root.tk_useinputmethods()
478+
self.assertIsInstance(old, bool)
479+
self.addCleanup(self.root.tk_useinputmethods, old)
480+
# Setting returns the resulting state. On systems without XIM support
481+
# the state is always False, so only check the True->False direction.
482+
self.assertIs(self.root.tk_useinputmethods(False), False)
483+
484+
def test_tk_caret(self):
485+
self.assertIsNone(self.root.tk_caret(x=5, y=10, height=20))
486+
caret = self.root.tk_caret()
487+
self.assertEqual(caret, {'x': 5, 'y': 10, 'height': 20})
488+
466489
def test_tk_scaling(self):
467490
old = self.root.tk_scaling()
468491
self.assertIsInstance(old, float)

Lib/tkinter/__init__.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,47 @@ def tk_setPalette(self, *args, **kw):
745745
self.tk.call(('tk_setPalette',)
746746
+ _flatten(args) + _flatten(list(kw.items())))
747747

748+
def tk_appname(self, name=None):
749+
"""Query or set the name used to communicate with this application
750+
through the send command.
751+
752+
With no argument, return the current name; otherwise change it to NAME
753+
and return the actual name set (which may have a suffix appended to
754+
keep it unique)."""
755+
if name is None:
756+
return self.tk.call('tk', 'appname')
757+
return self.tk.call('tk', 'appname', name)
758+
759+
def tk_useinputmethods(self, boolean=None, *, displayof=0):
760+
"""Query or set whether Tk uses the X Input Methods (XIM) for filtering
761+
events, and return the resulting state.
762+
763+
This is significant only on X11; if XIM support is not available it
764+
always returns False."""
765+
args = ('tk', 'useinputmethods') + self._displayof(displayof)
766+
if boolean is not None:
767+
args += (boolean,)
768+
return self.tk.getboolean(self.tk.call(args))
769+
770+
def tk_caret(self, *, x=None, y=None, height=None):
771+
"""Set or query the caret location for this widget's display.
772+
773+
The caret is the per-display insertion position used for global focus
774+
indication (for accessibility) and for placing the input method
775+
(XIM or IME) window. With no argument, return the current location as
776+
a dictionary with keys 'x', 'y' and 'height'; otherwise update the
777+
given coordinates."""
778+
args = ('tk', 'caret', self._w)
779+
for option, value in (('-x', x), ('-y', y), ('-height', height)):
780+
if value is not None:
781+
args += (option, value)
782+
if len(args) > 3:
783+
self.tk.call(args)
784+
else:
785+
values = self.tk.splitlist(self.tk.call(args))
786+
return {values[i][1:]: self.tk.getint(values[i + 1])
787+
for i in range(0, len(values), 2)}
788+
748789
def tk_scaling(self, number=None, *, displayof=0):
749790
"""Query or set the scaling factor used by Tk to convert between
750791
physical units and pixels.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add the :meth:`!tkinter.Misc.tk_appname`,
2+
:meth:`!tkinter.Misc.tk_useinputmethods` and :meth:`!tkinter.Misc.tk_caret`
3+
methods, wrapping the ``tk appname``, ``tk useinputmethods`` and ``tk caret``
4+
Tk commands.

0 commit comments

Comments
 (0)