Search code examples
gladepygobject

Set focus to child window in PyGtk


I am writing a simple application in python using PyGObject and glade. The application has a main window and a functional window (Generate logs, also a Window object) which opens up on clicking a menu item.

The Generate logs window is supposed to:

  1. Show options to generate log for a particular date
  2. Be minimizable and should close automatically when the task is complete (OR)
  3. Be able to be closed manually if the user wishes so

The problem is, once I show up the Generate logs window, I am directly able to select the main window as well. Then, I can go to the menu and bring up as many Generate logs windows as I want.

I have tried several options (Is Focus, setting up main window as Transient parent etc) but nothing worked. How can I fix this?


Solution

  • First you say PyGTK, then you say PyGObject, this are 2 different things. I'm going to answer for PyGTK (my sources are from GTK+ 2 docs) since it's in the title and maybe people looking for that will end up here. But never fear, because for this question, the answer is practically (I think exactly) the same for both.

    What I understand is that you want you "Generate log" window to be modal. That means other windows can't be used while your modal window is up, just like a Dialog window. Also you should set the main window to be the parent of your modal window, since this helps the OS Window Manager i.e. keep the dialog on top of the main window.

    Yo can do both of this things directly from Glade (if you've created both windows in the same project, not always the case) selecting the Modal atribute to True and the Transient for Window attribute to your main window, in the General Properties section of your Generate log window.

    Also you can do it programmatically using the set_modal() and the set_transient_for(parent_window) method of your child window.
    Let's say your parent window is called main_window and the child window is generate_log_window, then you can do it like this:

    generate_log_window.set_modal(gtk.TRUE)  
    generate_log_window.set_transient_for(main_window)  
    

    If you want it to show center top of your main window, do this

    generate_log_window.set_position(gtk.WIN_POS_CENTER_ON_PARENT)  
    

    To your second point, the ability to minimize can be set from Glade.

    Sources:
    GTK+ 2 GtkWindow reference set_modal
    GTK+ 2 GtkWindow reference set_transient_for
    PyGTK FAQ: How do I get my windows to show up where I want
    PyGTK FAQ: How do I make a dialog block the whole application, so the user is forced to answer it?