473,473 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

1 New Member
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 2042

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

Similar topics

8
by: Marc | last post by:
Hi all, I was thinking about developing a drag and drop application and was curious how difficult it is to do. Basically I want to have a set of objects that I move around the screen and drop...
1
by: portraitmaker | last post by:
I found some drag and drop code on the web and modified it a little b taking out some of the stuff I didn't need. This sample allows you to drag an image in a table to another positio and swaps...
2
by: SamSpade | last post by:
There seems to be two ways to put things on the clipboard ( I don't mean different formats): SetClipboardData and OleSetClipboard If I want to get data off the clipboard do I care how it was put...
3
by: Ajay Krishnan Thampi | last post by:
I have a slight problem implementing 'drag and drop' from a datagrid to a tree-view. I have pasted my code below. Someone please advice me on what to do...pretty blur right now. ==code== ...
2
by: sebastian.janoschka | last post by:
Hi, I build my first Drag & Drop with JavaScript and I would like to drag the pictures when I click on it. When I create a normal div tag with some text the script works, but when I put a...
5
by: murrayatuptowngallery | last post by:
I would like to have a slider or mouse position click capture means of collecting a user input from a scale or ruler-like graphic. I found some code snippets for mouse position capture on...
0
by: arvinds | last post by:
I have an application in which I have 2 forms. 1.Review Form 2. FilmForm. Review Form is used for Loading some images and we can transfer the Images from Review From to FilmForm by Drag-Drop...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
4
by: liangchen76 | last post by:
Hey, I am writing a web app to allow users to draw over an image. The workflow of drawing a rectangle is: 1. click a mouse button 2. hold and drag 3. while the mouse moving, the size of a...
7
by: Peter Pearson | last post by:
Tkinter makes it very easy to drag jpeg images around on a canvas, but I would like to have a "target" change color when the cursor dragging an image passes over it. I seem to be blocked by the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.