2013-06-25 2 views
1

나는 이해가 안되는 문제에 갇혀있다. 여기이미지를 동적으로 크기 조정 pyGTK (파이썬)

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

하고는 다음과 같습니다 : 내가 두 신호가

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

클래스 HelloWorld 내부에서 메인 프로그램을 실행

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() 메서드 내에서 지역화됩니다. 왜냐하면 내가 제거하면 "이미지로드"가 발생하고 루프가없는 "윈도우 크기가 조정 된"인쇄물이 인쇄되기 때문입니다. 역 추적 : You can easily get into infinite loops doing this type of thing though. 내가, 제안은 또 다른 is usually to write a custom container widget that sizes things the way you want. 크기를 조정할 동안 The "right way" to 한 위젯의 크기를 조정 이해로 제 경우입니다 : this 소스에서

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 

업데이트 합니다. 이 컨테이너를 쓰는 방법?

+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

감사합니다! GTK3 관계자 대신 PyGTK2 문서를 읽었습니다 ... – boldnik

+0

사람들은 왜 대답을 받아들이지 만 투표하지는 않습니까?! – saeedgnu

+0

github 링크가 404 오류를 발생시킵니다. – fossfreedom

관련 문제