472,806 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,806 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 9714
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: ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.