1

I am facing a problem in Tkinter python. my code is below:

cycles = 0
def send_it():
    now = datetime.now()
    print(now.strftime("%Y-%m-%d %H:%M:%S"))
    time.sleep(1)

def send_me():
    with keyboard.Listener(on_press=on_press) as listener:
            global cycles
            if(radio_var.get() == 1):
                if cycles <= 3:
                    send_it()
                    cycles = cycles+1
                elif(cycles == 4):
                    print("Done!")
                    break
            elif(radio_var.get() == 2):
                    print("No!")
        listener.join()

def order_schedule():
    #Scheduling order send time
    schedule.every().day.at("20:00:00).do(send_me)
    while True:
        schedule.run_pending()
        time.sleep(2) 

the problem is that although my job(send_me) which is scheduled to be done every day at 20:00:00, stops when cycles == 4, but after that Tkinter will be locked and I can't do anything for that. it took me many hours to figure out how to solve it but wasn't lucky.

I tried many things like return schedule.CancleJobs and schedule.clear(), also I tried to use threading but I was not able to do what I want.

hope you guys can help me get to the answer.

Thanks a lot

bghad1
  • 89
  • 8
  • 2
    My suspicion is the problem lies with time.sleep(2). time.sleep blocks the thread for a certain amount of time. If tkinter is also running in the same thread, it will be blocked by the time.sleep and cannot update. see https://stackoverflow.com/q/10393886/1275942 for info and possible solutions. – Kaia Aug 27 '20 at 18:32
  • thanks for your reply. if the problem is with time.sleep(2), Tkinter shouldn't freeze only for 2 seconds and after that be clickable and interactive? – bghad1 Aug 27 '20 at 18:43
  • 2
    Well, you have it in a while True loop, so it'll time.sleep(), briefly check the schedule, and then sleep again. It'll never go back to the main loop of Tkinter (the tk.mainloop() function, which presumably order_schedule gets called from) – Kaia Aug 27 '20 at 18:47
  • Does it print "Done!" ? I see `break` when cycles=4. What are you breaking from ? – Mike67 Aug 27 '20 at 18:48
  • @Mike67 actually I wrote it just for making the code here more readable(although it does not do any special thing and that is just for breaking repetition of the scheduled job. – bghad1 Aug 27 '20 at 19:15
  • @Keon as you said about the true while, I commented that line and started the project again, but nothing different happened. same freezing. – bghad1 Aug 27 '20 at 19:17
  • 2
    If you put a print statement inside the while loop, you'll probably see it print something out every two seconds. As a general rule of thumb you should never use while like this in a GUI. – Bryan Oakley Aug 27 '20 at 21:06
  • 1
    I would agree with @Bryan. while loops are not recommended in the GUI 'cause mainloop(). and even if you have to use a while loop, it should not be **while True* cause most of the times it stops the GUI from reaching mainloop(). – P S Solanki Aug 28 '20 at 07:19
  • @BryanOakleythanks for your reply. so what should I do now? cause schedule usage is like this.(mentioned here: [link](https://schedule.readthedocs.io/en/stable/index.html) ) – bghad1 Aug 28 '20 at 08:10
  • @PSSolanki thanks for your reply. what should I do now? as i want to start the `send_me ` at a certain time, I'm using schedule usage . (mentioned here: [link](https://schedule.readthedocs.io/en/stable/index.html) ) – bghad1 Aug 28 '20 at 08:11
  • I replaced `break` with `tk.mainloop()` and everything seems okay(although i have another `tk.mainloop()` at the end of my codes-which I think it won't ever start there- ). the program is working like a charm and doing what I want. Is it okay to do that? or it will have disadvantages? am I doing wrong? – bghad1 Aug 28 '20 at 09:06

0 Answers0