[pygtk] Need help with spawn_async and callback
Tony Freeman
t0ny.fr33man at gmail.com
Wed Sep 7 22:07:19 WST 2011
Hello Grant,
Below is the entire bit of code I was working on at that time. Pay
attention to the spawn_async stuff. Good luck!
-- Tony
#!/usr/bin/python
####################################################
## THIS IS A GUI FRONT END TO THE adjust_grids.sh
## SCRIPT LOCATED IN /DATA/LOCAL/IFPS/
##
## t0ny.fr33man at noaa.gov (26 July 2010)
##
####################################################
####################################################
## SETUP THE ENVIRONMENT:
####################################################
import pygtk
import gtk
import gtk.glade
import gobject
import os
os.chdir("/awips/dev/localapps/adjust_grids/")
####################################################
### GLOBAL VARIABLES AND SITE CONFIGURATION:
####################################################
import siteConfig
xxxxxx = siteConfig.xxxxxx
guess = siteConfig.guess
program = siteConfig.program
### GLOBAL VARIABLES:
keep_pulsing = True
####################################################
### PULL IN THE GUI:
####################################################
wTree = gtk.glade.XML('adjust_grids.glade')
### MAIN WINDOW ITEMS (window1):
calendar = wTree.get_widget('calendar1')
gridbox = wTree.get_widget('combobox_grid')
modelbox = wTree.get_widget('combobox_model')
hourbox = wTree.get_widget('combobox_hour')
statusbar = wTree.get_widget('label_status')
### PROGRESS WINDOW ITEMS (window2):
window2 = wTree.get_widget('window2')
dialog1 = wTree.get_widget('dialog1')
progressbar = wTree.get_widget('progressbar1')
progress_headline = wTree.get_widget('label_progress_headline')
progress_info = wTree.get_widget('label_progress_info')
textview = wTree.get_widget('textview1')
textbuffer=textview.get_buffer()
####################################################
### FILL IN THE LIST BOXES:
####################################################
for i in xxxxxx:
gridbox.append_text(i)
for i in guess:
modelbox.append_text(i)
gridbox.set_active(0)
modelbox.set_active(0)
hourbox.set_active(0)
####################################################
### DEFINE FUNCTIONS:
####################################################
def write_status(comment):
statusbar.set_text(comment)
progress_info.set_text(comment)
def cstdout_callback(fd, condition, channel):
global keep_pulsing
if condition == gobject.IO_HUP:
keep_pulsing=False
elif condition == gobject.IO_IN:
text = channel.readline()
iter = textbuffer.get_end_iter()
textbuffer.insert(iter, text)
textview.scroll_to_mark(textbuffer.get_insert(),0)
return keep_pulsing
def update_progress_callback():
global keep_pulsing
if keep_pulsing:
progressbar.pulse()
else:
write_status("Done")
window2.hide()
return keep_pulsing
def run_command(command):
global keep_pulsing
keep_pulsing=True
textbuffer.set_text("")
(cpid, cstdin, cstdout, cstderr) =
gobject.spawn_async(command,flags=gobject.SPAWN_DO_NOT_REAP_CHILD,standard_output=True)
channel = os.fdopen(cstdout)
gobject.io_add_watch(cstdout, gobject.IO_HUP|gobject.IO_IN,
cstdout_callback, channel)
gobject.timeout_add(150, update_progress_callback)
window2.show()
def get_active_text(combobox):
model = combobox.get_model()
active = combobox.get_active()
if active < 0:
return None
return model[active][0]
def build_timestamp():
year, month, day = calendar.get_date()
hour = get_active_text(hourbox)
month = "%02d" % month
day = "%02d" % day
timestamp = str(year) + str(month) + str(day) + "_" + str(hour)
return timestamp
def on_dialog1_destroy(*args):
write_status("Backgrounded script")
dialog1.hide()
def on_closebutton1_clicked(*args):
write_status("Backgrounded script")
dialog1.hide()
def on_window1_destroy(*args):
write_status("Good-bye!")
gtk.main_quit()
def on_window2_destroy(*args):
window2.hide()
dialog1.show()
write_status("Continuing in background")
return True
def on_window2_delete_event(*args):
window2.hide()
dialog1.show()
write_status("Continuing in background")
return True
def on_button_quit_clicked(*args):
write_status("See ya next time")
gtk.main_quit()
def on_button_apply_clicked(*args):
scripttime = build_timestamp()
scriptmodel = get_active_text(modelbox)
scriptgrid = get_active_text(gridbox)
command = [program, scriptgrid, scriptmodel, scripttime]
write_status("Here we go ...")
run_command(command)
####################################################
### CONNECT SIGNALS TO OUR HANDLERS:
####################################################
wTree.signal_autoconnect(locals())
####################################################
### START UP THE PROGRAM:
####################################################
gtk.main()
On Wed, Aug 3, 2011 at 7:44 AM, Grant McWilliams
<grantmasterflash at gmail.com> wrote:
> Tony Freeman <t0ny.fr33man <at> gmail.com> writes:
>
> Can you post the whole code anyway for those of us trying to figure out the same
> thing.
>
> _______________________________________________
> pygtk mailing list pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/
>
More information about the pygtk
mailing list