Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am creating a simple GTK3 application. The main toplevel window has a search button which will display another toplevel gtk3 window. When I click the close button (near the titlebar) on the second gtk3 window it closes as expected. However, when I click on the search button again, the second toplevel window is not redrawn with all its widgets..instead it appears empty.

If on the other hand, I click the close button (gtk button seen at the bottom) it closes properly. And when reopned all the widgets can be seen.

Info: I created the UI using Glade.

The code is displayed below. However to get the UI files, you need to download the package. You can download them here

#! /usr/bin/env python

from gi.repository import Gtk
import os,sys

UI_FILE = "main.ui"
SECOND_UI_FILE = "second.ui"

class SampleApp:
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file(UI_FILE)
        self.builder.connect_signals(self)

        self.builder2 = Gtk.Builder()
        self.builder2.add_from_file(SECOND_UI_FILE)
        self.builder2.connect_signals(self)

        self.window = self.builder.get_object('window')
        self.window.set_position(Gtk.WindowPosition.CENTER)
        self.window.connect("destroy", self.destroy)
        self.window.show_all()

        self.search = self.builder.get_object('button1')
        self.search.connect("clicked", self.search_clicked)

    def destroy(self, window):
        Gtk.main_quit()

    def search_clicked(self, window):
        self.secondwindow = self.builder2.get_object('window1')
        self.secondwindow.show_all()

def main():
    app = SampleApp()
    Gtk.main()

if __name__ == "__main__":
    main()

Main Window enter image description here

Second Window - Toplevel enter image description here

Second Window - Blank enter image description here

Why does this happen and how do I solve this?

share|improve this question
    
if you don't get a good answer, you might want to create a minimal example that recreates this problem and post the code. –  xubuntix May 16 '12 at 11:08
    
@xubuntix thnx for the info. I added the sample code and also the files required if they want to run it. –  nik90 May 16 '12 at 11:18
add comment

1 Answer

up vote 2 down vote accepted

Ok, what your code does is, it lets the second window get destroyed. After that its gone.

You can either keep the window from being destroyed (and hide it instead) by adding these two lines at the end of your init method:

secondwindow = self.builder2.get_object('window1')
secondwindow.connect('delete-event', lambda w, e: w.hide() or True)

which replaces the delete event (documentation here).

Or you can recreate the window everytime you open it, by placing those lines at the beginning of your search_clicked method:

self.builder2 = Gtk.Builder()
self.builder2.add_from_file(SECOND_UI_FILE)
self.builder2.connect_signals(self)
share|improve this answer
    
Thanks a lot mate...this is one of the major stoppers for my multiple window gtk3 application...perfect answer. –  nik90 May 16 '12 at 13:54
    
glad I could help. Seems like a great app you're writing... –  xubuntix May 16 '12 at 13:58
    
thnx :)..it is a application to show the train schedule for the netherlands trains.. –  nik90 May 16 '12 at 14:01
    
in case you want to extend it to german trains, there is a non-official api here: code.google.com/p/openbahn-api/wiki/APIDocumentation –  xubuntix May 16 '12 at 14:22
    
ooh that's nice..I am bookmarking it for now...will look at it when I am done with netherlands trains.. –  nik90 May 16 '12 at 17:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.