So, the idea is that I try to load an image, navigate Up, Down, Right, Left with the arrows and each time I press the mouse button to get the image's coordinates, not the canvas' coordinates. I use code from here: https://stackoverflow.com/questions/5563...s-of-the-s and from here: https://stackoverflow.com/questions/3366...arrow-keys I face difficulties in achieving that... more specifically none of the keys Up, Down, Left, Right works and also when I press the mouse button nothing happens... My code is depicted below...Do you have any idea what I wrote wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
print ( "A" ) import tkinter as tk print ( "B" ) from PIL import Image, ImageTk print ( "C" ) #class Example(tk.Frame): #print("D") ##def __init__(self, parent): ##print("0") ##tk.Frame.__init__(self, parent) print ( "1" ) WIDTH, HEIGHT = 900 , 900 print ( "2" ) topx, topy, botx, boty = 0 , 0 , 0 , 0 print ( "3" ) rect_id = None print ( "4" ) path = "test_image.jpg" print ( "5" ) def get_mouse_posn(event): print ( "6" ) topx, topy = event.x, event.y print ( "7" ) print ( "topx = " , topx) print ( "8" ) print ( "topy = " , topy) print ( "9" ) def update_sel_rect(event): print ( "10" ) botx, boty = event.x, event.y print ( "11" ) canvas.coords(rect_id, topx, topy, botx, boty) # Update selection rect. print ( "12" ) print ( "botx = " , botx) print ( "13" ) print ( "boty = " , boty) window = tk.Tk() print ( "14" ) window.title( "Select Area" ) print ( "15" ) window.geometry( '%sx%s' % (WIDTH, HEIGHT)) print ( "16" ) window.configure(background = 'grey' ) print ( "17" ) img = ImageTk.PhotoImage(Image. open (path)) print ( "18" ) canvas = tk.Canvas(window, width = img.width(), height = img.height(), borderwidth = 0 , highlightthickness = 0 ) print ( "19" ) canvas.pack(expand = True ) print ( "20" ) canvas.img = img # Keep reference in case this code is put into a function. print ( "21" ) canvas.create_image( 0 , 0 , image = img, anchor = tk.NW) print ( "22" ) # Create selection rectangle (invisible since corner points are equal). rect_id = canvas.create_rectangle(topx, topy, topx, topy, dash = ( 2 , 2 ), fill = ' ', outline=' white') print ( "23" ) canvas = tk.Canvas(background = "bisque" ) print ( "24" ) vsb = tk.Scrollbar(orient = "vertical" , command = canvas.yview) print ( "25" ) hsb = tk.Scrollbar(orient = "horizontal" , command = canvas.xview) print ( "26" ) canvas.configure(xscrollcommand = hsb. set , yscrollcommand = vsb. set ) print ( "27" ) #canvas.grid(row=0, column=0, sticky="nsew") print ( "28" ) #vsb.grid(row=0, column=1, sticky="ns") print ( "29" ) #hsb.grid(row=1, column=0, sticky="ew") print ( "30" ) canvas.configure(scrollregion = canvas.bbox( "all" )) print ( "31" ) canvas.bind( "<1>" , lambda event: canvas.focus_set()) print ( "32" ) canvas.bind( "<Left>" , lambda event: canvas.xview_scroll( - 1 , "units" )) print ( "33" ) canvas.bind( "<Right>" , lambda event: canvas.xview_scroll( 1 , "units" )) print ( "34" ) canvas.bind( "<Up>" , lambda event: canvas.yview_scroll( - 1 , "units" )) print ( "35" ) canvas.bind( "<Down>" , lambda event: canvas.yview_scroll( 1 , "units" )) print ( "36" ) canvas.focus_set() print ( "37" ) canvas.bind( '<Button-1>' , get_mouse_posn) print ( "38" ) canvas.bind( '<B1-Motion>' , update_sel_rect) print ( "39" ) window.mainloop() |