uMuppet
December 7th, 2008, 10:33 PM
I'm fairly new to Python but thought I had the basics down when I got hit with this and can't find a way around.
I'm using Glade for GUI and Python for code making a program that will call a script I wrote in Perl, pause for x seconds then call it again infinite times until told to stop. Sounds easy and I had no problem doing it until I tried to stop it and ctrl-c is the only way.
I know you're thinking 'why not just write the whole thing in Python, why call a Perl script?' The Perl script queries a Modbus device connected to the serial port and stores the result in a MySQL database. This works well and I can't seem to do it using the pyserial library in Python so it has to stay in Perl.
Here is a cutdown sample of the code
from pylab import *
import sys
import datetime
import os
import time
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import MySQLdb
################################################## #############################
# End of Imports
record = 0
view = 0
class gui:
# Not sure where to put this but I need it to be scanned all the time
global record
if (record ==1):
os.system("perl modbus3c.pl")
time.sleep(3)
def __init__(self):
global record
global view
#Set the Glade file
self.gladefile = "graph4a.glade"
self.wTree = gtk.glade.XML(self.gladefile)
#Create our dictionay and connect it
dic = { "on_record_toggled" : self.record_toggle,
"on_window1_destroy" : gtk.main_quit }
self.wTree.signal_autoconnect(dic)
def record_toggle(self, widget):
global record
if (record == 1):
print "Record is turned off"
record = 0
else:
print "Record is turned on"
record = 1
if __name__ == "__main__":
hwg = gui()
gtk.main()
I'm using Glade for GUI and Python for code making a program that will call a script I wrote in Perl, pause for x seconds then call it again infinite times until told to stop. Sounds easy and I had no problem doing it until I tried to stop it and ctrl-c is the only way.
I know you're thinking 'why not just write the whole thing in Python, why call a Perl script?' The Perl script queries a Modbus device connected to the serial port and stores the result in a MySQL database. This works well and I can't seem to do it using the pyserial library in Python so it has to stay in Perl.
Here is a cutdown sample of the code
from pylab import *
import sys
import datetime
import os
import time
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import MySQLdb
################################################## #############################
# End of Imports
record = 0
view = 0
class gui:
# Not sure where to put this but I need it to be scanned all the time
global record
if (record ==1):
os.system("perl modbus3c.pl")
time.sleep(3)
def __init__(self):
global record
global view
#Set the Glade file
self.gladefile = "graph4a.glade"
self.wTree = gtk.glade.XML(self.gladefile)
#Create our dictionay and connect it
dic = { "on_record_toggled" : self.record_toggle,
"on_window1_destroy" : gtk.main_quit }
self.wTree.signal_autoconnect(dic)
def record_toggle(self, widget):
global record
if (record == 1):
print "Record is turned off"
record = 0
else:
print "Record is turned on"
record = 1
if __name__ == "__main__":
hwg = gui()
gtk.main()