开发者

Displaying and refreshing my picture every 5 seconds

Ok, I've got the GUI in tkinter working, and I'm trying to grab and image every 5 seconds and display it in a Label named Picturelabel.

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

class PictureThread(threading.Thread):
    def run(self):
        print "test"
       开发者_Go百科 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()

Firstly how can I clean up this code, as starting a thread inside another thread can't be good practice.

Also when I run this it prints "test" in the console, but it does not bring up the GUI. If I comment out the commented text (PictureThread().run() where I'm creating yet another thread inside it.) then it displays the first image, but not any more.


You should call start() instead of run(). From the 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.

I see you're invoking a new thread inside your run() method. This will cause you to spawn infinite threads!

EDIT: I'm not sure if this works:

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()


The problem is that you return the new image from the PictureThread().run() in the method, but you never save it.

How about:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜