draw box with mouse on image in canvas tkinter 1

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("", self.on_button_press)
        self.canvas.bind("", self.on_move_press)
        self.canvas.bind("", 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()

Here is what the above code is Doing:
1. Create a canvas and scrollbars.
2. Bind the scrollbars to the canvas.
3. Bind the mouse events to the canvas.
4. Create a rectangle on the canvas.
5. When the mouse is dragged, expand the rectangle.
6. When the mouse is released, do nothing.

The only thing that's missing is the scrolling.

The scrolling is done in the on_move_press method.

The idea is to check if the mouse is near the edge of the canvas.
If it is, then scroll the canvas.

The scrolling is done by calling the xview_scroll and yview_scroll methods.

The first argument is the number of units to scroll.
The second argument is the type of units to scroll.

The units can be "units" or "pages".

The "units" option will scroll by one unit.
The "pages" option will scroll by one page.

The page size is determined by the size of the canvas.

The size of the canvas is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the scrollbars is determined by the size of the window.

The size of the window is determined by the size of the scrollbars.

The size of the

Similar Posts