non-GUI event detection in Tkinter mainloop()
I'm trying to figure out if Python and Tkinter can offer a solution to my following problem. I want to monitor a file and when that file is changed, I want to detect that event and have my window change its text from "File Unchanged" to "File has Changed." That is, I can produce a function to periodically sample the modification time of a file and if it is different than my last sample time, then this function will return an indication that the file is changed. I want this file-change event to be captured by the tkinit mainloop(), which I will then use to change the appearance of my window. I understand that any GUI event (mouse, keyboard, etc.) can be detected in the mainloop(), but I have seen no indication in the documentation and discussions that a file-change can produce an event that can be detected in the mainloop(). Does anyone know if I can do this? If so, how? Or, if there is an alternative way than mainloop() to get a window to change upon a file-change, that would be helpful, too.
The only way I know of to do this is to use "after" to invoke a function which does the file checking and whatever else is required. That function sets another "after" - with perhaps a one second delay or whatever seems appropriate - to invoke itself. Thus the function is invoked repeatedly to keep track of the file. It isn't pretty but it works. Cam Farnell O'Gorman, Lawrence (Larry) wrote:
I’m trying to figure out if Python and Tkinter can offer a solution to my following problem. I want to monitor a file and when that file is changed, I want to detect that event and have my window change its text from “File Unchanged” to “File has Changed.”
That is, I can produce a function to periodically sample the modification time of a file and if it is different than my last sample time, then this function will return an indication that the file is changed. I want this file-change event to be captured by the tkinit mainloop(), which I will then use to change the appearance of my window.
I understand that any GUI event (mouse, keyboard, etc.) can be detected in the mainloop(), but I have seen no indication in the documentation and discussions that a file-change can produce an event that can be detected in the mainloop(). Does anyone know if I can do this? If so, how? Or, if there is an alternative way than mainloop() to get a window to change upon a file-change, that would be helpful, too.
Hi Lawrence, File system events are not a part of tk library so Tkinter doesn't process such events. Nevertheless native _tkinter library contains two file event related procedures - createfilehandler() and deletefilehandler() So you can try finding samples how to use these functions. For example: http://mail.python.org/pipermail/python-list/1999-May/001838.html Also you can try resolving this issue using "fileevent" tcl command: http://www.tcl.tk/man/tcl8.5/TclCmd/fileevent.htm But this way supposes tcl programming. Regards, Igor Novikov sK1 Project http://sk1project.org On Wed, May 20, 2009 at 1:08 AM, O'Gorman, Lawrence (Larry) < larry.ogorman@alcatel-lucent.com> wrote:
I’m trying to figure out if Python and Tkinter can offer a solution to my following problem. I want to monitor a file and when that file is changed, I want to detect that event and have my window change its text from “File Unchanged” to “File has Changed.”
That is, I can produce a function to periodically sample the modification time of a file and if it is different than my last sample time, then this function will return an indication that the file is changed. I want this file-change event to be captured by the tkinit mainloop(), which I will then use to change the appearance of my window.
I understand that any GUI event (mouse, keyboard, etc.) can be detected in the mainloop(), but I have seen no indication in the documentation and discussions that a file-change can produce an event that can be detected in the mainloop(). Does anyone know if I can do this? If so, how? Or, if there is an alternative way than mainloop() to get a window to change upon a file-change, that would be helpful, too.
_______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
On Wed, May 20, 2009 at 12:25:08PM +0300, Igor Novikov wrote: . . .
File system events are not a part of tk library so Tkinter doesn't process such events. Nevertheless native _tkinter library contains two file event related procedures - createfilehandler() and deletefilehandler() So you can try finding samples how to use these functions. For example:
http://mail.python.org/pipermail/python-list/1999-May/001838.html
Also you can try resolving this issue using "fileevent" tcl command:
http://www.tcl.tk/man/tcl8.5/TclCmd/fileevent.htm
But this way supposes tcl programming. . . . There are arguably easier ways. It might be the weekend before I can explain. My summary: Mr. O'Gorman can try Mr. Novikov's sug- gestions, OR wait for the possibility of a more completed approach.
Yes, you are right. Solution without Tkinter usage is also possible. File watcher can be created on pure Python implementation: http://www.amk.ca/python/simple/dirwatch.html Sorry I forgot this issue because this thread is a [Tkinter-discuss] :) Regards, Igor Novikov sK1 Project http://sk1project.org On Wed, May 20, 2009 at 7:33 PM, Cameron Laird <Cameron@phaseit.net> wrote:
File system events are not a part of tk library so Tkinter doesn't
On Wed, May 20, 2009 at 12:25:08PM +0300, Igor Novikov wrote: . . . process
such events. Nevertheless native _tkinter library contains two file event related procedures - createfilehandler() and deletefilehandler() So you can try finding samples how to use these functions. For example:
http://mail.python.org/pipermail/python-list/1999-May/001838.html
Also you can try resolving this issue using "fileevent" tcl command:
http://www.tcl.tk/man/tcl8.5/TclCmd/fileevent.htm
But this way supposes tcl programming. . . . There are arguably easier ways. It might be the weekend before I can explain. My summary: Mr. O'Gorman can try Mr. Novikov's sug- gestions, OR wait for the possibility of a more completed approach.
participants (4)
-
Cam Farnell -
Cameron Laird -
Igor Novikov -
O'Gorman, Lawrence (Larry)