I am writing a program that performs some image processing, but needs the user to input some markers on the input images, before the main processing occurs. This is being done with Python/Numpy/Scipy, and PyGTK.
As a way to make this possible, I tried a small script which opens a GUI, and tries to take some value back from its mainloop, but it doesn't work:
import gtk
def getGuiInput(var):
window = gtk.Window()
button = gtk.Button('Will <var> be set to 10?')
window.add(button)
def submit(widget, event, var):
var = 10
gtk.main_quit()
button.connect('button-press-event', submit, var)
window.set_position(gtk.WIN_POS_CENTER)
window.show_all()
gtk.main()
var = 0
print "before, var is %d" % var
getGuiInput(var)
print "after, var is %d" % var
I'm pretty familiar with creating and laying out widgets, connecting events and using call-backs and event handlers.
What I would like to know is: how to put everything inside a function, so that in my main script I call it to open an "input window GUI" which, when done, returns a value to the caller? How can I get some value from inside gtk.main()?
EDIT: Following the suggestion given in one answer, I swapped the Window widget for a Dialog one, but still don't get what I want:
import gtk
def run(var):
dialog = gtk.Dialog("Entre em action")
dialog.response(1)
button = gtk.Button('Will <var> be set to 10?')
dialog.add_action_widget(button, 1)
button.show()
def submit(widget, event, var):
print var
var = 10
button.connect('button-press-event', submit, var)
dialog.run()
var = 0
print "before, var is %d" % var
result = run(var)
print result
print "after, var is %d" % var
Thanks for reading, and correct me if there is any conceptual flaw, please.