[PyGTK] Singleton Window

alex goretoy aleksandr.goretoy at gmail.com
Fri Mar 6 05:38:56 CET 2009


Hello All,

I am trying to create a singleton window in pygtk and for the life of me
can't seem to figure out a better way to go about doing this. The way I'm
doing it now is to recreate the window and show it. Example code is below. I
would much appreciate any assistance in this matter. I've googled to no
avail. I'm hiding the child window because when I execute main_quit on the
child it kills parent (which makes sense).

My singleton hack works, but its cluncky, I am wondering if theres some
other way(working shmingleton?).

My question is this. How to create a singleton child window without hiding
old childs(seeing as this creates many hidden childs after a while) or how
to hide it and when showing it again also have it display all contents in
the child window?

Thank you.

#!/usr/bin/env python
import os, sys

import pygtk
pygtk.require('2.0')
try:
  import gtk
except:
  print >> sys.stderr, "You need to install the python gtk bindings"
  sys.exit(1)



class Singleton_Test(object):
  def __init__(self):
    self.root_window = gtk.Window( type=gtk.WINDOW_TOPLEVEL )
    self.root_window.set_title("Singleton Test")
    self.root_window.connect("delete_event", lambda w,e: gtk.main_quit())

    self.vbox = gtk.VBox(False,0)
    self.root_window.add(self.vbox)
    self.vbox.show()

    self.singleton_button = gtk.Button("SINGLETON")
    self.singleton_button.connect("clicked",self.singleton_cb)
    self.vbox.pack_start(self.singleton_button,False,False,0)

    self.singleton_button.show()

    self.shmingleton_button = gtk.Button("SHMINGLETON")
    self.shmingleton_button.connect("clicked",self.shmingleton_cb)
    self.vbox.pack_start(self.shmingleton_button,False,False,0)

    self.shmingleton_button.show()

    self.create_singleton_child_window()

    self.root_window.show()

  singleton_window_count=0
  def singleton_cb(self,w):
    if self.singleton_window_count>0:
      self.create_singleton_child_window()

    self.singleton_child_window.show()
    print "singleton child window count is ",self.singleton_window_count
    self.singleton_window_count+=1

  def shmingleton_cb(self,w):
    self.singleton_child_window.show_all() #doesn't work as expected,
neither does show


  def create_singleton_child_window(self):
    self.singleton_child_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.singleton_child_window.set_title("SINGLETON child")

    #only hiding since gtk.main_quit kills parent, any better way?
    self.singleton_child_window.connect("destroy", lambda w:
self.singleton_child_window.hide())
    self.singleton_child_window.connect("delete_event", lambda w,e:
self.singleton_child_window.hide())

    self.vbox = gtk.VBox(False, 0)
    self.singleton_child_window.add(self.vbox)
    self.vbox.show()
    self.label = gtk.Label("SINGLETON")
    self.vbox.pack_start(self.label,False,False,0)
    self.label.show()

  def main(self):
    gtk.main()

if __name__ == "__main__":
  singleton = Singleton_Test()
  singleton.main()

-Alex Goretoy
http://www.goretoy.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090305/3fdae8dc/attachment.html>


More information about the Python-list mailing list