How to Stop a Running Function Without Exiting the Tkinter Window Entirely

How to stop a running function without exiting the Tkinter window entirely?

With Tkinter, such things are commonly done using the universal widget after() method. You should generally not use time.sleep() in a Tkinter program because it prevents the mainloop() from running (which is what is making the GUI unresponsive in your code).

A successful after() call will return an integer "cancel id" which can used to stop the callback just scheduled. This is what's needed by the Stop() method of your Example class to stop the method doing the counting.

from Tkinter import *

class Example:
def __init__(self, master):
self.startButton = Button(master,text='Start', command=self.start)
self.startButton.grid(row=0, column=0)

self.stopButton = Button(master, text='Stop', command=self.stop)
self.stopButton.grid(row=0, column=1)

self.textBox = Text(master, bd=2)
self.textBox.grid(row=1, columnspan=2)

def start(self):
self.count = 0
self.cancel_id = None
self.counter()

def counter(self):
self.textBox.delete("1.0", END)
if self.count < 10:
self.count += 1
self.textBox.insert(END, str(self.count)+'\n\n')
self.cancel_id = self.textBox.after(1000, self.counter)

def stop(self):
if self.cancel_id is not None:
self.textBox.after_cancel(self.cancel_id)
self.cancel_id = None

root=Tk()
Example(root)
root.mainloop()

tkinter exit/quit/killing function threading out of mainloop

Make the thread a daemon to have it die when the main thread dies.

def downloadbutton():
t = threading.Thread(target=download)
t.daemon = True
t.start()

For example:

import tkinter as tk
import threading
import time

def download():
while True:
time.sleep(1)
print('tick tock')

def stop(): # stop button to close the gui and should terminate the download function too
root.destroy()

def downloadbutton():
t = threading.Thread(target=download)
t.daemon = True
t.start()

root = tk.Tk()
btn = tk.Button(text = "Start", command=downloadbutton)
btn.pack()
btn = tk.Button(text = "Stop", command=stop)
btn.pack()
root.mainloop()

How do I close a tkinter window?

You should use destroy() to close a tkinter window.

from Tkinter import *

root = Tk()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()

Explanation:

root.quit()

The above line just Bypasses the root.mainloop() i.e root.mainloop() will still be running in background if quit() command is executed.

root.destroy()

While destroy() command vanish out root.mainloop() i.e root.mainloop() stops.

So as you just want to quit the program so you should use root.destroy() as it will it stop the mainloop().

But if you want to run some infinite loop and you don't want to destroy your Tk window and want to execute some code after root.mainloop() line then you should use root.quit(). Ex:

from Tkinter import *
def quit():
global root
root.quit()

root = Tk()
while True:
Button(root, text="Quit", command=quit).pack()
root.mainloop()
#do something

Closing all threads when Tkinter window is closed

Instead of while True: in def writeall(sock): you could use an internal bool variable, for instance while not self._stop:. Then, write in def gui(): simply

self._stop = False
root.mainloop()
self._stop = True


Related Topics



Leave a reply



Submit