我正在尝试构建图像loader.i使用exec函数生成随机图像变量。但解释器说意外的缩进请帮助我。
我使用exec函数为画布生成随机图像。
Traceback (most recent call last):
File "C:\Users\Desktop\gggg.py", line 74, in <module>
MainFrame()
File "C:\Users\Desktop\gggg.py", line 10, in __init__
self.loadImages()
File "C:\Users\Desktop\gggg.py", line 66, in loadImages
for k,v in c.iteritems():exec("%s = %s" % (k,v))
File "<string>", line 1
self.__image0 = PhotoImage( file = 'CRD.gif' )
^
IndentationError: unexpected indent
from Tkinter import *
root = Tk(); root.geometry( "320x240" )
c = Canvas( root, width = 320, height = 240, bg = "white" ); c.pack()
class MainFrame:
"""Class to move an image on a Canvas screen ( Python 2.5 )"""
def __init__( self):
self.loadImages()
#self.__image0 = PhotoImage( file = "CRD.gif" )
#self.__image1 = PhotoImage( file = "CRD.gif" )
#self.__image2 = PhotoImage( file = "CRD.gif" )
self.__picture0 = c.create_image( 100,100,image = self.__image0 )
# self.__picture1 = c.create_image( 300, 100,image = self.__image1 )
# self.__picture2 = c.create_image( 500, 100,image = self.__image2 )
self.__move = False
c.bind( "<Button-1>", self.startMovement )
c.bind( "<ButtonRelease-1>", self.stopMovement )
c.bind( "<Motion>", self.movement )
c.addtag_all(7)
def startMovement( self, event ):
self.__move = True
self.initi_x = c.canvasx(event.x) #Translate mouse x screen coordinate to canvas coordinate
self.initi_y = c.canvasy(event.y) #Translate mouse y screen coordinate to canvas coordinate
print('startMovement init', self.initi_x, self.initi_y)
ID of where mouse pointer is c.addtag_withtag(1,3)
print(self.movingimage)
print(c.find_all()) # get all canvas objects ID
def stopMovement( self, event ):
self.__move = False
def movement( self, event ):
if self.__move:
end_x = c.canvasx(event.x) #Translate mouse x screen coordinate to canvas coordinate
end_y = c.canvasy(event.y) #Translate mouse y screen coordinate to canvas coordinate
print('movement end', end_x, end_y)
deltax = end_x - self.initi_x #Find the difference
deltay = end_y - self.initi_y
print('movement delta', deltax, deltay)
self.initi_x = end_x #Update previous current with new location
self.initi_y = end_y
print('movement init', self.initi_x, self.initi_y)
c.move(1,deltax,deltay) # move object
c.move(2,deltax,deltay) # move object
c.move(3,deltax,deltay) # move object
def loadImages(self):
c = {" self.__image0": "PhotoImage( file = 'CRD.gif' )",
"self.__image1": "PhotoImage( file = 'CRD.gif' )","self.__image2":
"PhotoImage( file = 'CRD.gif' )"}
for k,v in c.iteritems():exec("%s = %s" % (k,v))
if __name__ == "__main__":
MainFrame()
mainloop()