3

I was searching for an solution to hide gtk dialog immediately after get the response. But now I am wondering why it disappears, but only if I don't click into the entry field:

import gtk, time

def get_info():
    entry = gtk.Entry()
    entry.set_text("Hello")
    dialog = gtk.Dialog(title = "Title",
                       parent = None,
                       flags = gtk.DIALOG_MODAL  | gtk.DIALOG_DESTROY_WITH_PARENT,
                       buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                        gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
    dialog.vbox.pack_start(entry)
    dialog.show_all()    
    response = dialog.run()
    if response == gtk.RESPONSE_ACCEPT:
        info = entry.get_text().strip()
        dialog.destroy()
        return info
    else:
        exit()

info = get_info()
time.sleep(4)
print info

If I just press "OK" the dialog disappears and after 4 seconds the info is printed. If I click into the entry field and then press "OK" the dialog doesn't disappear until the program ends. Why is this so?

edit:

Same problem if I make this with a main loop:

#!/usr/bin/env python
# -*- coding: utf8 -*-


import gtk, time

class EntryTest:

    def get_info(self):
        entry = gtk.Entry()
        entry.set_text("Hello")
        dialog = gtk.Dialog(title = "Title",
                           parent = None,
                           flags = gtk.DIALOG_MODAL  | gtk.DIALOG_DESTROY_WITH_PARENT,
                           buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                            gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
        dialog.vbox.pack_start(entry)
        dialog.show_all()    
        response = dialog.run()
        if response == gtk.RESPONSE_ACCEPT:
            info = entry.get_text().strip()
            dialog.destroy()
            return info
        else:
            exit()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    base = EntryTest()
    info = base.get_info()
    time.sleep(4)
    print info
jophab
  • 5,356
  • 14
  • 41
  • 60
oxidworks
  • 1,563
  • 1
  • 14
  • 37
  • Interesting thing: if you click in the box, but highlight the text just how it was, the dialog *does* disappear right away ... I'm dying of curiosity. – zondo Jun 26 '16 at 11:10

1 Answers1

2

You don't have a main loop running. This usually means Gtk+ doesn't do anything -- windows wouldn't be shown in the first place -- but dialog.run() is special in that it happens to run its own short-lived mainloop so it looks like things are working. After dialog.run() exits you really don't have a mainloop running so Gtk+ cannot do anything.

If you do this in a real app where gtk.main() is running, it should just work.

Example main loop use (EntryTest can stay as it is, but you will need an additional import glib):

def quit ():
    print "now quitting"
    gtk.main_quit()
    return False

if __name__ == "__main__":
    base = EntryTest()
    print base.get_info()
    glib.timeout_add_seconds (3, quit)
    gtk.main()

It's worth noting that the main loop is not running when the dialog is visible but only afterwards (because I was lazy). You could start the get_info() code within the main loop as well with e.g. glib.idle_add() but the point is the same: GTK+ usually needs the main loop to be running.

Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54
  • You are right, but this was not the question. This also happens when I use main loop. – oxidworks Jun 23 '16 at 14:18
  • Can you show the code where this happens with a main loop? – Jussi Kukkonen Jun 24 '16 at 15:45
  • You are not actually starting the main loop in your example. If you do, it starts working. I can add a minimal example to the answer. – Jussi Kukkonen Jun 26 '16 at 16:59
  • Today I tried again to understand but I do not. Why I need a main loop, if the Dialog has its own loop, I need not more. Why do I have to use glib.timeout_add_seconds (4, quit) instead of sleep. – oxidworks Feb 16 '17 at 13:08
  • I read a little bit about it, thx or the answer. Helped me to undertand. But the main question is: Why does the dialog disappears directly only when I do not click inside in my first example. – oxidworks Feb 16 '17 at 13:41