Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.builder2 = Gtk.Builder()

        self.window = self.builder.get_object('window')
        self.window.connect("destroy", self.destroy)
        self.window.show_all() = self.builder.get_object('button1')"clicked", self.search_clicked)

    def destroy(self, window):

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

def main():
    app = SampleApp()

if __name__ == "__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
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()
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 :) 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: – 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

Your Answer


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.