我被困在我不明白的問題上。主程序運行從動態調整圖像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.
。如何寫這個容器?
已接受的答案不再有解決此問題的信息 - 您有任何機會可以發佈您用來解決此問題的答案嗎? – fossfreedom
對不起,舊線程,沒有工作代碼。嘗試這樣的:https://github.com/onyxfish/ration – boldnik