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?
#1
I have a single window (root):
window = customtkinter.CTk() which holds several widges as (mapview,labels,combos....).

I use this function to get mouse x and y position, while mouse is moving:

It shows coordinates, but at every widget it resets x,y values. Intention was to treat window as a whole, without resetting coordinates when mousepointer hits a widget.

Widges uses window as first parameter.

Whats the approach for the above?
(think I read that windows needs focus for events to run, so thats not an option(?)

def motion(event):
    inp_posskjermx["text"] = f"{event.x}"
    inp_posskjermy["text"] = f"{event.y}"
    inp_posskjermx.configure(text=f"{event.x}") 
    inp_posskjermy.configure(text=f"{event.y}")
    
window.bind('<Motion>', motion)
Reply
#2
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()
Reply
#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
#4
I think it will work if you bind the map.canvas motion events.
import tkinter as tk
from tkintermapview import TkinterMapView
 
 
class MyWindow(tk.Tk):
    """A window that selects one of several pages to display."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.map = TkinterMapView(self, width=800, height=600, corner_radius=0)
        self.map.pack(expand=True, fill=tk.BOTH)
        self.map.set_position(60.03345, 11.35806)  # Norway
        self.map.set_address("fjellvegen 4, auli, norway", marker=True)
        self.map.set_zoom(16)
        self.map.canvas.bind("<Motion>", self.motion)  # map.canvas is the window that holds the map.
        self.coordinates = tk.Label(self, width=10, font=(None, 24))
        self.coordinates.pack(fill=tk.X, padx=5, pady=5)

    def motion(self, event):
        self.coordinates.configure(text=f"{event.x}, {event.y}")
 

MyWindow().mainloop()
Unless you need to reference something in another post, use the "New Reply" button instead of the "Reply" button. If we always use the "Replay" button it doesn't take long for posts with replies to replies to replies to replies to become unreadable.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  motion tracking script ig? k4ne 0 480 Dec-13-2023, 02:00 AM
Last Post: k4ne
  howto get size of a ctk image? janeik 2 890 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 1,807 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,188 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Creating Data Set for Changing Oscillation Motion jcraw77 0 1,598 Jun-19-2020, 12:25 AM
Last Post: jcraw77
  Detect event by motion/image - Help Zarqua 0 1,535 May-12-2020, 10:41 AM
Last Post: Zarqua
  Detection Motion Program Adek1243 11 4,381 Mar-23-2020, 10:12 AM
Last Post: Adek1243
  Simple python script for pir motion sensor Kimzer 0 4,088 Oct-03-2017, 05:36 PM
Last Post: Kimzer
  Help with circle and motion wigigx 4 4,069 Aug-29-2017, 09:49 PM
Last Post: sparkz_alot
  Howto try catch consuli 2 3,905 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