Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

draw box with mouse on image in canvas tkinter

import PIL.Image
import Image
import ImageTk
from Tkinter import *    


class ExampleApp(Frame):
    def __init__(self,master):
        Frame.__init__(self,master=None)
        self.x = self.y = 0
        self.canvas = Canvas(self,  cursor="cross")

        self.sbarv=Scrollbar(self,orient=VERTICAL)
        self.sbarh=Scrollbar(self,orient=HORIZONTAL)
        self.sbarv.config(command=self.canvas.yview)
        self.sbarh.config(command=self.canvas.xview)

        self.canvas.config(yscrollcommand=self.sbarv.set)
        self.canvas.config(xscrollcommand=self.sbarh.set)

        self.canvas.grid(row=0,column=0,sticky=N+S+E+W)
        self.sbarv.grid(row=0,column=1,stick=N+S)
        self.sbarh.grid(row=1,column=0,sticky=E+W)

        self.canvas.bind("<ButtonPress-1>", self.on_button_press)
        self.canvas.bind("<B1-Motion>", self.on_move_press)
        self.canvas.bind("<ButtonRelease-1>", self.on_button_release)

        self.rect = None

        self.start_x = None
        self.start_y = None

        self.im = PIL.Image.open("logo.png")
        self.wazil,self.lard=self.im.size
        self.canvas.config(scrollregion=(0,0,self.wazil,self.lard))
        self.tk_im = ImageTk.PhotoImage(self.im)
        self.canvas.create_image(0,0,anchor="nw",image=self.tk_im)   


    def on_button_press(self, event):
        # save mouse drag start position
        self.start_x = self.canvas.canvasx(event.x)
        self.start_y = self.canvas.canvasy(event.y)

        # create rectangle if not yet exist
        if not self.rect:
            self.rect = self.canvas.create_rectangle(self.x, self.y, 1, 1, outline='red')

    def on_move_press(self, event):
        curX = self.canvas.canvasx(event.x)
        curY = self.canvas.canvasy(event.y)

        w, h = self.canvas.winfo_width(), self.canvas.winfo_height()
        if event.x > 0.9*w:
            self.canvas.xview_scroll(1, 'units') 
        elif event.x < 0.1*w:
            self.canvas.xview_scroll(-1, 'units')
        if event.y > 0.9*h:
            self.canvas.yview_scroll(1, 'units') 
        elif event.y < 0.1*h:
            self.canvas.yview_scroll(-1, 'units')

        # expand rectangle as you drag the mouse
        self.canvas.coords(self.rect, self.start_x, self.start_y, curX, curY)    

    def on_button_release(self, event):
        pass    

if __name__ == "__main__":
    root=Tk()
    app = ExampleApp(root)
    app.pack()
    root.mainloop()
Comment

PREVIOUS NEXT
Code Example
Python :: queue using linked list in python 
Python :: making gifs via python 
Python :: DLL Injection in python 
Python :: python snake case to camel case 
Python :: get names of all file in a folder using python 
Python :: python get current date and time 
Python :: python Change the second item 
Python :: pandas group by include nan 
Python :: python convert to hmac sha256 
Python :: Read JSON files with automatic schema inference 
Python :: python pip Failed to build cryptography 
Python :: target ordinary encodiing) 
Python :: increment in python 
Python :: python sum of list axes 
Python :: python delete from dictionary 
Python :: python division 
Python :: cli args python 
Python :: sending email with django 
Python :: python logging basicConfig+time 
Python :: remove duplicates from tuple python 
Python :: how to make a loading gif in pyqt5 
Python :: python visualize fft of an image 
Python :: np sum 
Python :: how to know if the space button has been clicked in python pygame 
Python :: How do I merge two dictionaries in a single expression (taking union of dictionaries)? 
Python :: letters to numbers python 
Python :: install django in windows 
Python :: python area of rectangle 
Python :: how to clear the list in python 
Python :: python enum advanced 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =