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
#1
hello and thank for help given.
I have a goal:
When mousepointer moves,write its coordinates into text field of an allready existing label.
In order to achieve that, I need:
1) a mouse movement event to register movements
2) code to change/config text of an allready existing label

As a nearly beginner in codeing, code should be "stright forward"

re 1: found and understand,this code:
https://stackoverflow.com/questions/2292...on-tkinter
from textholders import *
from tkinter import ttk
from tkinter import *


import pyautogui
import tkinter as tk

window = tk.Tk()
# window.geometry(f"{1920}x{1200}")
window.geometry("1920x1200+0+0")
window.minsize(1920, 1200)
window.resizable(False, False)
window.title("map_view_example.py")
window.state("normal")
window.title("ROOT")
window.config(background="blue")
# hwnd = win32gui.GetForegroundWindow()
# win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
inp_posskjerm = tk.Label(window, text ="original txt", font=("Ariel",9,"bold"), justify=("left"), relief="flat").place(x=115,y=277)


def motion(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))

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



window.mainloop()
I should be able to use all but the formatting.

My,so fare, label control:(I probably use frames instead of place, later on.
inp_posskjerm = tk.Label(window, text ="original txt", font=("Ariel",9,"bold"), justify=("left"), relief="flat").place(x=115,y=277)
The x and y coordinate above should be input to a label configure function.
My 2 questions are:
1) how is this done without creating a new layer, but in the root?
2) I have tried to use configure, its quite convinient using a "helpbutton" to trigger the config. Doing this from an event seemes a bit more difficult,to me.
Which type of configure would You suggest for the above task?

I have read for 4 days without finding any clear answers, mainly howto
configure the text of a label,viewing x and y mouse coordinates, triggered by a moving mouse event.
Any help, hopefully with example code would be greatly appreciated. Thank You.
(I didnt post code in codetags as it probably would break the float of undertanding).
deanhystad write Jul-03-2023, 03:13 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Odd example. print() is python 3, but Tkinter (with a capital T, is python 2).

If you want the label to appear in root, specify root as the parent. I don't know what "window" is.

You should really post your own code, the code you are asking questions about, instead of the code you have based your code upon.

Your second question makes no sense. It would probably make sense if you posted the code that does configuring and has a help button.

I find it very difficult to believe that you spent 4 days searching and have not learned how to set the text in a label. I took me 10 seconds to find the answer googling. "How to set tkinter label text". You already have a function that is triggered by a mouse move event, so you don't need to research that. The function even gets the x and y position of the mouse.

If you still have questions, feel free to post another question, but this time post your own code.
Reply
#3
(Jul-03-2023, 03:29 PM)deanhystad Wrote: Odd example. print() is python 3, but Tkinter (with a capital T, is python 2).

If you want the label to appear in root, specify root as the parent. I don't know what "window" is.

You should really post your own code, the code you are asking questions about, instead of the code you have based your code upon.

Your second question makes no sense. It would probably make sense if you posted the code that does configuring and has a help button.

I find it very difficult to believe that you spent 4 days searching and have not learned how to set the text in a label. I took me 10 seconds to find the answer googling. "How to set tkinter label text". You already have a function that is triggered by a mouse move event, so you don't need to research that. The function even gets the x and y position of the mouse.

If you still have questions, feel free to post another question, but this time post your own code.

Thank You for spending 10 seconds, telling Im a failure. Due to age and illness my brain works slower. But I can ensure You, I didnt even close Your arrogance in front of my pupils teaching them fortran, cobol up to days programming, microcontrollers and others My brain is redusewd but still recognize arrogance. Yoou probably will find my last Parkinson app searhing. I learn by reading others code combined with videos. Wonder how You got so smart?.
Reply
#4
You really took that the wrong way. I'm sorry you were insulted. Not my intention, but it happened. I am sorry.

I want you to post your code that you are asking questions about. That is about all I asked. I cannot really answer your questions until you do that. Once I have the context provided by your actual code, your questions will probably be clear. Or at least clear enough that I can ask reasonable questions.

It really did take 10 seconds to find an answer about how to change the text in a tkinter label. It is probably the most common question about labels. If you can write posts like you have in this thread it is difficult understanding how you could not find that information. Bizarre. The brain is a mystery.

Good Luck.

Thanks for posting your code, but next time do so in a new post instead of changing the original post. It provides a better transcript of the conversation.

Always use python tags when posting code. Indenting is syntax in Python, and indenting is lost when you don't use python tags.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  python311 tkinter moving mouse event. janeik 1 946 Jul-01-2023, 02:00 PM
Last Post: deanhystad
  python move specific files from source to destination including duplicates mg24 3 1,113 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  python move folders and subfolders not working mg24 5 2,194 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  help with python mouse script z4rxxxx 0 1,141 Jan-15-2022, 04:39 PM
Last Post: z4rxxxx
  Move mouse and click in particular position biprabu 3 2,504 Sep-01-2020, 08:23 PM
Last Post: deanhystad
  Change mouse move speed in guibot script rulltartan 1 2,739 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,212 Jan-23-2020, 07:42 PM
Last Post: DeaD_EyE
  mouse 0.7.0 - mouse polling hate 125-1000hz penahuse 1 2,544 Dec-06-2019, 09:51 PM
Last Post: Larz60+
  Python 3+ kybd and mouse control keycodes ineuw 5 2,908 Aug-20-2019, 07:51 AM
Last Post: ineuw
  How can I move in an external program via Python? Pythoner 0 2,283 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