I’m testing out a simple sample PyGTK application based on the code in another SO question: How to set default button in PyGTK?.
However, I’m trying to figure out how to access the value that the run() function returns. I know that it is passed into button.connect() and the other arguments are passed to run() when it is called, but since the function is called latter, I don’t know how to access the return value.
def foo():
def run(button, window, entry):
try: snooze_time = int(entry.get_text())
except ValueError: snooze_time = 5
snooze_confirm_message = 'Snoozing for ' + str(snooze_time) + ' minutes'
dialog = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, snooze_confirm_message)
dialog.run()
dialog.destroy()
return snooze_time
snooze_time = None #Defaults to None
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
vbox = gtk.VBox(spacing = 40)
snooze_time_entry = gtk.Entry()
vbox.pack_start(snooze_time_entry)
button = gtk.Button(stock = gtk.STOCK_SAVE)
#Here the function is being passed to the button, though it is not called until later.
button.connect("clicked", run, window, snooze_time_entry)
button.set_flags(gtk.CAN_DEFAULT)
window.set_default(button)
vbox.pack_start(button)
window.add(vbox)
window.show_all()
gtk.main()
print("the result is " + str(snooze_time))
If there is a workaround that is more idiomatic with PyGTK, it’d be useful here, though I’d also be interested in knowing a more general, Python approach to this.
You should use class. Something like this (this is not complete code):