|
发表于 2016-5-22 01:24:02
|
显示全部楼层
- #!/usr/bin/env python
8 J0 P0 {+ H9 s! f+ q) ]% t) q% t ^5 h - # -*- coding: utf-8 -*-/ i. o6 }7 g- Y
- from Tkinter import *
* E) f/ f3 n# Z! n7 v6 \8 W - from tkFileDialog import askopenfilename2 h& K% g9 D8 S! A
- from PIL import Image, ImageTk* k! h2 [ H& t2 ?7 B# e9 S
- 9 D0 g7 Q+ D A& d3 B
- if __name__ == "__main__":3 j% I7 ]; L( X/ A U2 h
- root = Tk()
3 y* L( r/ G$ ]% _ - #setting up a tkinter canvas with scrollbars, w5 Q7 t" m. J1 Q3 S
- frame = Frame(root, bd=2, relief=SUNKEN)8 S! _6 z" i9 X2 _: e' p9 [& @
- frame.grid_rowconfigure(0, weight=1)7 u7 _6 j' Y* ]3 S7 C
- frame.grid_columnconfigure(0, weight=1)& {& _* S/ a2 |
- xscroll = Scrollbar(frame, orient=HORIZONTAL)
! Q, |" k. V9 W* V - xscroll.grid(row=1, column=0, sticky=E+W)
# E% W( S* H3 S6 {% _ - yscroll = Scrollbar(frame) Z. Z0 d' d6 u# U8 b2 u7 O
- yscroll.grid(row=0, column=1, sticky=N+S)
* F8 X0 u( m8 b - canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)8 ^6 p) G# O% c- w
- canvas.grid(row=0, column=0, sticky=N+S+E+W)
. o7 R" y) A* X: ]6 P - xscroll.config(command=canvas.xview). U, v. `5 s V2 I" W B7 A
- yscroll.config(command=canvas.yview)& [" x. p+ I1 U: O
- frame.pack(fill=BOTH,expand=1)
b7 ]; f9 a3 b# i9 [
: w+ E3 h# S$ R3 ]- #adding the image
) k1 t# {5 R5 }, @1 I3 l$ F - File = askopenfilename(parent=root, initialdir="C:/Users/dell/Desktop/意汉词典/_ok",title='Choose an image.')
* `" b; b- w2 D: S - img = ImageTk.PhotoImage(Image.open(File))+ F% S2 O' l! ~5 o2 y' S2 ~* P
- canvas.create_image(0,0,image=img,anchor="nw")/ ~( f0 ]1 q4 _5 ^/ V. g9 x2 H
- canvas.config(scrollregion=canvas.bbox(ALL))
b5 }& T' X; W2 q( N( c% g3 G - 2 G0 J/ s" T5 Z. H
- #function to be called when mouse is clicked
6 D3 f* L* l9 T* b J1 F( W - def printcoords(event):7 m& T4 I% _( ]$ h2 k2 z. W2 D
- #outputting x and y coords to console0 Z$ U" R3 \" J( f5 A; F% J
- canvas = event.widget
1 k- H9 a1 j" q- O - x = canvas.canvasx(event.x)
" w3 J& U7 T! c0 n4 t7 M0 K - y = canvas.canvasy(event.y)+ W& f9 G' \4 K! }; w
- oval = canvas.create_oval(x-8, y-8, x+8, y+8, fill="red")
, O( X- x$ s9 z8 Y6 K( ] ^ - print x,y
/ O: y0 X; m4 L3 u* R: n6 P, R b8 c - f = open('_coords.txt','a')
" n4 ^1 Z5 I- c$ {' s% S5 K4 J - f.write('X: %s\tY: %s\n' % (x,y))# X5 g0 w+ ?6 ~9 ^
- f.close()- ]6 x1 B/ F9 J
- 3 g' E6 f+ D! @' o) y$ @+ H
- #mouseclick event
; q; E. [* }( N. m" L1 t1 \% _ - canvas.bind("<Button 1>",printcoords)
. e3 M% e5 s+ @; R% [& ? - root.mainloop()
$ I2 v( m+ N7 C6 A, G9 t& i1 A
复制代码 0 |( M) n% y1 \3 M) F! i# j! F
* K; v7 N4 Y! T- r) x, A+ O
来源处:
6 `5 {, A) g8 }) \' o1 D$ `http://stackoverflow.com/questio ... ck-coordinate-on-it |
|