Seite 1 von 1

Event: event.x_root event.y_root

Verfasst: Samstag 28. Februar 2004, 17:37
von Pythonaya
Hallo @ all!

Ich habe in kleines Problem mit einem Event.

Das Event soll sich auf ein Canvas cv beziehen und wird mit Button-1 ausgelöst.
In der Prozedur für das Event möchte ich die x und y Koordinaten des Mauszeigers gemessen von den Koordinaten links oben (0.0) im Canvas haben.

Benutze ich also event.x_root und event.y_root, so bekomme ja nur die x und y Koordinaten von der linken oberen Ecke des Fenster.

Ich hoffe ihr könnt mir helfen,
Flo

Verfasst: Samstag 28. Februar 2004, 19:48
von Milan
Hi. Die Lösung ist bestimmt eine, wo man sich mit der Hand vor die Stirn schlägt :wink: "root" sagt ja schon relativ zum absoluten Master. Deswegen reicht ja auch event.x bzw event.y :lol: .

Gruß, Milan

Verfasst: Samstag 28. Februar 2004, 20:15
von wuf
Hi Pythonaya

Hier ein Beispiel für Dein Vorhaben

Code: Alles auswählen

from Tkinter import*

def ShowCoordinates(event):
	#~~ Schreibt die X und Y-Position auf die Canvas
	global dspxpos,dspypos

	xpos = event.x
	ypos = event.y

	canvas.delete(dspxpos)
	dspxpos = canvas.create_text(20,
								20,
								text="X-Position = " + str(xpos),
								font=font1,
								fill="blue",
								anchor="w")

	canvas.delete(dspypos)
	dspypos = canvas.create_text(20,
								40,
								text="Y-Position = " + str(ypos),
								font=font1,
								fill="blue",
								anchor="w")

root = Tk()

font1 = ('Helvetica', 15, 'bold')
dspxpos = None
dspypos = None

canvas = Canvas(root,width=200,height=100,bd=5,relief='raised',bg='yellow')
canvas.pack()
canvas.bind('<Button-1>', ShowCoordinates)

root.mainloop()
Gruss wuf :wink:

Re:

Verfasst: Sonntag 29. Februar 2004, 14:23
von Pythonaya
Oh, ja, also das mit event.x ... kannte ich noch nicht.
Danke für die schnelle Antwort.



MFG,
Flo