1

わからない問題で困っています。メインプログラムはから実行されます

if __name__ == "__main__":
    HelloWorld()
    gtk.main()

クラス内にHelloWorldは2つのシグナルがあります:

self.button.connect("file-set", self.load_image)
self.window.connect("check-resize", self.resize_image)

そしてここにそれらがあります:

def load_image(self, widget):
    self.image_loc = self.button.get_filename()
    pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_loc)

    self.resize_image
    print "image loaded"

def resize_image(self, widget):
    allocation = self.scrolledwindow.get_allocation()
    win_h = float(allocation.height)
    win_w = float(allocation.width)
    wk = round(float(win_h / win_w), 6)

    if self.image_loc is not None:
        pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_loc)

        image_h = float(pixbuf.get_height())
        image_w = float(pixbuf.get_width())
        ik = round(float(image_h / image_w), 6)

        if image_h <= win_h and image_w <= win_w:
            pixbuf = pixbuf.scale_simple(int(image_w), int(image_h), gtk.gdk.INTERP_BILINEAR)
        elif (image_h > win_h and image_w <= win_w) or (image_h > win_h and image_w > win_w and ik >= wk):
            pixbuf = pixbuf.scale_simple(int((win_h - 30) * (1 / ik)), int(win_h) - 30, gtk.gdk.INTERP_BILINEAR)
        elif (image_h <= win_h and image_w > win_w) or (image_h > win_h and image_w > win_w and ik < wk):
            pixbuf = pixbuf.scale_simple(int(win_w) - 30, int((win_w - 30) * ik), gtk.gdk.INTERP_BILINEAR)
        else:
            print "WTF? Incorrect image size calculation"
        self.image.set_from_pixbuf(pixbuf)

    print "window resized"

画像の読み込みはうまくいき、サイズ変更は問題ありませんがCtrl+C、ウィンドウのサイズを変更するたびに必要です。なんで?私が見つけたように、問題はset_from_pixbuf()メソッド内でローカライズされています。これを削除すると、「イメージがロードされ」、「ウィンドウのサイズが変更されました」がループなしで印刷されるためです。トレースバック:

window resized
window resized
image loaded
window resized
[...lots of prints...]
window resized
^CTraceback (most recent call last):
  File "./photgal.py", line 229, in resize_image
    pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_loc)
KeyboardInterrupt
window resized
[...lots of prints...]
window resized
window resized
^CTraceback (most recent call last):
  File "./photgal.py", line 229, in resize_image
    pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_loc)
KeyboardInterrupt

このソース からの更新You can easily get into infinite loops doing this type of thing though.:これは私が理解している私の場合です。提案は、The "right way" toあるウィジェットのサイズを変更し、別のウィジェットのサイズを変更することis usually to write a custom container widget that sizes things the way you want.です。このコンテナの書き方

4

1 に答える 1