468,290 Members | 1,849 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

Tkinter/PIL, How to Drag and Drop the image with mouse?

Hi,
I hv created an application with Tkinter/PIL in python25. It can import an image into the canvas. Now could someone kindly tell me how to select and move that image with mouse? Any help would be really appreciated.

Expand|Select|Wrap|Line Numbers
  1. # BirdaoGwra 
  2. # January 25, 2010
  3.  
  4. import sys, os, math
  5.  
  6. from Tkinter import *
  7. import Tkinter as tk
  8. import tkFileDialog as tkfd
  9. import tkMessageBox as tkmb
  10.  
  11. import PIL as pil
  12. from PIL import Image, ImageTk
  13.  
  14. myFormats = [
  15.         ('Windows Bitmap', '*.bmp'),
  16.         ('Portable Network Graphics', '*.png'),
  17.         ('JPEG/JFIF', '*.jpg'),
  18.         ('CompuServe GIF', '*.gif'),
  19.         ('All File', '*.*')
  20.         ]
  21.  
  22. class MyApp(object):
  23.     def __init__(self, master):
  24.         frame = tk.Frame(master)
  25.         frame.pack()
  26.  
  27.         self.canvas = tk.Canvas(width=520, height=350)
  28.         self.canvas.pack()
  29.         self.canvas.config(borderwidth=1, bg='black')
  30.  
  31.         # Create the menubar
  32.         menu = tk.Menu(master)
  33.         root.config(menu=menu)
  34.         # File menu
  35.         filemenu = tk.Menu(menu, tearoff=0)
  36.         menu.add_cascade(label="File", menu=filemenu)
  37.         filemenu.add_command(label="New          Ctrl+N", command=self.new_file)
  38.         filemenu.add_command(label="Open...      Ctrl+O", command=self.file_open)
  39.         filemenu.add_separator()
  40.         filemenu.add_command(label="Save...      Ctrl+S")
  41.         filemenu.add_command(label="Save As...   Ctrl+Shift+S", command=self.file_saveas)
  42.         filemenu.add_separator()
  43.         filemenu.add_command(label="Exit         Ctrl+Q", command=self.do_exit)
  44.  
  45.     def new_file(self):
  46.         """create a new document"""
  47.         self.canvas.delete(ALL)
  48.         # self = tkmb.showinfo("Birdao Anime!", "Yet to implement!")
  49.  
  50.     def file_open(self):
  51.         """open a file to read"""
  52.         # self.canvas.delete(ALL)
  53.         filename = tkfd.askopenfilename(filetypes=myFormats)
  54.         if filename != '':
  55.             im = Image.open(filename)
  56.             if im.mode != "RGBA":
  57.                 im = Image.open(filename).convert("RGBA")
  58.                 source = im.split()
  59.                 R, G, B, A = 0, 1, 2, 3
  60.                 mask = im.point(lambda i: i> 0 and 255)
  61.                 source[A].paste(mask)
  62.                 im = Image.merge(im.mode, source)
  63.  
  64.         self.graphic = ImageTk.PhotoImage(image=im)
  65.         self.canvas.create_image(100,100, image=self.graphic)
  66.  
  67.     def file_saveas(self):
  68.         """get a file name and save it"""
  69.         # no default file
  70.         filename = tkfd.asksaveasfile(filetypes=myFormats)
  71.         if filename:
  72.             self.saveimage.save(filename)
  73.  
  74.     def do_exit(self):
  75.         root.destroy()
  76.  
  77. root = tk.Tk()
  78. root.title("Birdao Anime 0.1")
  79. root.config(bg='gray')
  80.  
  81. # use width x height + x_offset + y_offset (no spaces!)
  82. root.geometry("%dx%d+%d+%d" % (640, 480, 120, 80))
  83. myapp = MyApp(root)
  84. root.mainloop()
And also how to center the canvas?

Thanks in advance
Regards
Jan 25 '10 #1
0 1788

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by portraitmaker | last post: by
2 posts views Thread by SamSpade | last post: by
3 posts views Thread by Ajay Krishnan Thampi | last post: by
2 posts views Thread by sebastian.janoschka | last post: by
5 posts views Thread by murrayatuptowngallery | last post: by
7 posts views Thread by Peter Pearson | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.