PyGTK:动态标签包装

det*_*tly 9 pygtk

这是一个已知的错误/问题,当父级更改时,GTK中的标签不会动态调整大小.这是一个非常烦人的小细节之一,如果可能的话,我想破解它.

我按照16软件的方法进行了操作,但根据免责声明,您无法将其调整得更小.所以我尝试了其中一个注释(set_size_request信号回调中的调用)中提到的技巧,但这导致了某种无限循环(尝试并看到).

有没有人有任何其他想法?

(您不能仅在调用期间阻止信号,因为正如print语句似乎表明的那样,问题在函数离开后开始.)

代码如下.你可以看到我的意思,如果你运行它并尝试调整窗口大小然后更小.(如果要查看原始问题,请在"连接到大小分配信号"之后注释掉该行,运行它,然后将窗口调整得更大.)

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>
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

Kai*_*Kai 3

VMware 的 libview 有一个名为WrapLabel 的小部件,它应该可以完成您想要的操作,但它是用 C++ 编写的。Meld 存储库中提供了 Python 翻译(与busybox.py分开)。