Re: How to get and set the width/height of a label?
- From: cbx <bnucbx gmail com>
- To: Tristan Van Berkom <tvb gnome org>
- Cc: gtk-list gnome org
- Subject: Re: How to get and set the width/height of a label?
- Date: Mon, 10 Aug 2009 11:14:18 +0800
Hi!Still have problem.
If you change the line
scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
to
scrolledWindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
The program will act differently:the later won't do as the former.
The later one is prefered if I want the scrolledbar never to present.But the later one has resizing problem.
First,the output when running the program differs:
the former:
self.vadjustment 298.0
self.label gtk.gdk.Rectangle(0, 0, 298, 95) gtk.gdk.Rectangle(0, 202, 298, 96)
button gtk.gdk.Rectangle(0, 95, 298, 107)
self.vadjustment 298.0
self.label gtk.gdk.Rectangle(0, 0, 280, 149) gtk.gdk.Rectangle(0, 178, 280, 149)
button gtk.gdk.Rectangle(0, 149, 280, 29)
self.vadjustment 298.0
self.label gtk.gdk.Rectangle(0, 0, 280, 149) gtk.gdk.Rectangle(0, 178, 280, 149)
button gtk.gdk.Rectangle(0, 149, 280, 29)
the later:
self.vadjustment 298.0
self.label gtk.gdk.Rectangle(0, 0, 298, 95) gtk.gdk.Rectangle(0, 202, 298, 96)
button gtk.gdk.Rectangle(0, 95, 298, 107)
self.vadjustment 298.0
self.label gtk.gdk.Rectangle(0, 0, 298, 149) gtk.gdk.Rectangle(0, 178, 298, 149)
button gtk.gdk.Rectangle(0, 149, 298, 29)
self.vadjustment 327.0 #you can see the different from the previous one
self.label gtk.gdk.Rectangle(0, 0, 298, 149) gtk.gdk.Rectangle(0, 178, 298, 149)
button gtk.gdk.Rectangle(0, 149, 298, 29)
Second,the later would prevent you to resize it to a smaller one than its initial size.It seems that it ignore the line:scrolledWindow.set_size_request(-1, -1)
Code:
import pygtk
pygtk.require('2.0')
import gtk
class Example:
def __init__(self):
window = gtk.Window()
window.connect("destroy", lambda w: gtk.main_quit())
label = gtk.Label("example")
label2 = gtk.Label("here")
self.label, self.label2 = label, label2
button = gtk.Button("button")
self.button = button
vbox = gtk.VBox()
vbox.pack_start(label)
vbox.pack_start(button)
vbox.pack_start(label2)
scrolledWindow = gtk.ScrolledWindow()
self.scrolledWindow = scrolledWindow
scrolledWindow.connect("size-allocate", self.changeSize)
#scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scrolledWindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
scrolledWindow.set_size_request(300, 300)
scrolledWindow.add_with_viewport(vbox)
hadjustment = scrolledWindow.get_hadjustment()
vadjustment = scrolledWindow.get_vadjustment()
self.vadjustment, self.hadjustment = vadjustment, hadjustment
window.add(scrolledWindow)
window.show_all()
# 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.vadjustment", self.vadjustment.page_size
print "self.label", self.label.allocation, self.label2.allocation
print "button", self.button.allocation
print
if __name__ == "__main__":
example = Example()
gtk.main()
On Sun, Aug 9, 2009 at 6:38 PM, cbx
<bnucbx gmail com> wrote:
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
pygtk.require('2.0')
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()
vbox.pack_start(label)
vbox.pack_start(button)
vbox.pack_start(label2)
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)
scrolledWindow.add_with_viewport(vbox)
hadjustment = scrolledWindow.get_hadjustment()
vadjustment = scrolledWindow.get_vadjustment()
self.vadjustment, self.hadjustment = vadjustment, hadjustment
window.add(scrolledWindow)
window.show_all()
# 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()
gtk.main()
On Sun, Aug 9, 2009 at 10:52 AM, cbx
<bnucbx gmail com> wrote:
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]