[Tutor] Displaying data produced from commandline in Tkinter

Michael P. Reilly arcege@speakeasy.net
Sun, 21 Oct 2001 13:13:03 -0400


On Sun, Oct 21, 2001 at 06:42:32PM +0200, Sharriff Aina wrote:
> Hi guys! my Grayson Tkinter book is still in backorder :-(, but in the meantime, can someone tel me how to display data that continuously changes on the commandline in Tkinter? I have an idea how that could be don for a command that is run and piped to tkinter, but not if the commandline process verbosely outputs comments. Ideas?
> 

You probably want to look into the createfilehandler() function in the
Tkinter._tkinter module.


import os, Tkinter

# keep sending any lines that are written to /var/log/messages
file = os.popen('tail -f /var/log/messages')
textbox = Text(None)
textbox.pack()
def getline(inputfile, mask, widget=textbox):
  line = inputfile.readline()
  widget.insert(Tkinter.END, line)  # line already has a newline

Tkinter._tkinter.createfilehandler(file, Tkinter._tkinter.READABLE, getline)
textbox.mainloop()

Each time through the mainloop, Tkinter will also check to see if there
is data coming from the file you specify and the function specified is
called with the data file and with a mask (READABLE, WRITABLE, etc.).

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |