parogers / pgu

Automatically exported from code.google.com/p/pgu
GNU Lesser General Public License v2.1
89 stars 37 forks source link

Label size problems with changing content #40

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
>>> from pgu import gui
>>> label = gui.Label("mytext",width=300)
>>> label.style.width
55

What is the expected output? What do you see instead?
(expected 300)

What version of the product are you using? On what operating system?
pgu-0.18 on ubuntu

Please provide any additional information below.

The reason that I feel this is needed is that I want to be able to change the 
label value in a fixed size container. Label.set_text was very slow.
(attempting to display the value of a moving slider caused lag)

I created a workaround that allows you to set a number of characters that you 
would like to be able to display, and if it is set, then the label will use 
that number of 'M' characters to estimate the size of the field.

===================================
#new Label.__init__()
    def __init__(self, value="", **params):
        params.setdefault('focusable', False)
        params.setdefault('cls', 'label')
        widget.Widget.__init__(self, **params)
        self.style.check("font")
        self.value = value
        self.font = self.style.font
        params.setdefault('chars')
        self.chars=params['chars']

        if self.chars is None:
            self.style.width, self.style.height = self.font.size(self.value)
        else:
            self.style.width, self.style.height = self.font.size("M" * self.chars)

#new set_text()
    def set_text(self, txt):
        """Set the text of this label."""
        self.value = txt
        # Signal to the application that we need to resize this widget
        if self.chars is None:
            self.chsize()
        else:
            self.repaint()

#new Label.resize()
    def resize(self,width=None,height=None):
        # Calculate the size of the rendered text
        if self.chars is None:
            self.style.width, self.style.height = self.font.size(self.value)
        else:
            (self.style.width, self.style.height) = self.font.size("M" * self.chars)
        return (self.style.width, self.style.height)

==========================================================
#showing differences
>>> from pgu import gui
>>> oldlabel = gui.Label("mytext")
>>> oldlabel.style.width
55
>>> sizelabel = gui.Label("mytext", width=100)
>>> sizelabel.style.width
55
>>> newlabel = gui.Label("mytext", chars=10)
>>> newlabel.style.width
130

Original issue reported on code.google.com by Pontif...@gmail.com on 30 Jul 2013 at 10:30