在Gtk Textview中打印bash命令结果

时间:2014-10-19 21:37:22

标签: python bash gtk

我想使用bash命令打印文件属性并在Textview中写入结果。 此处路径通过选择按钮选择。文件路径存储在文本中。 os.system(“stat”+ text +“”)在终端上打印。我想存储该命令的结果。

#/usr/bin/python
import pygtk,gtk,os

class project:
  def enter_callback(self,widget,entry):
    entry_text=entry.get_text()
    print("entry contents: %s \n" % entry_text)
    
  def file_ok_sel(self,w):
    print("%s " % self.filew.get_filename())
    text=self.filew.get_filename()
    print(text)    
    os.system("stat " + text + "") 
    
  def button1_select(self,widget,data=None):
    self.filew=gtk.FileSelection("File selection")
    self.filew.connect("destroy",self.destroy)
    self.filew.ok_button.connect("clicked",self.file_ok_sel)
    self.filew.cancel_button.connect("clicked",lambda w:self.filew.destroy())
    self.filew.set_filename("penguin.png")
    self.filew.show()
    
  def destroy(self,widget):
    print("quit")    
  
  def __init__(self):
    self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.set_title("printing")
    self.window.connect("delete_event",lambda a1,a2:gtk.main_quit())
    self.window.set_border_width(20)      
    vbox=gtk.VBox(True,2)
    self.window.add(vbox)  
    
    label=gtk.Label("Select File")
    label.set_alignment(0,0)
    vbox.pack_start(label,False,False,0)
    label.show()
    
   #adding TEXTVIEW to print bash command result
    textview = gtk.TextView()
    textbuffer = textview.get_buffer()
    self.window.add(textview)
    
    vbox.pack_start(textview,True,True,0)
    textview.show()
    vbox.show()
    self.window.show()
    
    #Button used to select FILE
    button=gtk.Button("select")
    button.connect("clicked",self.button1_select,"select 1")    
    vbox.pack_start(button,True,True,2)
    button.show()
    
def main():
  gtk.main()
  return 0

if __name__=="__main__":
  project()
  main()
  
    

1 个答案:

答案 0 :(得分:1)

init添加

self.textbuffer = textview.get_buffer()

然后,当您想要更新它时,请执行以下操作:

self.result = os.system("stat"+text+"")
self.textbuffer.set_text(self.result)`