Jul-03-2023, 05:30 PM
(This post was last modified: Jul-03-2023, 05:30 PM by deanhystad.)
In your recent post, what is the pyautogui for?
Do not do this:
This is a problem in your current code:
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.
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()