I tried to make a program which on a button click,starts reading from a file
and then shows contents in different text fields.However i am not able to do
that properly.I have attached two attempts.How can i do the above task.
#!/usr/bin/env python


#Program to show text entries

import pygtk
pygtk.require('2.0')
import gtk
import serial
import re


class Entry:
	#ser=serial.Serial()
	#ser.port="/dev/ttyUSB0"
	#ser.baudrate=9600
	#ser.open()
	
	def callback(self,widget,entry):
		g=open('/home/nipun/1.txt')
	
		vars = re.compile('^(\d+\.?\d*)a(\d+\.?\d*)b(\d+\.?\d*)c(\d+\.?\d*)d(.*)')
		i=0
		while(i<3):
			s=g.readline()
			print s
		#g.write(s)
		#g.write("\n")
		#print "\n"
			m = re.search(vars, s) # Run the regexp on the packet
			var2 = m.group(2)
			var1 = m.group(1) # take out the values
			var3 = m.group(3)
			var4 = m.group(4)
			print var1,"\n"

			al=str(12)
			entry.set_text(al)
			entry1.set_text(var2)
			entry2.set_text(var3)
			entry3.set_text(var4)
			i=i+1
		
		
		
	def enter_callback(self,widget,entry):
		print"Entry contents"
	def entry_toggle_editable(self,checkbutton,entry):
		entry.set_editable(checkbutton.get_active())
	def entry_toggle_visibility(self,checkbutton,entry):
		entry.set_visibility(checkbutton.get_active())
	def __init__(self):
		window=gtk.Window(gtk.WINDOW_TOPLEVEL)
		window.set_size_request(200,100)
		window.set_title("Entry")
		window.connect("delete_event",lambda w,e:gtk.main_quit())
		
		vbox=gtk.VBox(False,0)
		window.add(vbox)
		vbox.show()
		
		label1=gtk.Label("Variable a")
		label2=gtk.Label("Variable b")
		label3=gtk.Label("Variable c")
		label4=gtk.Label("Variable d")

		entry=gtk.Entry()
		entry1=gtk.Entry()
		entry2=gtk.Entry()
		entry3=gtk.Entry()
		entry.set_max_length(50)
		entry.connect("activate",self.enter_callback,entry)
		
		vbox.pack_start(label1,True,True,2)
		vbox.pack_start(entry,True,True,2)
		label1.show()
		entry.show()
		
		vbox.pack_start(label2,True,True,2)
		vbox.pack_start(entry1,True,True,2)
		label2.show()
		entry1.show()
		vbox.pack_start(label3,True,True,2)
		vbox.pack_start(entry2,True,True,2)
		label3.show()
		entry2.show()
		
		vbox.pack_start(label4,True,True,2)
		vbox.pack_start(entry3,True,True,2)
		label4.show()
		entry3.show()
		
		hbox=gtk.HBox(False,0)
		vbox.add(hbox)
		hbox.show()
		
		check = gtk.CheckButton("Editable")
		hbox.pack_start(check, True, True, 0)
		check.connect("toggled", self.entry_toggle_editable, entry)
		check.set_active(True)
		check.show()

		check = gtk.CheckButton("Visible")
		hbox.pack_start(check, True, True, 0)
		check.connect("toggled", self.entry_toggle_visibility, entry)
		check.set_active(True)
		check.show()

		button = gtk.Button("Start receiving info")
		button.connect("clicked",self.callback,3)
		vbox.pack_start(button, True, True, 0)
		button.set_flags(gtk.CAN_DEFAULT)
		button.grab_default()
		button.show()
		window.show()


def main():
	gtk.main()
	return 0

if __name__ == "__main__":
	Entry()
	main()
		
#!/usr/bin/env python


#Program to show text entries

import pygtk
pygtk.require('2.0')
import gtk
import serial
import re


class Entry:
	#ser=serial.Serial()
	#ser.port="/dev/ttyUSB0"
	#ser.baudrate=9600
	#ser.open()
	

	def enter_callback(self,widget,entry):
		g=open('/home/nipun/1.txt')
	
		vars = re.compile('^(\d+\.?\d*)a(\d+\.?\d*)b(\d+\.?\d*)c(\d+\.?\d*)d(.*)')
		i=0
		while(i<3):
			s=g.readline()
			print s
		#g.write(s)
		#g.write("\n")
		#print "\n"
			m = re.search(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)
			print var1,"\n"

		
			entry.set_text(var1)
			i=i+1
		#print"Entry contents%s\n"%entry_text
	def entry_toggle_editable(self,checkbutton,entry):
		entry.set_editable(checkbutton.get_active())
	def entry_toggle_visibility(self,checkbutton,entry):
		entry.set_visibility(checkbutton.get_active())
	def __init__(self):
		window=gtk.Window(gtk.WINDOW_TOPLEVEL)
		window.set_size_request(200,100)
		window.set_title("Entry")
		window.connect("delete_event",lambda w,e:gtk.main_quit())
		
		vbox=gtk.VBox(False,0)
		window.add(vbox)
		vbox.show()
		
		label1=gtk.Label("Information")

		entry=gtk.Entry()
		entry.set_max_length(50)
		entry.connect("activate",self.enter_callback,entry)
		
		vbox.pack_start(label1,True,True,2)
		vbox.pack_start(entry,True,True,2)
		label1.show()
		entry.show()
		
		hbox=gtk.HBox(False,0)
		vbox.add(hbox)
		hbox.show()
		
		check = gtk.CheckButton("Editable")
		hbox.pack_start(check, True, True, 0)
		check.connect("toggled", self.entry_toggle_editable, entry)
		check.set_active(True)
		check.show()

		check = gtk.CheckButton("Visible")
		hbox.pack_start(check, True, True, 0)
		check.connect("toggled", self.entry_toggle_visibility, entry)
		check.set_active(True)
		check.show()

		button = gtk.Button(stock=gtk.STOCK_CLOSE)
		button.connect("clicked", lambda w: gtk.main_quit())
		vbox.pack_start(button, True, True, 0)
		button.set_flags(gtk.CAN_DEFAULT)
		button.grab_default()
		button.show()
		window.show()


def main():
	gtk.main()
	return 0

if __name__ == "__main__":
	Entry()
	main()
		
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to