I need from this code that var1,var2,var3,var4 should be continuosly read from file(or serial port) and there value be updated continuosly.Also i wish to use these 4 variables for plotting in pygame/matplotlib/VPython .How can i do that.Moreover for every read cycle there has to be a write cycle on to a file(or serial port) and the user must be able to set this value via an entry.have not been able to figure out all this.Moreover with my code i am able to presently display updated information on a button click(where as i wanna use button just to start things of)
#!/usr/bin/env python
#This program will attempt to read a line from a file using a GUI
import pygtk
pygtk.require('2.0')
import gtk
import re
import time
class Nipun:
f=open('/home/nipun/1.txt')
vars = re.compile('^(\d+\.?\d*)a(\d+\.?\d*)b(\d+\.?\d*)c(\d+\.?\d*)d(.*)')
def __init__(self):
self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("File read GUI Label")
self.window.connect("delete_event",self.delete_event)
self.window.set_border_width(10)
self.box2=gtk.VBox(False,0)
self.window.add(self.box2)
self.button=gtk.Button("Read")
self.button.connect("clicked",self.callback)
self.box2.pack_start(self.button,True,True,0)
self.button.show()
self.label1=gtk.Label()
self.label2=gtk.Label()
self.label3=gtk.Label()
self.label4=gtk.Label()
self.label5=gtk.Label("A")
self.label6=gtk.Label("B")
self.label7=gtk.Label("C")
self.label8=gtk.Label("D")
self.box2.pack_start(self.label5,True,True,0)
self.box2.pack_start(self.label1,True,True,0)
self.box2.pack_start(self.label6,True,True,0)
self.box2.pack_start(self.label2,True,True,0)
self.box2.pack_start(self.label7,True,True,0)
self.box2.pack_start(self.label3,True,True,0)
self.box2.pack_start(self.label8,True,True,0)
self.box2.pack_start(self.label4,True,True,0)
self.label1.show()
self.label2.show()
self.label3.show()
self.label4.show()
self.label5.show()
self.label6.show()
self.label7.show()
self.label8.show()
self.box2.show()
self.window.show()
def delete_event(self,widget,event,data=None):
print "Delete event has occcured"
gtk.main_quit()
return False
def callback(self,widget,data=None):
print "alpha"
s=hello.f.readline()
m = re.search(hello.vars, s) # Run the regexp on the packet
var1 = m.group(1) # take out the values
var2 = m.group(2)
var3 = m.group(3)
var4 = m.group(4)
hello.label1.set_text(var1)
hello.label2.set_text(var2)
hello.label3.set_text(var3)
hello.label4.set_text(var4)
def main(self):
gtk.main()
if __name__=="__main__":
hello=Nipun()
hello.main()
Contents of 1.txt are:
100a45.787b89c70d
7a45b98c7d
5a3b5c4d
6a5b5c8d
5a9b4c5d
6a5b5c4d