Re: How to get and set the width/height of a label?

It does work.
What the following code does is dynamiclly changing the size of labels(as placeholders) while the gtk.WIndow is resized manually.
Here is the code in Python:

import pygtk
import gtk

class Example:
    def __init__(self):
        window = gtk.Window()
        window.connect("destroy", lambda w: gtk.main_quit())

        label = gtk.Label("place")
        label2 = gtk.Label("-holder")
        self.label, self.label2 = label, label2

        button = gtk.Button("button")
        self.button = button

        vbox = gtk.VBox()

        scrolledWindow = gtk.ScrolledWindow()
        self.scrolledWindow = scrolledWindow
        scrolledWindow.connect("size-allocate", self.changeSize)
        scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        scrolledWindow.set_size_request(300, 300)

        hadjustment = scrolledWindow.get_hadjustment()
        vadjustment = scrolledWindow.get_vadjustment()
        self.vadjustment, self.hadjustment = vadjustment, hadjustment


    #    label.set_size_request(-1, -1)    #should be commentted
        scrolledWindow.set_size_request(-1, -1)

    def changeSize(self, allocation ,widget):
        size = self.vadjustment.page_size
        size =int((size + 1)/2)
        self.label.set_size_request(-1, size)
        self.label2.set_size_request(-1, size)
        print "self.hadj", self.hadjustment.lower, self.hadjustment.upper
        print "self.vadjustment", self.vadjustment.page_size
        print "self.label", self.label.allocation, self.label2.allocation
        print "button", self.button.allocation

if __name__ == "__main__":
    example = Example()

On Sun, Aug 9, 2009 at 10:52 AM, cbx <bnucbx gmail com> wrote:
I want a label to act as a placeholder,the size of which can be specified
Here is the picture:
A label,along with some other gtk.Widgets,will be put in a gtk.ScrolledWindow,which then be added into a gtk.Window.
When the gtk.Window first shows,the size of label and scrolledWindow can be specified to certain value.But this is not the minimum one,that is,scrolledWindow won't prevent the gtk.Window from being resized to a smaller value.

Something like(In Python):
import gtk

class Example:
     def __init__(self):
         window = gtk.Window()
         scrolledWindow = gtk.ScrolledWindow()
         vbox = gtk.VBox()
         label = gtk.Label("example")
         window.connect("destroy", lambda w: gtk.main_quit())
if __name__ == "__main__":
         example = Example()

I would try the hack to see if it works for this.

On Sat, Aug 8, 2009 at 10:37 PM, Tristan Van Berkom <tvb gnome org> wrote:
On Fri, Aug 7, 2009 at 9:54 PM, cbx<bnucbx gmail com> wrote:
> Thank you,for your kind and your example!
> In Python,gtk.Widget.get_allocation() and gtk.Windget.allocation(as an
> attribute) return the same gtk.gdk.Rectange,which indeed is the actual size.
> So i guess get the size is a solved problem now.
> What i need about setting the size of the widget is not about the minimum
> one.It more like setting the 'allocation' size and not to prevent the widget
> from resizing to a smaller size by the user manually.
> Maybe it's not clear,but i don't know how to explain myself better.

What exactly do you need the label to do when it resizes ?

Currently in GTK+ the size of text is what defines the minimum
size of your interface, labels float within the space they were
allocated, using the alignment and padding rules.

I have seen hacks along the lines of:

  gtk_widget_set_size_request (widget, default_width, default_height);
  gtk_widget_show_all (toplevel);
  gtk_widget_set_size_request (widget, -1, -1);

i.e. a way to trick the hierarchy into realizing the specified widget
at a certain size, and then remove the constraint afterwords.
(Im not sure how well this works, it may break for instance if
gtk_container_check_resize() is called in the wrong place).

GtkLabels that dynamically wordwrap or ellipsize also dont come
for free (someone please correct me if Im wrong about that, I'd
like to know), i.e. tricks need to be played to synchronize the
label's size request to a size relative to the label's parent's allocation.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]