Python Forum
Howto do motion event on solely window and not the widgets on it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Howto do motion event on solely window and not the widgets on it?
#3
Thanks for th info. As seeen from attached image, the map aare and several widtges (controls). All on root window, named window.

Map,https://github.com/TomSchimansky/TkinterMapView,

The map returns longitude/latitude coords when clicked, so I suppose I cant add a transparent window to hold the screen mouse movemennt event handler to avoid that controls (widtges) resets the coordinates.
I would like event report back coordinates untouced by controls.

I am thinking of adding values to reported x and y coordinates when mousepointer reach the map area, but that involves some nasty comparements :)

Ill look into your postof today tomorrow. thanks.



(Jul-10-2023, 06:03 PM)deanhystad Wrote: Are you sure this is something you want? I think I remember one of your earlier posts that included a map. Are your trying to find the mouse location in the map window? If so, the easiest way to do this is bind motion for the map only.

In this example I only bind potion events for the "position" label. Move the mouse inside the "position" label and it reports the mouse position relative to the label. Move the mouse outside the label and the motion() function is not called. I find this to be a most useful behavior since widgets generally don't care about mouse events that happen somewhere else.

The window does not need focus for the motion event to run. To verify I added an entry widget where you can type text. Press the Enter key and the text is copied to the position label. Now move the cursor over the position label and you will see the mouse position displayed. You can still type, so the entry has retained focus.
import tkinter as tk

window = tk.Tk()

def copy_entry(event):
    print(event)
    position["text"] = entry.get()

def motion(event):
    position["text"] = f"{event.x}, {event.y}"

entry = tk.Entry(window, width=10, font=("Ariel", 24, "bold"))
entry.pack(padx=50, pady=20)
entry.bind("<Return>", copy_entry)

position = tk.Label(
    window, width=10, font=("Ariel", 60, "bold")
)  # Save label object in variable.
position.pack(padx=50, pady=50)
position.bind("<Motion>", motion)

window.mainloop()
And if you really want to get the position of the mouse in the root window, you can ask for it.
import tkinter as tk

window = tk.Tk()


def copy_entry(event):
    print(event)
    position["text"] = entry.get()


def motion(event):
    x, y = window.winfo_pointerxy()  # Ignore the event info.  Ask for the cursor position.
    position["text"] = f"{x}, {y}"


entry = tk.Entry(window, width=10, font=("Ariel", 24, "bold"))
entry.pack(padx=50, pady=20)
entry.bind("<Return>", copy_entry)


position = tk.Label(
    window, width=10, font=("Ariel", 60, "bold")
)  # Save label object in variable.
position.pack(padx=50, pady=50)
window.bind("<Motion>", motion)

window.mainloop()

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
RE: Howto do motion event on solely window and not the widgets on it? - by janeik - Jul-10-2023, 09:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  motion tracking script ig? k4ne 0 513 Dec-13-2023, 02:00 AM
Last Post: k4ne
  howto get size of a ctk image? janeik 2 1,002 Oct-03-2023, 03:49 AM
Last Post: janeik
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 2,099 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,256 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Creating Data Set for Changing Oscillation Motion jcraw77 0 1,639 Jun-19-2020, 12:25 AM
Last Post: jcraw77
  Detect event by motion/image - Help Zarqua 0 1,575 May-12-2020, 10:41 AM
Last Post: Zarqua
  Detection Motion Program Adek1243 11 4,493 Mar-23-2020, 10:12 AM
Last Post: Adek1243
  Simple python script for pir motion sensor Kimzer 0 4,137 Oct-03-2017, 05:36 PM
Last Post: Kimzer
  Help with circle and motion wigigx 4 4,148 Aug-29-2017, 09:49 PM
Last Post: sparkz_alot
  Howto try catch consuli 2 3,985 Nov-14-2016, 08:03 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020