![]() |
ubuntu.com - launchpad.net - ubuntu help
|
|
|||||||
|
Programming Talk This forum is for all programming questions. The questions do not have to be directly related to Ubuntu and any programming language is allowed. |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Python Enthusiast
![]() |
Hi, I would like to know how I can redirect the output of bash commands (e.g. apt-get install vim-gnome) to a TextView widget using pygtk.
For example if I press a button (see the attached code) I would like to call a few commands in bash (os.popen doesn't seem to handle well commands such as "apt-get install vim-gnome" ) and display their output on the TextView widget. If this is not possible with a TextView widget I would appreciate any suggestion about an alternative. An explanation about how I could use python-vte in this case would be very welcome. I have attached the file project.tar.gz which contains the file written in Python (myapp.py) and the .glade file Thanks in advance. |
|
|
|
|
|
#2 | |
|
Ubuntu House Blend
![]() Join Date: Nov 2004
Location: Clemson, SC
Beans: 271
|
Re: pygtk - the output of bash commands on a TextView widget
Quote:
Code:
v = vte.Terminal()
v.fork_command('bash')
v.feed_child('ssh %s tail -f %s.o%s \n' % (host,jname,id))
v.show()
notebook.append_page(v, tab_label=gtk.Label(nodeid))
|
|
|
|
|
|
|
#3 |
|
Python Enthusiast
![]() |
Re: pygtk - the output of bash commands on a TextView widget
Thanks for your reply.
VTE works great now |
|
|
|
|
|
#4 | ||
|
Ubuntu Member
![]() Join Date: Jan 2005
Location: Toronto, Ontario, Canada
Beans: 2,566
Ubuntu 9.04 Jaunty Jackalope
|
Re: pygtk - the output of bash commands on a TextView widget
any way to get rid of the prompt?
eg Code:
v = vte.Terminal()
v.fork_command('bash')
v.feed_child('whoami\n')
v.feed_child('echo test\n')
v.show()
Quote:
Quote:
__________________
Edward A Robinson -- www.earobinson.org |
||
|
|
|
|
|
#5 |
|
Ubuntu Member
![]() Join Date: Jan 2005
Location: Toronto, Ontario, Canada
Beans: 2,566
Ubuntu 9.04 Jaunty Jackalope
|
Re: pygtk - the output of bash commands on a TextView widget
Ok so I have found another problem
Code:
#!/usr/bin/env python import os import gtk import vte import time from subprocess import Popen, PIPE def show_callback(terminal): terminal.feed_child('cd /\n') terminal.feed_child('whoami\n') terminal.feed_child('echo test\n') for ii in range (10): terminal.feed_child('echo ' + str(ii + 1) + '\n') time.sleep(1) def write(terminal, text): x, y = terminal.get_cursor_position() terminal.feed(text + '\n', len(text) + 1) window = gtk.Window() window.connect('destroy', lambda w: gtk.main_quit()) terminal = vte.Terminal() terminal.connect("show", show_callback) child_pid = terminal.fork_command() #write(terminal, '1234567890') window.add(terminal) window.show_all() gtk.main()
__________________
Edward A Robinson -- www.earobinson.org Last edited by earobinson; September 4th, 2007 at 03:17 PM.. |
|
|
|
|
|
#6 |
|
Python Enthusiast
![]() |
Re: pygtk - the output of bash commands on a TextView widget
Here is the solution to your problem.
create a file named "ear1" and make it look like this: Code:
#!/bin/bash
NUM="0"
cd /
whoami
echo test
while [ $NUM -lt 10 ]; do
echo $[$NUM+1]
NUM=$[$NUM + 1]
sleep 1
done
Code:
chmod +x ear1 Code:
#!/usr/bin/env python import os import gtk import vte import time from subprocess import Popen, PIPE def show_callback(terminal): terminal.feed_child('./ear1') def write(terminal, text): x, y = terminal.get_cursor_position() terminal.feed(text + '\n', len(text) + 1) window = gtk.Window() window.connect('destroy', lambda w: gtk.main_quit()) terminal = vte.Terminal() terminal.fork_command() terminal.connect("show", show_callback) #write(terminal, '1234567890') window.add(terminal) window.show_all() gtk.main() |
|
|
|
|
|
#7 |
|
Ubuntu Member
![]() Join Date: Jan 2005
Location: Toronto, Ontario, Canada
Beans: 2,566
Ubuntu 9.04 Jaunty Jackalope
|
Re: pygtk - the output of bash commands on a TextView widget
hum, so there is no way I could have a user inputting commands in one terminal and the results coming out on the other?
UPDATE: The problem is then I need to know all the commands that I want to run when I launch the terminal, there seems to be no way to make it interactactive(sp) or monitor the success of each command I issue to the terminal is there? Ex like a gtk.ProgressBar that monitored each command like synaptic does. Thanks for the feedback
__________________
Edward A Robinson -- www.earobinson.org Last edited by earobinson; September 4th, 2007 at 06:00 PM.. |
|
|
|
|
|
#8 | |
|
Python Enthusiast
![]() |
Re: pygtk - the output of bash commands on a TextView widget
Quote:
I'm not sure as to what you mean by interactive. |
|
|
|
|
|
|
#9 |
|
Ubuntu Member
![]() Join Date: Jan 2005
Location: Toronto, Ontario, Canada
Beans: 2,566
Ubuntu 9.04 Jaunty Jackalope
|
Re: pygtk - the output of bash commands on a TextView widget
I guess the idea in my head was two guis, one that you can type commands into and the other (the terminal) that displays the commands.
maybe this isent possible? But it seems like I should be able to issue commands to the terminal and get a response from them. I have made a simple example., What I would like is to push the press me button and then not be able to interact any more until the command has ran to completion. eventually I would add a gtk.entry and be able to run any command. I guess what your saying is I should send but the program I want to run and then a output log file to signify that the program is done?
__________________
Edward A Robinson -- www.earobinson.org |
|
|
|
|
|
#10 | |
|
Python Enthusiast
![]() |
Re: pygtk - the output of bash commands on a TextView widget
Quote:
here is an example from Envy: Code:
def log_output(self,term):
column,row = self.term.get_cursor_position()
if self.r != row:
off = row-self.r
text = self.term.get_text_range(row-off,0,row-1,-1,self.capture_text)
self.r=row
text = text.strip()
if "\n" not in text or text[-1] != "\n":
text += "\n"
##self.logged += text
self.completetxt.append(text)
a = self.logged
error = re.compile('.*ENVY ERROR.*\n')
success = re.compile('.*ENVY:.*Operation.*Completed.*')
for line in self.completetxt:#self.logged:
m1 = error.match(line)
m2 = success.match(line)
if m1:
self.error = 'error'
sep = ''#write a logfile
logtext = sep.join(self.completetxt)
logfile = open(self.logtxtfile, 'w')
logfile.write(logtext)
logfile.close()
self.errorcheck()
if m2:
self.error = 'noerror'
sep = ''#write a logfile
logtext = sep.join(self.completetxt)
logfile = open(self.logtxtfile, 'w')
logfile.write(logtext)
logfile.close()
self.errorcheck()
You might use strings such as "DONE", etc. so that if you find such a string you can allow the user to type commands again in the text input area. EDIT: in case you're wonder what self.term is: self.term = vte.Terminal() Last edited by tseliot; September 4th, 2007 at 06:47 PM.. |
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|