Home > OS >  Tkinter problem changing the tkinter values from a while loop using Tk.after()
Tkinter problem changing the tkinter values from a while loop using Tk.after()

Time:10-04

I was using threads for lookforq() but then I got RuntimeError: main thread is not in main loop. Then I found the function root.after() but using this I couldn't change anything on the gui which I could with threads. I hope I could express my problem.

I am trying to adjust the buttons and change the text in the label with keyboard inputs.

Here is my code:

from tkinter import *
import pyautogui
import keyboard as kb
import sys
import pyperclip
import time


sc_x = str(int((list(pyautogui.size())[0]/4)))
sc_y = str(int((list(pyautogui.size())[1]/4)))


def findloc():
    ls = list(pyautogui.position())
    return f"x={ls[0]}, y={ls[1]}"

def stopper(xloc):
    print("asd1")
    finalloc = xloc
    lbl.config(text = loca)
    print("lbl",lbl["text"])

def copy(t):
    pyperclip.copy(t)

def k():
    window.destroy()
    sys.exit()

def started():
    global loca

    btn2.config(state=DISABLED)
    while True:
        if kb.is_pressed("e"):
            btn2.config(state=NORMAL)
            stopper(loca)
            break
        loca = findloc()
        print(loca)


window=Tk()

btn=Button(window, text="W= Copy", command=lambda: copy(lbl["text"]), font=("Helvetica", 12))
btn.place(x=150, y=50)

btn2=Button(window, command=started ,text="Q= Start", font=("Helvetica", 12))
btn2.place(x=50, y=50)


btn3=Button(window, text="E= Stop", font=("Helvetica", 12))
btn3.place(x=250, y=50)

btn4=Button(window, text="K= Close", command=k, font=("Helvetica", 12))
btn4.place(x=250, y=250)

loc = str(findloc())
lbl=Label(window, text=findloc(), fg='black', font=("Helvetica", 16))
lbl.place(x=116, y=125)


def lookforq():
    while True:
        if kb.is_pressed("q"):
            started()
        elif kb.is_pressed("k"):
            k()
            break
        elif kb.is_pressed("w"):
            btn.config(state=DISABLED)
            copy(lbl["text"])
            time.sleep(0.4)
            btn.config(state=ACTIVE)



if __name__ == "__main__":
    window.title('Mouse Location Printer')
    window.geometry(f"375x300 {sc_x} {sc_y}")
    window.after(1000, lookforq)
    window.mainloop()

CodePudding user response:

You can assign a tkinter.StringVar() to store a string, then use that variable as your button's textvariable parameter. Then your button's text will be updated whenever you write to the variable. You'll probably want a function to handle updating multiple labels. I hope this helps!

button_label = StringVar('W= Copy')  # the default string is optional
btn = Button(
    window,
    textvariable=button_label,  # bind your StringVar to this button
    command=lambda: copy(lbl["text"]),
    font=("Helvetica", 12)
)

CodePudding user response:

I could get the code to update the GUI by adding window.update() after every update on the GUI. It is now working perfectly.

  • Related