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