Python Forum
mouse move event/cinfiguration ttk/python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mouse move event/cinfiguration ttk/python
#5
In your recent post, what is the pyautogui for?

Do not do this:
from tkinter import *
I know you will see this a lot, and the ttk library was even designed to make using ttk as simple as adding.
from tkinter.ttk import *
But wildcard imports (import *) should be avoided because they clog the "namespace". If, in your program, you used "TOP" (it is an attribute commonly used when packing tkinter widgets), where did that come from? If there is no error, it must be defined somewhere, but where? Is it local? Using module names eliminates this confustion. I know that tk.Label is a Label from something named "tk". When I look for "tk" I see at the top of the module that you have bound that name to the "tkinter" module (import tkinter as tk).

This is a problem in your current code:
inp_posskjerm = tk.Label(window, text ="original txt", font=("Ariel",9,"bold"), justify=("left"), relief="flat").place(x=115,y=277)
You need to be careful when daisy chaining commands like this. tk.Label() returns a label object, but .place() returns None. When you call "x = tk.Label().place()", x is assigned None, not the Label object. You have no way to change the Label's text because you have no handle to the label.

This is a working example of your code. I stripped out anything not needed to demonstrate how to make the bound function update the label text.
import tkinter as tk

window = tk.Tk()

inp_posskjerm = tk.Label(window, width=10, font=("Ariel", 60, "bold"))  # Save label object in variable.
inp_posskjerm.pack()

def motion(event):
    inp_posskjerm["text"] = f"{event.x}, {event.y}"
    # or inp_posskjerm.configure(text=f"{event.x}, {event.y}")

window.bind("<Motion>", motion)

window.mainloop()
You could also set the textvariable attribute of your Label object and indirectly set the label text like this.
import tkinter as tk

window = tk.Tk()

# inp_posskjerm is not a label.  It is the text part of the label.
inp_posskjerm = tk.StringVar(window, "Hello")
tk.Label(
    window, textvariable=inp_posskjerm, width=10, font=("Ariel", 60, "bold")
).pack()  # Can daisy chain now since I don't need the label object.


def motion(event):
    inp_posskjerm.set(f"{event.x}, {event.y}")

window.bind("<Motion>", motion)

window.mainloop()
Reply


Messages In This Thread
RE: mouse move event/cinfiguration ttk/python - by deanhystad - Jul-03-2023, 05:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python311 tkinter moving mouse event. janeik 1 996 Jul-01-2023, 02:00 PM
Last Post: deanhystad
  python move specific files from source to destination including duplicates mg24 3 1,140 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  python move folders and subfolders not working mg24 5 2,270 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  help with python mouse script z4rxxxx 0 1,159 Jan-15-2022, 04:39 PM
Last Post: z4rxxxx
  Move mouse and click in particular position biprabu 3 2,538 Sep-01-2020, 08:23 PM
Last Post: deanhystad
  Change mouse move speed in guibot script rulltartan 1 2,764 Mar-30-2020, 01:51 PM
Last Post: pevogam
  how difficult its to move to Python 3.8 from Python 2.7 dkirandasari 4 3,241 Jan-23-2020, 07:42 PM
Last Post: DeaD_EyE
  mouse 0.7.0 - mouse polling hate 125-1000hz penahuse 1 2,561 Dec-06-2019, 09:51 PM
Last Post: Larz60+
  Python 3+ kybd and mouse control keycodes ineuw 5 2,965 Aug-20-2019, 07:51 AM
Last Post: ineuw
  How can I move in an external program via Python? Pythoner 0 2,291 Dec-29-2018, 12:43 PM
Last Post: Pythoner

Forum Jump:

User Panel Messages

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