473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

TKinter + display of a shell command return

Hello.

Using Tkinter, i would create a widget which display a shell command return.
This return is long, and i would display a real time display (like with the
tail -f commande on Linux)

I think to use the text widget.
I have no problem to execute the command, but how to display, in a
*real-time* fashion the shell retrun?

If you have a track or an idee...

Thanks for your help.
--
Yann.K
Jul 18 '05 #1
4 9855
Yann.K wrote:
Hello.

Using Tkinter, i would create a widget which display a shell command return.
This return is long, and i would display a real time display (like with the
tail -f commande on Linux)

I think to use the text widget.
I have no problem to execute the command, but how to display, in a
*real-time* fashion the shell retrun?


What is your problem here? Inserting at the end of the text and call the see
method on the text widget to make sure the last line is displayed should be
enough. An update_idletasks may also be needed to actually display something,
but it depends on the architecture of your script, typically on whether you use
threads or not.

A basic script doing what you want is:

--shell_output.py---------------------
import os
from Tkinter import *

root = Tk()
t = Text(root)
t.pack()

def go():
p = os.popen("find . -name '*.py'", 'r')
for l in p.xreadlines():
t.insert(END, '%s\n' % l.rstrip())
t.see(END)
t.update_idletasks()

Button(root, text='Go', command=go).pack()

root.mainloop()
--------------------------------------

HTH
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #2
Eric Brunel wrote:
What is your problem here? Inserting at the end of the text and call the
see method on the text widget to make sure the last line is displayed
should be enough. An update_idletasks may also be needed to actually
display something, but it depends on the architecture of your script,
typically on whether you use threads or not.

Yes, really it run great but no as i would!
For long process, the display wait the end of the script execution to
display all the lines of the mesage.

I would that the lines appears as soon as the shell putt the message.
So; if the treatment is very long (ie 10 min), the line of the message
appears every second (in fact h*just when they are forwarder from the
shell.

I would display the shell return like an "tail -f syslog" command on
linux...
I hope to be clearer...

Thanks for your help,

--
Yann.K
Jul 18 '05 #3
Yann.K wrote:
Eric Brunel wrote:

What is your problem here? Inserting at the end of the text and call the
see method on the text widget to make sure the last line is displayed
should be enough. An update_idletasks may also be needed to actually
display something, but it depends on the architecture of your script,
typically on whether you use threads or not.
Yes, really it run great but no as i would!
For long process, the display wait the end of the script execution to
display all the lines of the mesage.


The problem may not be in the Python script, but in the shell command itself:
the output for commands is usually buffered and you won't get a chance to
display anything until the buffer is full or the command explicitely does a
flush. There may be a means to ask the shell to avoid buffering the command
outputs (like Python's -u option), but my shell knowledge does not go as far as
that... And of course, it will depend on the shell flavor you're using...

[...] Thanks for your help,


You're welcome. Good luck!
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #4
"Yann.K" <yk*****@bullier.org> wrote in message news:<ch***********@biggoron.nerim.net>...

Yes, really it run great but no as i would!
For long process, the display wait the end of the script execution to
display all the lines of the mesage.

I would that the lines appears as soon as the shell putt the message.
So; if the treatment is very long (ie 10 min), the line of the message
appears every second (in fact h*just when they are forwarder from the
shell.

I would display the shell return like an "tail -f syslog" command on
linux...
I hope to be clearer...

Thanks for your help,


You can use a tk filehandler to capture the output stream of a shell
command.
Here's a code snippet I used to display the output of a shell command
in a text widget:

from Tkinter import *
import fcntl, popen2, os

(...)
self.text.insert('end', '\nExecuting :\n' + cmd + '\n\n')
self.pp = popen2.Popen4(cmd)
# cmd is of course the shell command
self.mkfilehandler(self.pp, self.get_msg)

def mkfilehandler(self, popen4object, function):
fileobject = popen4object.fromchild
filedescr = fileobject.fileno()
fcntl.fcntl(filedescr, fcntl.F_SETFL, os.O_NONBLOCK)
tkinter.createfilehandler(fileobject, READABLE, function)

def get_msg(self, fileobject, mask):
msg = self.pp.fromchild.read()
if msg == '':
p = self.pp.poll()
if p != -1:
tkinter.deletefilehandler(self.pp.fromchild)
self.pp = None
if p == 0:
tkMessageBox.showinfo(...)
else:
tkMessageBox.showerror(...)
else:
self.text.insert('end', msg)

This works well at least on linux, on windows the createfilehandler()
method may not be available:

http://mail.python.org/pipermail/pyt...ry/089526.html

More information on createfilehandler() can be found at:

http://www.python.org/doc/faq/gui.html#id14

I hope this helps

Michael
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
2
by: Michael Zhang | last post by:
My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images from server and display those images. I created a thread (also a separate toplevel window) for displaying images and another...
2
by: Mark English | last post by:
Is there a safe way to run tkinter in a multithreaded app where the mainloop runs in a background thread ? Here's some test code demonstrating the problem. I'm running Python2.4 under Windows...
5
by: Sean McIlroy | last post by:
hi all i recently wrote a script that implements a puzzle. the interface mostly consists of a bunch of colored disks on a tkinter canvas. the problem is that the disks change their colors in...
3
by: Jo Schambach | last post by:
I am trying to write a GUI with tkinter that displays the stdout from a regular C/C++ program in a text widget. The idea i was trying to use was as follows: 1) use "popen" to execute the C/C++...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
1
by: C D Wood | last post by:
To whom this may concern, Below is the source code, which demonstrates a problem I am having making a GUI for my python project work. 'table.txt' is a file that is read from the same folder....
5
by: H J van Rooyen | last post by:
Hi, I am struggling to get the pack method to do what I intend. I am trying to display user input in a seperate window, along with a little description of the field, something like this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.