[英]How to display and move multiple images as same image in same time python Tkinter?
I trying to improve offline GPS system. 我试图改善离线GPS系统。 I wrote a python code for handle images such as move as follow.
我写了一个python代码来处理图像,例如移动如下。 I want modify this code to handle multiple images.This code able to load a single image and it can move within canvas by mouse movement.
我想修改此代码以处理多个图像。此代码能够加载单个图像,并且可以通过鼠标移动在画布中移动。 I need load multiple images to this program simultaneously and show each image parts as a single image such as google maps.
我需要同时将多个图像加载到该程序,并将每个图像部分显示为单个图像,例如Google Maps。
from Tkinter import *
root = Tk(); root.geometry( "600x400" )
c = Canvas( root, width = 600, height = 400, bg = "white" ); c.pack()
class MainFrame:
"""Class to move an image on a Canvas screen ( Python 2.5 )"""
def __init__( self, image ):
self.__image = image
self.__x, self.__y = 250,250
self.__picture = c.create_image( self.__x, self.__y, image = self.__image )
self.__move = False
c.bind( "<Button-1>", self.startMovement )
c.bind( "<ButtonRelease-1>", self.stopMovement )
c.bind( "<Motion>", self.movement )
c.create_polygon((0, 100, 50, 0, 100, 100), fill="#4eccde")
def startMovement( self, event ):
global initi_x
global initi_y
initi_x = event.x
initi_y = event.y
self.__move = True
def stopMovement( self, event ):
self.__move = False
def movement( self, event ):
if self.__move:
global initi_x
global initi_y
c.delete( self.__picture )
x_axis = initi_x - event.x
y_axis = initi_y - event.y
self.__x -= x_axis
self.__y -= y_axis
print("x is ",event.x)
print("y is ",event.y)
#self.__x, self.__y = event.x ,event.y
self.__picture = c.create_image( self.__x, self.__y, image = self.__image )
initi_x = event.x
initi_y = event.y
c[ "cursor" ] = "hand2"
if __name__ == "__main__":
im = PhotoImage( file = "new1.gif" )
m = MainFrame( im )
mainloop()
I like to recommend you use the [.move(tagOrId, xAmount, yAmount)]
method to move objects in tk.canvas. 我建议您使用
[.move(tagOrId, xAmount, yAmount)]
方法在tk.canvas中移动对象。 Read more from this webpage . 从阅读这更多的网页 。
Advantages of .move() method: .move()方法的优点:
General Steps to move a tk.Canvas object : 移动tk.Canvas对象的一般步骤:
Revised Code: I have revised your code. 修改后的代码:我已经修改了您的代码。 See below.
见下文。 I have also provided comments to help you understand the general step.
我还提供了评论,以帮助您了解一般步骤。 I have also shown in the code how you can add another image object.
我还在代码中显示了如何添加另一个图像对象。 I trust you will be able to use these information to move on to create a tk.canvas with multiple images and moving them with your mouse pointer.
我相信您将能够使用这些信息继续创建带有多个图像的tk.canvas并使用鼠标指针移动它们。
from Tkinter import *
root = Tk(); root.geometry( "600x400" )
c = Canvas( root, width = 800, height = 400, bg = "white" ); c.pack()
class MainFrame:
"""Class to move an image on a Canvas screen ( Python 2.5 )"""
def __init__( self, image0, image1 ):
self.__image0 = image0
self.__image1 = image1
self.__x, self.__y = 250,250
self.__picture0 = c.create_image( self.__x, self.__y,
image = self.__image0 )
self.__picture1 = c.create_image( self.__x, self.__y,
image = self.__image1 )
c.create_polygon((0, 100, 50, 0, 100, 100), fill="#4eccde")
self.__move = False
c.bind( "<Button-1>", self.startMovement )
c.bind( "<ButtonRelease-1>", self.stopMovement )
c.bind( "<Motion>", self.movement )
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)
self.movingimage = c.find_closest(self.initi_x, self.initi_y, halo=5) # get canvas object ID of where mouse pointer is
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(self.movingimage, deltax, deltay) # move object
if __name__ == "__main__":
im0 = PhotoImage( file = "monkey.gif" )
im1 = PhotoImage( file = "smooch.gif" )
im2 = PhotoImage( file = "giphy.gif" )
m = MainFrame( im0, im1 )
mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.