Python Forum
python311 tkinter moving mouse event.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python311 tkinter moving mouse event.
#1
hi.I am running a tkinter mapview in root window.
Coordinate,pos_x, is shown on a label in full size rootwindow.
However when I move mouse from i.e pos_x= 10 to 9, label shows 90.
I have tried config, giving input of "", but seemes not to work.
I would appreciate any hints howto view the mouse coordinate not to "add" to next coordinate when length of screen distances decreases.
Below is some code involved. Image attached shows result moving mouse from a 4 ciffer posit to a 3 ciffer screen position (The 4th ciffer adding to 3ciffer position). [Image: DC0LLCB]
import tkintermapview
from textholders import *
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *

window = tkinter.Tk()
window.geometry("1920x1200+0+0")
window.minsize(1920, 1200)
window.resizable(False, False)
window.state("normal")
window.title("ROOT")
window.config(background="blue")



map_widget = tkintermapview.TkinterMapView(window, width=1000, height=1000, corner_radius=0)
map_widget.place(x=450, y=100)

def motion(event):
    global pos_x, pos_y
    pos_x = event.x_root
    pos_y = event.y_root

    inp_posskjerm = ttk.Label(window, font=("Ariel",16,"bold"), text=(pos_x))
    
    inp_posskjerm.place(x=115,y=277)
window.bind("<Motion>", motion)


window.mainloop()   

Attached Files

Thumbnail(s)
   
Reply
#2
You should not make a new label to change the mouse position display. Instead, change the text value of the existing label. In this example I use a StringVar, but it would work the same if I set the "text" attribute of the label.
import tkinter as tk  # Don't use wildcard imports


# Learn about classes.  Works really well for GUI code.
class MyWindow(tk.Tk):
    """ A window that displays the mouse position. """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.mouse_pos = tk.StringVar(self, 0)
        tk.Label(self, textvariable=self.mouse_pos, font=(None, 100), width=10).pack()
        self.bind("<Motion>", self.motion) 

    def motion(self, event):
        """ Method called when mouse moves in window. """
        self.mouse_pos.set(f"{event.x}, {event.y}")


MyWindow().mainloop()
Adding widgets to a window after it is drawn is rarely done.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mouse move event/cinfiguration ttk/python janeik 4 1,078 Jul-03-2023, 05:30 PM
Last Post: deanhystad
  How to hardcode event.state? (tkinter) philipbergwerf 1 1,629 Oct-24-2022, 01:55 PM
Last Post: deanhystad
  Moving mouse so that games can detect movement SheeppOSU 2 1,959 Jun-09-2020, 07:23 PM
Last Post: SheeppOSU
  mouse 0.7.0 - mouse polling hate 125-1000hz penahuse 1 2,541 Dec-06-2019, 09:51 PM
Last Post: Larz60+
  Moving Mouse ian 1 2,360 Dec-25-2018, 10:41 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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