简体   繁体   English

如何在同一时间python Tkinter中将多个图像显示和移动为同一图像?

[英]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()方法的优点:

  1. This can help you reduce the amount of codes you need to write. 这可以帮助您减少所需编写的代码量。
  2. It is more efficient than your approach. 它比您的方法更有效。 Your present approach of creating and destroying is inefficient and I would not recommend it. 您当前的创建和销毁方法效率低下,我不建议您这样做。
  3. It will allow you to move any objects (ie many images) your mouse pointer touches. 它将允许您移动鼠标指针触摸的任何对象(即许多图像)。

General Steps to move a tk.Canvas object : 移动tk.Canvas对象的一般步骤:

  1. Convert mouse pointer screen coordinate to canvas coordinate. 将鼠标指针屏幕坐标转换为画布坐标。
  2. Get canvas object ID using the mouse pointer canvas coordinate. 使用鼠标指针的画布坐标获取画布对象ID。
  3. Initialize mouse pointer canvas coordinate as start point. 初始化鼠标指针的画布坐标为起点。
  4. During move, get mouse pointer new canvas coordinate. 在移动过程中,获取鼠标指针新的画布坐标。
  5. Find the delta of new and old canvas coordinate. 查找新旧画布坐标的增量。
  6. Update mouse pointer initial canvas coordinate with mouse pointer new canvas given in step 4. 使用步骤4中给出的鼠标指针新画布更新鼠标指针初始画布坐标。
  7. Use that info and object ID to move the object. 使用该信息和对象ID来移动对象。

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.

相关问题 如何同时在tkinter画布上更改和移动图像? - How can I change and move an image on a tkinter Canvas at same time? 我如何同时移动多个对象,但每个 object 在 tkinter python 中移动不同的距离? - How do i move multiple objects at the same time but every object by different distance in tkinter python? 如何在python中同时移动多只海龟? - How to move multiple turtles at the same time in python? 如何显示多个图像,每次尝试的图像数量都不相同 - How to display multiple images which numbers of image not same for every attempt 如何在python(如SanctuaryRPG)中同时显示图像和文字? - How to display image and text at the same time in python (like SanctuaryRPG)? Tkinter-无法同时打开多张照片(Python) - Tkinter - can't open multiple photos at the same time (Python) 如何同时使用键盘键移动两个图像? - How to move two images using keyboard keys at the same time? 如何在 Python 中为 tkinter 在同一个 GUI 中显示输入和输出 - How to display input and output in same GUI for tkinter in Python 绑定到Tkinter中的键的“移动对象”功能只能一次移动一个对象,如何使更多的对象同时移动? - 'Move object' function bound to a key in Tkinter can only make one object move at a time, how to make more object move at the same time? 依次在Python Tkinter中显示多个图像 - Display multiple images in Python Tkinter Sequentially
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM