Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d986563

Browse files
committed
Issue #22155: Add File Handlers subsection with createfilehandler to tkinter
doc. Remove obsolete example from FAQ. Patch by Martin Panter.
1 parent 0e8168c commit d986563

2 files changed

Lines changed: 53 additions & 22 deletions

File tree

Doc/faq/gui.rst

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,30 +139,11 @@ might include the Tix libraries as well).
139139
Can I have Tk events handled while waiting for I/O?
140140
---------------------------------------------------
141141

142-
Yes, and you don't even need threads! But you'll have to restructure your I/O
142+
On platforms other than Windows, yes, and you don't even
143+
need threads! But you'll have to restructure your I/O
143144
code a bit. Tk has the equivalent of Xt's :c:func:`XtAddInput()` call, which allows you
144145
to register a callback function which will be called from the Tk mainloop when
145-
I/O is possible on a file descriptor. Here's what you need::
146-
147-
from Tkinter import tkinter
148-
tkinter.createfilehandler(file, mask, callback)
149-
150-
The file may be a Python file or socket object (actually, anything with a
151-
fileno() method), or an integer file descriptor. The mask is one of the
152-
constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
153-
follows::
154-
155-
callback(file, mask)
156-
157-
You must unregister the callback when you're done, using ::
158-
159-
tkinter.deletefilehandler(file)
160-
161-
Note: since you don't know *how many bytes* are available for reading, you can't
162-
use the Python file object's read or readline methods, since these will insist
163-
on reading a predefined number of bytes. For sockets, the :meth:`recv` or
164-
:meth:`recvfrom` methods will work fine; for other files, use
165-
``os.read(file.fileno(), maxbytecount)``.
146+
I/O is possible on a file descriptor. See :ref:`tkinter-file-handlers`.
166147

167148

168149
I can't get key bindings to work in Tkinter: why?

Doc/library/tkinter.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,3 +794,53 @@ some widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a
794794
reference to the image. When the last Python reference to the image object is
795795
deleted, the image data is deleted as well, and Tk will display an empty box
796796
wherever the image was used.
797+
798+
799+
.. _tkinter-file-handlers:
800+
801+
File Handlers
802+
-------------
803+
804+
Tk allows you to register and unregister a callback function which will be
805+
called from the Tk mainloop when I/O is possible on a file descriptor.
806+
Only one handler may be registered per file descriptor. Example code::
807+
808+
import tkinter
809+
widget = tkinter.Tk()
810+
mask = tkinter.READABLE | tkinter.WRITABLE
811+
widget.tk.createfilehandler(file, mask, callback)
812+
...
813+
widget.tk.deletefilehandler(file)
814+
815+
This feature is not available on Windows.
816+
817+
Since you don't know how many bytes are available for reading, you may not
818+
want to use the :class:`~io.BufferedIOBase` or :class:`~io.TextIOBase`
819+
:meth:`~io.BufferedIOBase.read` or :meth:`~io.IOBase.readline` methods,
820+
since these will insist on reading a predefined number of bytes.
821+
For sockets, the :meth:`~socket.socket.recv` or
822+
:meth:`~socket.socket.recvfrom` methods will work fine; for other files,
823+
use raw reads or ``os.read(file.fileno(), maxbytecount)``.
824+
825+
826+
.. method:: Widget.tk.createfilehandler(file, mask, func)
827+
828+
Registers the file handler callback function *func*. The *file* argument
829+
may either be an object with a :meth:`~io.IOBase.fileno` method (such as
830+
a file or socket object), or an integer file descriptor. The *mask*
831+
argument is an ORed combination of any of the three constants below.
832+
The callback is called as follows::
833+
834+
callback(file, mask)
835+
836+
837+
.. method:: Widget.tk.deletefilehandler(file)
838+
839+
Unregisters a file handler.
840+
841+
842+
.. data:: READABLE
843+
WRITABLE
844+
EXCEPTION
845+
846+
Constants used in the *mask* arguments.

0 commit comments

Comments
 (0)