2010-07-02 4 views
1

좋아, tkinter에서 GUI가 작동하고 매 5 초마다 이미지를 캡처하여 이미지 라벨이라는 이름의 Picturelabel에 표시하려고합니다.5 초에 한 번씩 사진을 표시하고 새로 고침

from Tkinter import * 
from PIL import ImageGrab 
import cStringIO, base64, time, threading 

class PictureThread(threading.Thread): 
    def run(self): 
     print "test" 
     box = (0,0,500,500) #x,x,width,height 
     MyImage = ImageGrab.grab(box) 

     fp = cStringIO.StringIO() 
     MyImage.save(fp, 'GIF') 
     MyPhotoImage = PhotoImage(data=base64.encodestring(fp.getvalue())) 

     time.sleep(5) 
     PictureThread().run() #If I get rid of this then it just display one image 
     return MyPhotoImage 

MyVeryNewImage = PictureThread().run() 

Picturelabel = Label(BalanceFrame, image=MyVeryNewImage) 
Picturelabel.grid(row=3, column=2, columnspan=3) 
Picturelabel.image = MyVeryNewImage 

window.mainloop() 

먼저이 코드를 정리하면 다른 스레드 내부의 스레드를 시작하는 것이 좋을 수 있습니다.

또한 이것을 실행하면 콘솔에 "test"가 인쇄되지만 GUI가 표시되지 않습니다. 주석 처리 된 텍스트 (PictureThread(). run() 내에서 아직 다른 스레드를 만들고있는 곳)를 주석 처리하면 첫 번째 이미지가 표시되지만 더 이상 표시되지 않습니다.

답변

0

당신은 start() 대신 run() 호출해야합니다 :에 대해 어떻게

. Documentation에서 :

Once a thread object is created, its activity must be started by calling the thread’s start() method. This invokes the run() method in a separate thread of control.

난 당신이 run() 방법 안에 새 스레드를 호출하고 참조하십시오. 이로 인해 무한 스레드가 생성됩니다!

from Tkinter import * 
from PIL import ImageGrab 
import cStringIO, base64, time, threading 


Picturelabel = Label(BalanceFrame) 
Picturelabel.grid(row=3, column=2, columnspan=3) 

class PictureThread(threading.Thread): 

    def run(self): 
     print "test" 
     box = (0,0,500,500) #x,x,width,height 
     fp = cStringIO.StringIO() 
    while(1): 
      MyImage = ImageGrab.grab(box) 
      MyImage.save(fp, 'GIF') 
      self.image = PhotoImage(data=base64.encodestring(fp.getvalue())) 
      Picturelabel.image = self.image 
      fp.reset() # reset the fp position to the start 
      fp.truncate() # and truncate the file so we don't get garbage 
      time.sleep(5) 

PictureThread().start() 
window.mainloop() 
+0

무한한 스레드를 생성 할 때 도움을 요청한 이유는 무엇입니까? 이 질문에 대한 답변으로 위의 대답 감사합니다. –

+0

대부분의 경우, 수면 때문에 다른 스레드에 제어권을 넘겨주고 끝내기 때문에 스레드 수가 2보다 많아서는 안됩니다. – KLee1

+0

NameError : 전역 이름 'PictureLabel'입니다 정의되지 않았습니다 PhotoImage가 존재하지 않기 때문일 수 있습니까? 그러나이 버전 d GUI는 표시되지만 이미지는 표시되지 않습니다. –

0

문제는 PictureThread(). run()에서 새 이미지를 반환하지만 결코 저장하지 않는 것입니다.

from Tkinter import * 
from PIL import ImageGrab 
import cStringIO, base64, time, threading 

box = (0,0,500,500) #x,x,width,height 
MyImage = ImageGrab.grab(box) 

fp = cStringIO.StringIO() 
MyImage.save(fp, 'GIF') 
MyPhotoImage = PhotoImage(data=base64.encodestring(fp.getvalue())) 
Picturelabel = Label(BalanceFrame, image=MyPhotoImage) 
Picturelabel.grid(row=3, column=2, columnspan=3) 

class PictureThread(threading.Thread): 
    def run(self): 
     while True: 
      box = (0,0,500,500) #x,x,width,height 
      MyImage = ImageGrab.grab(box) 

      fp = cStringIO.StringIO() 
      MyImage.save(fp, 'GIF') 
      MyPhotoImage = PhotoImage(data=base64.encodestring(fp.getvalue())) 

      time.sleep(5) 
      Picturelabel.image = MyPhotoImage 

PictureThread().start() 

window.mainloop() 
+0

이것은 여전히 ​​GUI를 생성하지 :

편집 :이 작품 있는지 확실하지 않습니다. –

+0

pcostesi – KLee1

+0

에 의해 제안 된 것처럼 실행 대신 시작을 시도해보십시오. PictureThread(). start()를 실행하면 실제로 GUI가 표시되지만 아무 그림도 표시되지 않습니다. ((지금 편집을 시도 중임) –

관련 문제