2013-06-25 44 views
1

我被困在我不明白的問題上。主程序運行從動態調整圖像pyGTK(python)

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

HelloWorld類我有兩個信號:

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 

更新this來源: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.。如何寫這個容器?

+0

已接受的答案不再有解決此問題的信息 - 您有任何機會可以發佈您用來解決此問題的答案嗎? – fossfreedom

+0

對不起,舊線程,沒有工作代碼。嘗試這樣的:https://github.com/onyxfish/ration – boldnik

回答

1

resize_image()是一個無限循環。因爲如果窗口收到check-resize信號,resize_image()被調用,並且圖像被重新渲染,並且另一個check-resize信號被再次命中....

因此,我們需要一些技巧來打破這一點。

我寫了一個小型的演示應用程序,在這裏,https://github.com/LiuLang/gtk-test/tree/master/resize-image,它解決了這個問題。

+0

謝謝!我已閱讀PyGTK2文檔,而不是使用GTK3官員... – boldnik

+0

爲什麼人們接受答案但不投票呢?! – saeedgnu

+0

該github鏈接給出了一個404錯誤:( – fossfreedom