python持续显示图片
时间: 2023-11-03 20:59:54 浏览: 87
要实现Python持续显示图片,可以使用tkinter库和PIL库。下面是几种方法:
方法一:
```python
from tkinter import *
from PIL import ImageTk, Image
class Test(object):
def __init__(self, master):
self.root = master
self._label = None
self._img = None
self._page()
def _page(self):
Button(self.root, text='显示图片', command=self.choose_pic).pack()
self._label = Label(self.root, image=None)
self._label.pack()
self.root.mainloop()
def choose_pic(self):
img_open = Image.open('./1.jpg')
self._img = ImageTk.PhotoImage(img_open)
self._label.configure(image=self._img)
if __name__ == '__main__':
window = Tk()
Test(window)
window.mainloop()
```
方法二:
```python
from tkinter import *
from PIL import ImageTk, Image
def choose_pic():
img_open = Image.open('./1.jpg')
img = ImageTk.PhotoImage(img_open)
_label.configure(image=img)
if __name__ == '__main__':
root = Tk()
root.wm_minsize(900, 600)
Button(root, text='显示图片', command=choose_pic).pack()
_label = Label(root, image=None)
_label.pack()
root.mainloop()
```
方法三:
```python
from tkinter import *
from PIL import ImageTk, Image
def choose_pic():
img_open = Image.open('./1.jpg')
img = ImageTk.PhotoImage(img_open)
_label.configure(image=img)
_label.image = img # keep a reference
if __name__ == '__main__':
root = Tk()
root.wm_minsize(900, 600)
Button(root, text='显示图片', command=choose_pic).pack()
_label = Label(root, image=None)
_label.pack()
root.mainloop()
```
阅读全文