Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python if语句接受什么类型的对象?_Python_Class_User Interface_If Statement_Tkinter - Fatal编程技术网

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语句检查其操作数是否真实,这对于不同的对象意味着不同的事情。在测试
列表
容器的“”。请参阅联机文档中的。