|
发表于 2016-5-22 01:24:02
|
显示全部楼层
- #!/usr/bin/env python
& ?- P- L5 u* s7 J/ Z9 ]% F! W, g - # -*- coding: utf-8 -*-! ]5 f' Y% @6 e
- from Tkinter import *, D9 ?9 W3 `4 n- _
- from tkFileDialog import askopenfilename0 t% [& i, ^) r& W+ v4 k/ U! D/ T( P
- from PIL import Image, ImageTk
+ Y# K) I0 A7 L9 E z
8 W, a( e/ f- x7 D! ]; _- if __name__ == "__main__":
2 i' U- C+ v3 C- Q2 u - root = Tk()
7 |* [) C4 P4 R9 Z' y+ W - #setting up a tkinter canvas with scrollbars
5 V! y6 D' K: e0 A, T - frame = Frame(root, bd=2, relief=SUNKEN)
" A2 s! Y0 a4 U9 p - frame.grid_rowconfigure(0, weight=1)) E6 ~- K- ]% U$ C7 M6 q' e
- frame.grid_columnconfigure(0, weight=1) w' G) ?2 g' @* |( N
- xscroll = Scrollbar(frame, orient=HORIZONTAL)
$ o+ m+ \8 z* F* B9 e( u - xscroll.grid(row=1, column=0, sticky=E+W)- @) c$ X% K- R( }* r
- yscroll = Scrollbar(frame)5 ` I7 {# O* V- N" |* \' f
- yscroll.grid(row=0, column=1, sticky=N+S)
+ o. l6 Y/ E) v9 T* s - canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
! V/ l. v1 W. Q - canvas.grid(row=0, column=0, sticky=N+S+E+W)1 M& f( {2 y, v; B5 V
- xscroll.config(command=canvas.xview)8 X8 r1 g' [! t% \& S8 B- w; I: E- N
- yscroll.config(command=canvas.yview)
* C! ~3 b" F( Z7 F; J9 i: ?* E3 q/ N8 c b - frame.pack(fill=BOTH,expand=1)
2 p! r2 c0 S) p% j - ! S# h* t- _2 [
- #adding the image
6 g( j/ ~ j* `& c! A - File = askopenfilename(parent=root, initialdir="C:/Users/dell/Desktop/意汉词典/_ok",title='Choose an image.')& _1 v" |, ?3 A0 g$ Z1 v' f R
- img = ImageTk.PhotoImage(Image.open(File))
; E1 N, D* @0 f9 l2 Y" V0 R9 G - canvas.create_image(0,0,image=img,anchor="nw")" [. [, S# M0 y, [
- canvas.config(scrollregion=canvas.bbox(ALL))
3 [) U" s9 E5 a) C% f - * Y1 b% O2 D6 t" d" B! c8 z
- #function to be called when mouse is clicked4 E2 T3 d) ?- c1 N; L& t* C
- def printcoords(event):
3 u; v. _2 V9 f - #outputting x and y coords to console
" w' d6 D/ y9 r' k( S - canvas = event.widget' C% Z6 _' Q! E6 C# }0 g
- x = canvas.canvasx(event.x)% ]& G: {; N6 T
- y = canvas.canvasy(event.y)7 r v" M% E9 z7 O3 S# \( I
- oval = canvas.create_oval(x-8, y-8, x+8, y+8, fill="red")# p p2 ?0 b3 G$ S* f8 l: j
- print x,y
- n6 e& _0 P5 Z( C( H - f = open('_coords.txt','a')' l* _8 `$ W! x
- f.write('X: %s\tY: %s\n' % (x,y))
. ^2 I8 w; k8 {# O- g$ ~ - f.close()
& i, O2 m* m- X* Z8 Z, U2 U0 ? - / H3 {2 U t: P7 N
- #mouseclick event
( G% q3 `8 I, d - canvas.bind("<Button 1>",printcoords)
$ i9 G5 B6 _. V( Y - root.mainloop()
/ X3 g5 Z$ c# t+ Y+ f( k7 p' ^1 Q
复制代码
2 \# X2 C' A7 x4 P1 Q
) Q- G% X$ I. d& [. ~来源处:0 k; `; A' f: q" w8 T* Z- k$ K
http://stackoverflow.com/questio ... ck-coordinate-on-it |
|