0

So what i'm basically trying to do is that when i click button one i want the program to print go once every second until button two is clicked but what happens is that when i run the code it just prints go even though i haven't clicked button one. i'm trying to do this as i want to do a IoT project with my esp32 where i get my internet speed once every 15 seconds and send the data to my esp 32 and the program will do it until i/the user will give an input/button click for it to stop so if you guys know a better way i could do this it would be appreciated

import time
import tkinter as tk
user_input=""
def onclick1():
    while user_input!="stop":
        print("go")
        time.sleep(1)
def onclick2():
    user_input="stop"



root=tk.Tk()
root.title("BUTTON_TEST")

btn1=tk.Button(root,text="Button 1",command=onclick2())
btn2=tk.Button(root,text="Button 2",command=onclick1())

btn1.pack()
btn2.pack()
root.mainloop()

1 Answers1

0

The onclick1() function will bolck the main loop of the window.You need to create a thread and do the work in that thread. Or you can use an executor in module concurrent.futures.

lhgzbxhz
  • 25
  • 2