PyGTK: embalagem dinâmica de etiquetas
É umbug / problema conhecido que um rótulo no GTK não será redimensionado dinamicamente quando o pai é alterado. É um daqueles pequenos detalhes realmente irritantes, e eu quero vasculhar isso, se possível.
Eu segui a abordagem em16 softwares, mas de acordo com o aviso legal você não pode redimensioná-lo menor. Então eu tentei um truque mencionado em um dos comentários (oset_size_request
chamar o sinal de retorno de chamada), mas isso resulta em algum tipo de loop infinito (tente e veja).
Alguém tem alguma outra ideia?
(Você não pode bloquear o sinal apenas pela duração da chamada, já que oprint
instruções parecem indicar, o problema começa depois que a função é deixada.)
O código está abaixo. Você pode ver o que quero dizer se você executá-lo e tentar redimensionar a janela maior e menor. (Se você quiser ver o problema original, comente a linha após "Conectar ao sinal de alocação de tamanho", execute-o e redimensione a janela maior).
O arquivo Glade ("example.glade"):
<?xml version="1.0"?>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<widget class="GtkWindow" id="window1">
<property name="visible">True</property>
<signal name="destroy" handler="on_destroy"/>
<child>
<widget class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="label" translatable="yes">In publishing and graphic design, lorem ipsum[p][1][2] is the name given to commonly used placeholder text (filler text) to demonstrate the graphic elements of a document or visual presentation, such as font, typography, and layout. The lorem ipsum text, which is typically a nonsensical list of semi-Latin words, is a hacked version of a Latin text by Cicero, with words/letters omitted and others inserted, but not proper Latin[1][2] (see below: History and discovery). The closest English translation would be "pain itself" (dolorem = pain, grief, misery, suffering; ipsum = itself).</property>
<property name="wrap">True</property>
</widget>
</child>
</widget>
</glade-interface>
O código Python:
#!/usr/bin/python
import pygtk
import gobject
import gtk.glade
def wrapped_label_hack(gtklabel, allocation):
print "In wrapped_label_hack"
gtklabel.set_size_request(allocation.width, -1)
# If you uncomment this, we get INFINITE LOOPING!
# gtklabel.set_size_request(-1, -1)
print "Leaving wrapped_label_hack"
class ExampleGTK:
def __init__(self, filename):
self.tree = gtk.glade.XML(filename, "window1", "Example")
self.id = "window1"
self.tree.signal_autoconnect(self)
# Connect to the size-allocate signal
self.get_widget("label1").connect("size-allocate", wrapped_label_hack)
def on_destroy(self, widget):
self.close()
def get_widget(self, id):
return self.tree.get_widget(id)
def close(self):
window = self.get_widget(self.id)
if window is not None:
window.destroy()
gtk.main_quit()
if __name__ == "__main__":
window = ExampleGTK("example.glade")
gtk.main()