|
|
发表于 2016-5-22 01:24:02
|
显示全部楼层
- #!/usr/bin/env python
g8 c+ c- [; F; P% q* ` - # -*- coding: utf-8 -*-9 j! P8 U9 c5 f- C, M4 R$ ^8 _$ M
- from Tkinter import *
9 L5 Q0 i4 `3 j' \ - from tkFileDialog import askopenfilename: L& ]9 }5 E* S
- from PIL import Image, ImageTk- e$ B' }2 \* E7 f3 f8 A
- 3 [$ S& `- W$ p* N
- if __name__ == "__main__":
8 B( i; ~. Z, H3 X - root = Tk()) g8 P( b5 r/ H) k; R p
- #setting up a tkinter canvas with scrollbars+ R3 o+ a4 y H, w6 s1 q: i
- frame = Frame(root, bd=2, relief=SUNKEN)5 ]# v5 N) |; s" D
- frame.grid_rowconfigure(0, weight=1)
4 D0 S' P5 A; B" T1 z" a - frame.grid_columnconfigure(0, weight=1). p k' U, l( j' X$ x( [* i3 ?
- xscroll = Scrollbar(frame, orient=HORIZONTAL)
5 U' K/ l9 j+ L" u - xscroll.grid(row=1, column=0, sticky=E+W): K8 @- O; u4 i$ k; G
- yscroll = Scrollbar(frame)
% V6 W% U, `2 {/ i - yscroll.grid(row=0, column=1, sticky=N+S)
& t. r D3 U! R& p/ N - canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
4 @, p3 f0 B% D$ B3 D( x$ | - canvas.grid(row=0, column=0, sticky=N+S+E+W)
1 C$ k/ G7 ?( I3 u U8 V* G/ n - xscroll.config(command=canvas.xview)
( w" {; `7 {9 ` - yscroll.config(command=canvas.yview)
2 h. y1 [9 _* [; A: W8 U: w% I - frame.pack(fill=BOTH,expand=1)
& T6 W, i' Z3 h0 s - ( {" h2 u0 `' `! _) o
- #adding the image
8 P' k$ D1 Z: p3 ]* ]- c - File = askopenfilename(parent=root, initialdir="C:/Users/dell/Desktop/意汉词典/_ok",title='Choose an image.')" F; ~; E+ A9 l* l2 y: O# ?
- img = ImageTk.PhotoImage(Image.open(File))
v! U4 y) W) R& C j( \4 T2 B( ] - canvas.create_image(0,0,image=img,anchor="nw")4 d( S: S! |; c$ P! u* P! R
- canvas.config(scrollregion=canvas.bbox(ALL))) k$ T! t7 v0 E! ]
5 P2 \. k# [! R8 t$ e O% z: B- #function to be called when mouse is clicked
0 }& L, E! F4 ~- ^, N/ L - def printcoords(event):
& K. f/ O& Q$ Z/ K! t! i2 n - #outputting x and y coords to console
9 w* F( n. f: `; k& K- U6 P5 o9 U4 @; B - canvas = event.widget
3 U2 x9 d$ G1 \5 z ] - x = canvas.canvasx(event.x)
" T: j h! U- d2 G! w - y = canvas.canvasy(event.y)2 z" H8 O# R. d- j+ K
- oval = canvas.create_oval(x-8, y-8, x+8, y+8, fill="red")) J& _% ?8 p, K& p7 p: C& j
- print x,y4 C; O( k; j$ ?$ \) H
- f = open('_coords.txt','a')% \( D a: Q2 J0 x9 C& j
- f.write('X: %s\tY: %s\n' % (x,y))
, t7 U, q1 M3 B. z) `% |, `& |1 _ - f.close()- D' c7 [+ g2 B! g1 X, N b
- - X; b x0 M# w- y3 o
- #mouseclick event
) f0 j7 ^0 t/ U' b/ q; x4 M+ O - canvas.bind("<Button 1>",printcoords)* T- x: u$ d- v. Y8 @
- root.mainloop()
, o; q- m) H4 ?- t! M: Y/ Y
复制代码 ( N) t6 W9 @ A! \: P9 X
) H6 [% z# C, }2 Z+ I6 P% {* Q来源处:
) C3 U5 z( ?; m- {http://stackoverflow.com/questio ... ck-coordinate-on-it |
|