2017-02-19 4 views
0

オフラインGPSシステムを改善しようとしています。私は次のように移動するような画像を扱うためのPythonコードを書いた。私はこのコードを変更して複数の画像を処理したいのです。このコードは単一の画像を読み込むことができ、マウスの動きによってキャンバス内を移動することができます。私はこのプログラムに複数の画像を同時にロードし、各画像部分をGoogleマップなどの単一の画像として表示する必要があります。複数の画像を同じ時間に同じ画像として表示および移動する方法python Tkinter?

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() 
+0

コードのインデントとスペースを親切に修正できますか? –

答えて

0

私はあなたがtk.canvasでオブジェクトを移動する[.move(tagOrId, xAmount, yAmount)]メソッドを使用することをお勧めしたいです。詳しくはwebpageをご覧ください。 .moveの

メリット()メソッド:

  1. これはあなたが書く必要があるコードの量を減らすことができます。
  2. あなたのアプローチよりも効率的です。あなたの現在の作成と破壊のアプローチは非効率的で、私はそれをお勧めしません。
  3. マウスポインタが触れるオブジェクト(つまり、多くの画像)を移動できます。

一般的な手順はtk.Canvasオブジェクトを移動する:

  1. 変換マウスポインタ画面座標キャンバスに座標。
  2. マウスポインタのキャンバス座標を使用してキャンバスオブジェクトIDを取得します。
  3. マウスポインターのキャンバス座標を開始点として初期化します。
  4. 移動中に、新しいキャンバス座標をマウスポインタで取得します。
  5. 新しいキャンバスと古いキャンバスの座標のデルタを検索します。
  6. 手順4で指定したマウスポインタの新しいキャンバスでマウスポインタの初期キャンバス座標を更新します。
  7. オブジェクトを移動するには、その情報とオブジェクトIDを使用します。

改訂コード: 私はあなたのコードを修正しました。下記参照。私はまた、一般的なステップを理解するのに役立つコメントを提供しました。また、コード内に別のイメージオブジェクトを追加する方法を示しました。これらの情報を使って、複数の画像を持つ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() 
+0

あなたの改訂コードは完璧です。しかし、私はすべての読み込まれた画像を1つの画像として同時に移動したいと思います。このコードを改訂できますか? –

+0

@ireshanpathirana '.find_closest()メソッドの直後にカンバスメソッド' .addtag_withtag(newTag、tagOrId).'を使用して、すべての四角形に同じタグ名を付けることができます。また、マウスのクリック、解放、移動のイベントにバインドした 'Checkbutton'または' Radiobutton'を作成して、すべての矩形オブジェクトを一緒に移動する必要があります。それらのコードは上記と似ていますが、すべての矩形を移動するために使用されます。上記のコードを自分で修正し、必要に応じて新しい問題で質問を更新してください。セクトを参照してください。 http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.htmlの8〜8.6 –

+0

Thnkあなたはとてもすごいです。私はこのコードが私の自己を修正することがより良い方法と思われました。もう一度ありがとうございます –

関連する問題