4

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.

1 Answer 1

2

From within a function, you can create your window as a gtk.Dialog and do dialog.run() which will run a second GTK main loop restricted to that one dialog. (This means that you won't be able to interact with other windows in your program while the secondary loop is running.)

If you add a button to the dialog with a response ID, or call dialog.response() with a value, then that value will be the return value of dialog.run().

dialog.add_action_widget(button, 10)
# ...
print 'var is', var
var = dialog.run()
print 'var is now', var  # should be 10 if you clicked the button
Sign up to request clarification or add additional context in comments.

4 Comments

Actually, my main program runs from command line. I think I wouldn't call a dialog if I don't have a main window yet. That's why I think I should, from the CLI control flow, call a function that opens a GUI program, then takes the result upon gtk.main_quit() and gives back control to the CLI script which called it.
There's nothing wrong with calling a dialog if you don't have a main window, but what you suggest there is equally possible.
Ok, but I still don't know how to do it. The code I posted simply doesn't work.
Yeah. This isn't quite solved either. In ruby-gtk I solved this by simply storing the result in a toplevel instance variable which I can query from the parent window. Not an elegant solution but it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.