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



Hi.
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")
         vbox.pack_start(label)
         scrolledWindow.add_with_viewport(vbox)
         window.add(scrolledWindow)
         window.connect("destroy", lambda w: gtk.main_quit())
         window.show_all()
        
if __name__ == "__main__":
         example = Example()
         gtk.main()


########################
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.

Cheers,
         -Tristan



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