|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我要将一些网络图片显示到tkinter的label上,以下是部分代码:
- def zoom_image(url):
- #将缩小后的图片转为label可以显示的格式
- image_bytes = urlopen(url).read()
- data_stream = io.BytesIO(image_bytes)
- pil_image = Image.open(data_stream)
- w,h = pil_image.size
- def resize(w,h,pil_image):
- #将网络图片按照比例缩小尺寸
- f1 = 1.0*142/w
- f2 = 1.0*172/h
- factor = min([f1,f2])
- width = int(w*factor)
- height = int(h*factor)
- return pil_image.resize((width,height),Image.ANTIALIAS)
- pil_image_resize = resize(w,h,pil_image)
- [color=Red] image = ImageTk.PhotoImage(pil_image_resize)[/color]
- return image
复制代码
这部分代码直接运行的话很正常,但是如果想要通过线程去执行 zoom_image(url) ,就会在最后一行 'image = ImageTk.PhotoImage(pil_image_resize)' 出错。错误信息如下:
- Exception in thread Thread-2:
- Traceback (most recent call last):
- File "D:\Program Files\Python\lib\threading.py", line 917, in _bootstrap_inner
- self.run()
- File "D:\Program Files\Python\lib\threading.py", line 865, in run
- self._target(*self._args, **self._kwargs)
- File "C:\Users\kangaroo\Desktop\1212112.py", line 166, in showtime
- photo[i] = Data_Processing.zoom_image(image_list[i])
- File "C:\Users\kangaroo\Desktop\1212112.py", line 86, in zoom_image
- image = ImageTk.PhotoImage(image=pil_image_resize,size=pil_image_resize.size)
- File "D:\Program Files\Python\lib\site-packages\PIL\ImageTk.py", line 118, in __init__
- self.__photo = tkinter.PhotoImage(**kw)
- File "D:\Program Files\Python\lib\tkinter\__init__.py", line 3545, in __init__
- Image.__init__(self, 'photo', name, cnf, master, **kw)
- File "D:\Program Files\Python\lib\tkinter\__init__.py", line 3501, in __init__
- self.tk.call(('image', 'create', imgtype, name,) + options)
- RuntimeError: main thread is not in main loop
- Exception ignored in: <function PhotoImage.__del__ at 0x000001C5D056CD90>
- Traceback (most recent call last):
- File "D:\Program Files\Python\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
- name = self.__photo.name
- AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
复制代码
有没有高手可以告诉我是为什么吗? |
|