0
from tkinter import *
import tkinter as tk
from multiprocessing import Process
import time



def notify(text):
    notify_heading_text.set(f"App : {text}")
    
    
def on_restart():
    global callId
    callId = root.after(100,print_hi)

def on_stop():
    print("Stopping")
    global callId
    if callId is not None:
        root.after_cancel(callId)
        callId = None


def print_hi():
    print("Task is begining")
    time.sleep(10)
    print("task over")


if __name__ == '__main__':
    root = Tk()

    # root = Tk()
    root.geometry('400x400')
    root.resizable(0, 0)
    global notify_heading_text
    fact = "tree is infact intact"
    notify_heading_text = tk.StringVar()

    notification1 = Label(height=1, textvariable=notify_heading_text, bd=0)
    notification1.pack(fill=X, side=TOP)
    notification1.configure(bg='white', fg='black', font=('Helvetica', 12, 'bold'), pady=10)
    bind2 = tk.StringVar()
    bind3 = tk.StringVar()
    b2 = Button(root, borderwidth=0, fg="white", activebackground='#40556a', activeforeground='white', bg="black",
                textvariable=bind2, command=on_stop, font=('Helvetica', 12, 'bold'), padx=10, pady=6)
    bind2.set('Stop')
    b2.pack(side=LEFT)
    b3 = Button(root, borderwidth=0, fg="white", activebackground='#40556a', activeforeground='white', bg="black",
                textvariable=bind3, command=on_restart, font=('Helvetica', 12, 'bold'), padx=10, pady=6)
    bind3.set('Restart')
    b3.pack(side=RIGHT)

    root.mainloop()

As I run the above program and press the 'Restart' button on_restart() method is called which with help of Tkinter's root.after() method, in turn, calls the print_hi() method. The problem is I can't stop the program when print_hi() is still in execution(i.e in this case it is sleeping) and the stop button is jammed and gets only executed when print_hi() method gets complete. The stop button does not work and only works after print_hi() method gets completed. The question is how to stop the program with the stop button. I used thread in place of the after method but was not able to update text inside label it, issue:Not able to update text in tkinter python

Pranav HS
  • 53
  • 7
  • 3
    The `time.sleep(10)` is blocking `tkinter` from updating itself so the window becomes unresponsive. That is why you shouldn't have `sleep` (or any other commands that take a lot of time) in the same thread as the `tkinter` `mainloop` – TheLizzard Apr 27 '21 at 15:18
  • The `after_cancel` only works if the function hasn't been started yet and is till waiting to be executed. The only solution to your problem is to move the `time.sleep`/other long processes in another thread. Be careful as `tkinter` doesn't like it when you call its methods from other threads. – TheLizzard Apr 27 '21 at 15:20
  • 1
    I can't use the thread as mentioned in the question, please see that link in the last part of the question(In brief thread limits GUI updating). So now if I use thread I get the 'stop' function back but not the 'text updating' function in GUI but if I use the after method I get the 'text updating' feature but cannot use the 'stop function'.Please help – Pranav HS Apr 27 '21 at 15:35
  • You could use `.after()` method for waiting and it won't interfere with tkinter's mainloop – Matiiss Apr 27 '21 at 16:15
  • 1
    can you elaborate, please – Pranav HS Apr 27 '21 at 16:18
  • 2
    @Matiiss my guess is that OP will put code that runs for a long time there and the `time.sleep` is just there to mimic the behaviour of the full function. When you see `time.sleep` don't assume that it's just sleeping. – TheLizzard Apr 27 '21 at 17:03
  • 1
    @TheLizzard you are right. – Pranav HS Apr 27 '21 at 17:13

0 Answers0