Python if语句接受什么类型的对象?
在下面的代码中,我们有Python if语句接受什么类型的对象?,python,class,user-interface,if-statement,tkinter,Python,Class,User Interface,If Statement,Tkinter,在下面的代码中,我们有if self.frames,但是self.frames是一个列表。这是什么意思 import tkinter as tk from PIL import Image, ImageTk from itertools import count class ImageLabel(tk.Label): '''a label that displays images, and plays them if they are gifs''' def load(self
if self.frames
,但是self.frames
是一个列表。这是什么意思
import tkinter as tk
from PIL import Image, ImageTk
from itertools import count
class ImageLabel(tk.Label):
'''a label that displays images, and plays them if they are gifs'''
def load(self, im):
if isinstance(im, str):
im = Image.open(im)
self.loc = 0
self.frames = []
try:
for i in count(1):
self.frames.append(ImageTk.PhotoImage(im.copy()))
im.seek(i)
except EOFError:
pass
try:
self.delay = im.info['duration']
except:
self.delay = 100
if len(self.frames) == 1:
self.config(image=self.frames[0])
else:
self.next_frame()
def unload(self):
self.config(image=None)
self.frames = None
def next_frame(self):
if self.frames:
self.loc += 1
self.loc %= len(self.frames)
self.config(image=self.frames[self.loc])
self.after(self.delay, self.next_frame)
root = tk.Tk()
lbl = ImageLabel(root)
lbl.pack()
lbl.load("name_of_image.gif")
root.mainloop()
在Python中,空元组、列表、集合、字典、字符串和
None
在转换为布尔值时为False
。您可以通过启动Python解释器并键入:
print(bool(()), bool([]), bool(set()) , bool({}), bool(''), bool(None))
因此,执行
if self.frames
就像执行if len(self.frames)!=0
。它检查列表是否为空 对于列表(或任何序列),它意味着“如果该序列不是空的”。if语句检查其操作数是否真实,这对于不同的对象意味着不同的事情。在测试列表
容器的“”。请参阅联机文档中的。