@@ -794,3 +794,53 @@ some widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a
794794reference to the image. When the last Python reference to the image object is
795795deleted, the image data is deleted as well, and Tk will display an empty box
796796wherever 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