Strumenti Utente

Strumenti Sito


pygtk:resizing

Questa è una vecchia versione del documento!


resize tweaks

First: the method gtk.Window.resize() doesnt't work with a single argument (as instead does gtk.Widget.set_size_request()). OK, this is documented, though ugly.

Now: if a gtk.Window w has size (100,100) and we run the following code:

w.resize(200,200)
size = w.get_size()
w.resize(*size)

, then the window won't resize to 200,200, but to 100,100. In fact “resize” doesn't really resize, it just queues a resize process, while if we use get_size we get the current size, not the queued one. This means if several functions try to resize (and it makes sense, if each one takes care of a single dimension), we have race conditions.

I solved that (both problems) with the following functions:

def unset_real_size(window, event):
    """Fields 'real width' and 'real height' become obsolete (or at least
    useless) when a resize actually takes place.
    """

    window.set_data('real width', None)
    window.set_data('real height', None)
    
    handler = window.get_data('configure handler')
    if handler:
        # The callback is now useless; remove it.
        window.disconnect(handler)
        window.set_data('configure handler', None)
    
def ensure_configure_handler(window):
    """Ensure that unset_real_size is called.
    """
    if not window.get_data('configure handler'):
        new_handler = window.connect('configure-event', unset_real_size)
        window.set_data('configure handler', new_handler)

def set_width(window, width):
    ensure_configure_handler(window)

    window.set_data('real width', width)
    height = window.get_data('real height')
    if not height:
        height = window.get_size()[1]
      
    window.resize(width, height)
  
def set_height(window, height):
    ensure_configure_handler(window)

    window.set_data('real height', height)
    width = window.get_data('real width')
    if not width:
        width = window.get_size()[0]
      
    window.resize(width, height)
pygtk/resizing.1231890128.txt.gz · Ultima modifica: 2009/01/14 00:42 da pietro