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 108 109 110 111 112 113 114 115 116 117
|
"""
On a ici une image dans un ScrolledText
L'image est plus grande que le widget
J'ai pas tout compris sur les coordonnées, mais pour pouvoir pointer
sur des coordonnées correctes, il faut tenir compte
- de la taille de l'image
- la position du scroll
- la position de la souris
çà marche pour les mouvements souris, mais pour les dessins (clic),
il faut déduire des constantes sur x, y
"""
from tkinter import *
import tkinter.ttk
from turtle import *
import os
can1= 0
def quitter():
fen1.destroy()
fen1.quit()
def chargeimage():
global photo, can1
i= filedialog.FileDialog(fen1)
fname= i.go(dir_or_file=os.curdir, pattern= '*.gif')
try:
photo= PhotoImage(file= fname)
except:
return
print(can1, type(can1))
if type(can1) != int:
can1.destroy()
can1= ScrolledCanvas(framecanvas,\
width= photo.width()/2, height= photo.height()/2,\
canvwidth=photo.width(), canvheight=photo.height())
can1.create_image(0, 0, image= photo)
can1.grid(row= 1)
can1.bind('<Motion>', mouvement)
can1.bind('<ButtonPress-1>', clic)
def MenuBar():
filemenu= Menubutton(framemenu, text= 'Fichier')
filemenu.pack(side= LEFT, padx= 5)
menu1= Menu(filemenu)
menu1.add_command(label= 'Carte')
menu1.add_command(label= 'Charger', underline= 0,
command= chargeimage)
menu1.add_separator()
menu1.add_command(label= 'Quitter', underline= 0,
command= quitter)
filemenu.configure(menu= menu1)
fen1= Tk()
fen1.title('Essai ScrolledCanvas')
framemenu = Frame(fen1, bg="green", width=500, height=50)
framemenu.pack(side=TOP, fill=NONE)
MenuBar()
framemenu.pack()
framebouton= Frame(fen1, bg= "blue", width= 500, height= 50)
framebouton.pack(side= BOTTOM, fill= NONE)
#""""""""""""""""""""
x3= StringVar()
y3= StringVar()
x3.set('abcd')
y3.set('efgh')
lab= Label(framebouton, text= 'XCoord:').grid(row= 0, column= 0)
lab= Label(framebouton, text= 'YCoord:').grid(row= 0, column= 2)
entr5= Entry(framebouton, textvariable= x3)
entr6= Entry(framebouton, textvariable= y3)
entr5.grid(row= 0, column= 1)
entr6.grid(row= 0, column= 3)
framebouton.pack()
#""""""""""""""""""""
xclic= 0
yclic= 0
def evalX(X):
return int((X + can1.xview()[0] * float(photo.width())))
def evalY(Y):
return int((Y + can1.yview()[0] * float(photo.height())))
#correction pour une image 1702 X 1353
corrige_x= -856
corrige_y= -680
def clic(event):
global xclic, yclic
xclic= int(x3.get())
x= xclic + corrige_x #d'où on sort çà ?????
yclic= int(y3.get())
y= yclic + corrige_y # çà doit dépendre de la taille de l'image
can1.create_oval(x- 5, y-5, x+5, y+5,\
outline= 'red', width= 2)
def mouvement(event):
x3.set(evalX(event.x))
y3.set(evalY(event.y))
framecanvas= Frame(fen1, width= 500, height= 400)
framecanvas.pack(side= TOP, fill= NONE, expand= 1)
chargeimage() #('Aube8.gif')
fen1.mainloop() |
Partager