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.
And also how to center the canvas?
Thanks in advance
Regards
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.
Code:
# BirdaoGwra
# January 25, 2010
import sys, os, math
from Tkinter import *
import Tkinter as tk
import tkFileDialog as tkfd
import tkMessageBox as tkmb
import PIL as pil
from PIL import Image, ImageTk
myFormats = [
('Windows Bitmap', '*.bmp'),
('Portable Network Graphics', '*.png'),
('JPEG/JFIF', '*.jpg'),
('CompuServe GIF', '*.gif'),
('All File', '*.*')
]
class MyApp(object):
def __init__(self, master):
frame = tk.Frame(master)
frame.pack()
self.canvas = tk.Canvas(width=520, height=350)
self.canvas.pack()
self.canvas.config(borderwidth=1, bg='black')
# Create the menubar
menu = tk.Menu(master)
root.config(menu=menu)
# File menu
filemenu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New Ctrl+N", command=self.new_file)
filemenu.add_command(label="Open... Ctrl+O", command=self.file_open)
filemenu.add_separator()
filemenu.add_command(label="Save... Ctrl+S")
filemenu.add_command(label="Save As... Ctrl+Shift+S", command=self.file_saveas)
filemenu.add_separator()
filemenu.add_command(label="Exit Ctrl+Q", command=self.do_exit)
def new_file(self):
"""create a new document"""
self.canvas.delete(ALL)
# self = tkmb.showinfo("Birdao Anime!", "Yet to implement!")
def file_open(self):
"""open a file to read"""
# self.canvas.delete(ALL)
filename = tkfd.askopenfilename(filetypes=myFormats)
if filename != '':
im = Image.open(filename)
if im.mode != "RGBA":
im = Image.open(filename).convert("RGBA")
source = im.split()
R, G, B, A = 0, 1, 2, 3
mask = im.point(lambda i: i> 0 and 255)
source[A].paste(mask)
im = Image.merge(im.mode, source)
self.graphic = ImageTk.PhotoImage(image=im)
self.canvas.create_image(100,100, image=self.graphic)
def file_saveas(self):
"""get a file name and save it"""
# no default file
filename = tkfd.asksaveasfile(filetypes=myFormats)
if filename:
self.saveimage.save(filename)
def do_exit(self):
root.destroy()
root = tk.Tk()
root.title("Birdao Anime 0.1")
root.config(bg='gray')
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (640, 480, 120, 80))
myapp = MyApp(root)
root.mainloop()
Thanks in advance
Regards