title: Display content then manually refresh in PyGTK (2)
body: <p>Having trouble understanding how to manually (I'm not using timeouts at this stage) refresh a window, or part of a window in PyGTK, version 2.</p>
  
  <p>I want to display some information, in this example, a selection of output from the "df" command, and then be able to manually refresh the window, using a "refresh" button. I've looked at various examples and the PyGTK tutorial, but the exact place to put the refresh code escapes me. Tried using queue_draw in the "freedisk" function, in another function within that, in the "main" loop, without success.</p>
  
  <p>I appreciate GTK3 is the preferred version these days, but I'd rather get this right, before updating. </p>
  
  <p>The code for "freedisk":</p>
  
  <pre><code>#!/usr/bin/env python
  
  import pango, pygtk, subprocess, re, string, decimal, time
  pygtk.require('2.0')
  import gtk
  
  def yield_lines(data):
      for line in data.split("\n"):
          yield line
  
  def line_to_list(line):
      return re.sub(" +", " ", line).split()
  
  def remov_percent(col):
      return re.sub("%", "", col)
  
  
  class freedisk:
      def getdiskfree(self):
          diskfreecmd = ['/usr/bin/df', '-hlT', '-x', 'tmpfs']
          diskfree = subprocess.Popen(diskfreecmd, stdout=subprocess.PIPE).communicate()[0]
          return diskfree
  
      def delete_event(self, widget, event, data=None):
          gtk.main_quit()
          return False
  
      def destroy(self, widget, data=None):
          gtk.main_quit()
  
      def __init__(self):
          window = gtk.Window(gtk.WINDOW_TOPLEVEL)
          window.connect("delete_event", self.delete_event)
  
          box1 = gtk.VBox(False, 10)
          box1a = gtk.HBox(False, 0)
          box2 = gtk.VBox(False, 10)
          box3 = gtk.HBox(False, 0)
  
          frame_1 = gtk.Frame()
  
          window.add(box1)
  
          diskfreedata = self.getdiskfree()
          diskfreedata = diskfreedata.replace("Mounted on", "Mounted_on")
  
          lines = yield_lines(diskfreedata)
  
          headers = line_to_list(lines.next())
  
          columns = [list() for i in range(len(headers))]
  
          for i,h in enumerate(headers):
              columns[i].append(h)
  
          for line in lines:
              for i,l in enumerate(line_to_list(line)):
                  columns[i].append(l)
  
          j=0
          for i in columns[0]:
              j += 1
  
          title = gtk.Label('Disk usage and free space')
          box1.pack_start(title, False, False, 10)
          box1.pack_start(box1a, True, True, 0)
          box1a.pack_start(frame_1, True, True, 20)
          frame_1.add(box2)
  
          ucdiskfreedata = unicode(diskfreedata)
  
          label = gtk.Label(diskfreedata)
          label.set_line_wrap(False)
          label.set_max_width_chars(80)
          label.modify_font(pango.FontDescription("monospace"))
  
          box_sp1 = gtk.HBox(False, 0)
          box_sp2 = gtk.HBox(False, 0)
  
          refresh_button = gtk.Button("Refresh")
  
          close_button = gtk.Button("Close")
          close_button.connect("clicked", self.delete_event, "quit")
  
          mntlabel = [list() for i in range(j)]
          sizelabel = [list() for i in range(j)]
          progbar = [list() for i in range(j)]
          freelabel = [list() for i in range(j)]
          perlabel = [list() for i in range(j)]
  
          rows = [list() for i in range(j)]
  
          box2.pack_start(box_sp1, False, False, 0)
  
          rows[0] = gtk.HBox(True, 0)
          box2.pack_start(rows[0], False, False, 5)
  
          mntlabel[0] = gtk.Label(columns[6][0])
          freelabel[0] = gtk.Label(columns[4][0])
          sizelabel[0] = gtk.Label(columns[2][0])
          blank = gtk.Label(columns[5][0])
          rows[0].pack_start(mntlabel[0], False, False, 0)
          rows[0].pack_start(sizelabel[0], False, False, 0)
          rows[0].pack_start(blank, False, False, 0)
          rows[0].pack_start(freelabel[0], False, False, 0)
  
          for i in range(1,j):
              x = remov_percent(columns[5][i])
              y = decimal.Decimal(x) /  decimal.Decimal('100') 
              mntlabel[i] = gtk.Label(columns[6][i])
              mntlabel[i].set_justify(gtk.JUSTIFY_RIGHT)
              freelabel[i] = gtk.Label(columns[4][i])
              sizelabel[i] = gtk.Label(columns[2][i])
              progbar[i] = gtk.ProgressBar()
              gtk.ProgressBar.set_fraction(progbar[i], y)
              progbar[i].set_text(str(columns[5][i]))
              rows[i] = gtk.HBox(True, 0)
              rows[i].pack_start(mntlabel[i], False, False, 0)
              rows[i].pack_start(sizelabel[i], False, False, 0)
              rows[i].pack_start(progbar[i], False, False, 0)
              rows[i].pack_start(freelabel[i], False, False, 0)
              box2.pack_start(rows[i], False, False, 0)
  
              box2.pack_start(box_sp2, False, False, 10)
  
              box3.pack_end(close_button, False, False, 20)
              box3.pack_end(refresh_button, False, False, 20)
              box1.pack_start(box3, False, False, 10)
  
              box1.show()
              box1a.show()
              box2.show()
              box_sp1.show()
              box_sp2.show()
              frame_1.show()
              title.show()
  
              mntlabel[0].show()
              blank.show()
              for i in range(0, j):
                 rows[i].show()
                 mntlabel[i].show()
                 sizelabel[i].show()
                 freelabel[i].show()
              for i in range(1,j):
                  progbar[i].show()
  
              refresh_button.show()
              close_button.show()
              box3.show()
              window.show()
  
  
  def main():
      gtk.main()
  
  if __name__ == "__main__":
      freedisk()
      main()
  </code></pre>
  
  <p>Edited (24.10.17) to add a simpler example then, just show the outputs of the command "ls":</p>
  
  <pre><code>#!/usr/bin/env python
  
  import pango, pygtk, subprocess, re, string, decimal, time
  pygtk.require('2.0')
  import gtk
  
  
  class PyGlist:
  
      def getlisting(self):
          listingcmd = ['ls', '-1']
          listing = subprocess.Popen(listingcmd, stdout=subprocess.PIPE).communicate()[0]
          return listing
  
      def delete_event(self, widget, event, data=None):
          gtk.main_quit()
          return False
  
      def destroy(self, widget, data=None):
          gtk.main_quit()
  
      def __init__(self):
          # nothing here
          return None
  
      def processlisting(self):
          listingdata = self.getlisting()
          print (listingdata)
          loclabel = gtk.Label(listingdata)
          loclabel.set_line_wrap(False)
          loclabel.set_max_width_chars(80)
          loclabel.modify_font(pango.FontDescription("monospace"))
          return loclabel
  
      def refresh(self, widget, data):
          widget = self.processlisting()
          widget.queue_draw()
          return 
  
      def main(self):
          window = gtk.Window(gtk.WINDOW_TOPLEVEL)
          window.connect("delete_event", self.delete_event)
  
          box1 = gtk.VBox(False, 10)
          box1a = gtk.HBox(False, 0)
          box2 = gtk.VBox(False, 10)
          box3 = gtk.HBox(False, 0)
          frame_1 = gtk.Frame()
          window.add(box1)
  
          title = gtk.Label('Listing')
          box1.pack_start(title, False, False, 10)
          box1.pack_start(box1a, True, True, 0)
          box1a.pack_start(frame_1, True, True, 20)
          frame_1.add(box2)
  
          label = self.processlisting()
  
          box_sp1 = gtk.HBox(False, 0)
          box_sp2 = gtk.HBox(False, 0)
  
          box2.pack_start(label, False, False, 0)
  
          refresh_button = gtk.Button("Refresh")
          refresh_button.connect("clicked", self.refresh, label)
  
          close_button = gtk.Button("Close")
          close_button.connect("clicked", self.delete_event, "quit")
  
          box3.pack_end(close_button, False, False, 20)
          box3.pack_end(refresh_button, False, False, 20)
          box1.pack_start(box3, False, False, 10)
  
          box1.show()
          box1a.show()
          label.show()
          box2.show()
          box_sp1.show()
          box_sp2.show()
          frame_1.show()
          title.show()
  
          refresh_button.show()
          close_button.show()
          box3.show()
          window.show()
  
          gtk.main()
  
  
  if __name__ == "__main__":
      pyglist =PyGlist()
      pyglist.main()
  </code></pre>
  
  <p>I'm getting closer to understanding now what's happening; I'm just slow at picking up concepts.</p>