Threading with tkinter - How to terminate script from button press while function is running?2019 Community Moderator ElectionHow do I run a simple bit of code in a new thread?How do I update the GUI from another thread?How to run a Runnable thread in Android at defined intervals?how to get the return value from a thread in python?How to pass arguments to a Button command in Tkinter?Running code in main thread from another threadtkinter button press to function callTerminate the Thread by using button in TkinterPython run script when Tkinter button pressedHow to stop multiple threads with Tkinter button

Would this string work as string?

Why didn’t Eve recognize the little cockroach as a living organism?

What is the tangent at a sharp point on a curve?

Justification failure in beamer enumerate list

Norwegian Refugee travel document

Isn't the word "experience" wrongly used in this context?

Emojional cryptic crossword

How to find the largest number(s) in a list of elements, possibly non-unique?

UK Tourist Visa- Enquiry

How do you justify more code being written by following clean code practices?

Homology of the fiber

Fair way to split coins

How to read string as hex number in bash?

Should a narrator ever describe things based on a characters view instead of fact?

When should a starting writer get his own webpage?

Unfrosted light bulb

Can other pieces capture a threatening piece and prevent a checkmate?

What (if any) is the reason to buy in small local stores?

What will the Frenchman say?

Interior of Set Notation

Jem'Hadar, something strange about their life expectancy

Single word to change groups

Why is participating in the European Parliamentary elections used as a threat?

What are the rules for concealing thieves' tools (or items in general)?



Threading with tkinter - How to terminate script from button press while function is running?



2019 Community Moderator ElectionHow do I run a simple bit of code in a new thread?How do I update the GUI from another thread?How to run a Runnable thread in Android at defined intervals?how to get the return value from a thread in python?How to pass arguments to a Button command in Tkinter?Running code in main thread from another threadtkinter button press to function callTerminate the Thread by using button in TkinterPython run script when Tkinter button pressedHow to stop multiple threads with Tkinter button










0















I'm in Python37, on windows 10, working in tkinter and I've written a function did_file_update that is constantly scanning the desktop every 10 seconds to see if the file "test.txt" has changed. There is also a function abort_window_call that creates a window with a red "ABORT" button. I'm trying to write a script that will thread the two functions, and allow me to click the "ABORT" button and terminate the script once the "START" button has been pressed and did_file_update has been called.



import os
import time
import threading
import tkinter as tk
from tkinter import messagebox
import tkinter.messagebox as msg
from tkinter import Button, Message, Tk


global path_to_watch
path_to_watch = "C:\Users\Desktop"

class App():
def __init__(self):
pass

def abort_window_call(self):
abort_window = Tk()
abort_window.title("Would you like to abort?")
abort_window.geometry('50x90+200+200')
abort_button = tk.Button(abort_window, text='ABORT', font=('century gothic', 24),
bg='red', fg='white', height=1, width=5,
relief='raised', command=lambda: abort_window.destroy())
abort_button.pack(padx=10, pady=5)

abort_window.mainloop()

def did_file_update(self):
statinfo_before = os.stat("C:\Users\Desktop\test.txt")
statinfo_size_before = statinfo_before.st_size

while 1:
time.sleep (10)
statinfo_after = os.stat("C:\Users\Desktop\test.txt")
statinfo_size_after = statinfo_after.st_size
if statinfo_size_after != statinfo_size_before:
print("Updated: test.txt")
statinfo_size_before = statinfo_size_after
break
else:
pass

def main(self):
master = Tk()
ws = master.winfo_screenwidth()
hs = master.winfo_screenheight()
x = (ws/2) - (180)
y = (hs/2) - (70)
master.title("File Update")
master.geometry('360x140+%d+%d' % (x, y))
msg = Message(master, text = "FILE UPDATE", width=700)
msg.config(font=('century gothic', 28))
msg.grid(row=3, column=0)

start_button = tk.Button(master, text='START', font=('century gothic', 24),
bg='green', fg='white', height=1, width=17,
relief='raised', command=self.did_file_update)
start_button.grid(row=1, column=0, sticky='n', padx=10, pady=5)

master.mainloop()

app=App()

t = threading.Thread(target = app.abort_window_call)
t1 = threading.Thread(target = app.main)

t.start()
t1.start()


tl;dr How do I make the abort_window_call function terminate the whole program, even after "START" has been pressed and the function did_file_update is running?










share|improve this question






















  • You can force the application to terminate by executing sys.exit() in the lambda function of abort_button, but it is not a good way. Also you don't need to create thread for main(), just call it directly. Using multiple Tk() is not recommended, use Toplevel() in abort_window_call().

    – acw1668
    Mar 6 at 23:56















0















I'm in Python37, on windows 10, working in tkinter and I've written a function did_file_update that is constantly scanning the desktop every 10 seconds to see if the file "test.txt" has changed. There is also a function abort_window_call that creates a window with a red "ABORT" button. I'm trying to write a script that will thread the two functions, and allow me to click the "ABORT" button and terminate the script once the "START" button has been pressed and did_file_update has been called.



import os
import time
import threading
import tkinter as tk
from tkinter import messagebox
import tkinter.messagebox as msg
from tkinter import Button, Message, Tk


global path_to_watch
path_to_watch = "C:\Users\Desktop"

class App():
def __init__(self):
pass

def abort_window_call(self):
abort_window = Tk()
abort_window.title("Would you like to abort?")
abort_window.geometry('50x90+200+200')
abort_button = tk.Button(abort_window, text='ABORT', font=('century gothic', 24),
bg='red', fg='white', height=1, width=5,
relief='raised', command=lambda: abort_window.destroy())
abort_button.pack(padx=10, pady=5)

abort_window.mainloop()

def did_file_update(self):
statinfo_before = os.stat("C:\Users\Desktop\test.txt")
statinfo_size_before = statinfo_before.st_size

while 1:
time.sleep (10)
statinfo_after = os.stat("C:\Users\Desktop\test.txt")
statinfo_size_after = statinfo_after.st_size
if statinfo_size_after != statinfo_size_before:
print("Updated: test.txt")
statinfo_size_before = statinfo_size_after
break
else:
pass

def main(self):
master = Tk()
ws = master.winfo_screenwidth()
hs = master.winfo_screenheight()
x = (ws/2) - (180)
y = (hs/2) - (70)
master.title("File Update")
master.geometry('360x140+%d+%d' % (x, y))
msg = Message(master, text = "FILE UPDATE", width=700)
msg.config(font=('century gothic', 28))
msg.grid(row=3, column=0)

start_button = tk.Button(master, text='START', font=('century gothic', 24),
bg='green', fg='white', height=1, width=17,
relief='raised', command=self.did_file_update)
start_button.grid(row=1, column=0, sticky='n', padx=10, pady=5)

master.mainloop()

app=App()

t = threading.Thread(target = app.abort_window_call)
t1 = threading.Thread(target = app.main)

t.start()
t1.start()


tl;dr How do I make the abort_window_call function terminate the whole program, even after "START" has been pressed and the function did_file_update is running?










share|improve this question






















  • You can force the application to terminate by executing sys.exit() in the lambda function of abort_button, but it is not a good way. Also you don't need to create thread for main(), just call it directly. Using multiple Tk() is not recommended, use Toplevel() in abort_window_call().

    – acw1668
    Mar 6 at 23:56













0












0








0








I'm in Python37, on windows 10, working in tkinter and I've written a function did_file_update that is constantly scanning the desktop every 10 seconds to see if the file "test.txt" has changed. There is also a function abort_window_call that creates a window with a red "ABORT" button. I'm trying to write a script that will thread the two functions, and allow me to click the "ABORT" button and terminate the script once the "START" button has been pressed and did_file_update has been called.



import os
import time
import threading
import tkinter as tk
from tkinter import messagebox
import tkinter.messagebox as msg
from tkinter import Button, Message, Tk


global path_to_watch
path_to_watch = "C:\Users\Desktop"

class App():
def __init__(self):
pass

def abort_window_call(self):
abort_window = Tk()
abort_window.title("Would you like to abort?")
abort_window.geometry('50x90+200+200')
abort_button = tk.Button(abort_window, text='ABORT', font=('century gothic', 24),
bg='red', fg='white', height=1, width=5,
relief='raised', command=lambda: abort_window.destroy())
abort_button.pack(padx=10, pady=5)

abort_window.mainloop()

def did_file_update(self):
statinfo_before = os.stat("C:\Users\Desktop\test.txt")
statinfo_size_before = statinfo_before.st_size

while 1:
time.sleep (10)
statinfo_after = os.stat("C:\Users\Desktop\test.txt")
statinfo_size_after = statinfo_after.st_size
if statinfo_size_after != statinfo_size_before:
print("Updated: test.txt")
statinfo_size_before = statinfo_size_after
break
else:
pass

def main(self):
master = Tk()
ws = master.winfo_screenwidth()
hs = master.winfo_screenheight()
x = (ws/2) - (180)
y = (hs/2) - (70)
master.title("File Update")
master.geometry('360x140+%d+%d' % (x, y))
msg = Message(master, text = "FILE UPDATE", width=700)
msg.config(font=('century gothic', 28))
msg.grid(row=3, column=0)

start_button = tk.Button(master, text='START', font=('century gothic', 24),
bg='green', fg='white', height=1, width=17,
relief='raised', command=self.did_file_update)
start_button.grid(row=1, column=0, sticky='n', padx=10, pady=5)

master.mainloop()

app=App()

t = threading.Thread(target = app.abort_window_call)
t1 = threading.Thread(target = app.main)

t.start()
t1.start()


tl;dr How do I make the abort_window_call function terminate the whole program, even after "START" has been pressed and the function did_file_update is running?










share|improve this question














I'm in Python37, on windows 10, working in tkinter and I've written a function did_file_update that is constantly scanning the desktop every 10 seconds to see if the file "test.txt" has changed. There is also a function abort_window_call that creates a window with a red "ABORT" button. I'm trying to write a script that will thread the two functions, and allow me to click the "ABORT" button and terminate the script once the "START" button has been pressed and did_file_update has been called.



import os
import time
import threading
import tkinter as tk
from tkinter import messagebox
import tkinter.messagebox as msg
from tkinter import Button, Message, Tk


global path_to_watch
path_to_watch = "C:\Users\Desktop"

class App():
def __init__(self):
pass

def abort_window_call(self):
abort_window = Tk()
abort_window.title("Would you like to abort?")
abort_window.geometry('50x90+200+200')
abort_button = tk.Button(abort_window, text='ABORT', font=('century gothic', 24),
bg='red', fg='white', height=1, width=5,
relief='raised', command=lambda: abort_window.destroy())
abort_button.pack(padx=10, pady=5)

abort_window.mainloop()

def did_file_update(self):
statinfo_before = os.stat("C:\Users\Desktop\test.txt")
statinfo_size_before = statinfo_before.st_size

while 1:
time.sleep (10)
statinfo_after = os.stat("C:\Users\Desktop\test.txt")
statinfo_size_after = statinfo_after.st_size
if statinfo_size_after != statinfo_size_before:
print("Updated: test.txt")
statinfo_size_before = statinfo_size_after
break
else:
pass

def main(self):
master = Tk()
ws = master.winfo_screenwidth()
hs = master.winfo_screenheight()
x = (ws/2) - (180)
y = (hs/2) - (70)
master.title("File Update")
master.geometry('360x140+%d+%d' % (x, y))
msg = Message(master, text = "FILE UPDATE", width=700)
msg.config(font=('century gothic', 28))
msg.grid(row=3, column=0)

start_button = tk.Button(master, text='START', font=('century gothic', 24),
bg='green', fg='white', height=1, width=17,
relief='raised', command=self.did_file_update)
start_button.grid(row=1, column=0, sticky='n', padx=10, pady=5)

master.mainloop()

app=App()

t = threading.Thread(target = app.abort_window_call)
t1 = threading.Thread(target = app.main)

t.start()
t1.start()


tl;dr How do I make the abort_window_call function terminate the whole program, even after "START" has been pressed and the function did_file_update is running?







python-3.x multithreading tkinter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 23:18









MacItalyMacItaly

10810




10810












  • You can force the application to terminate by executing sys.exit() in the lambda function of abort_button, but it is not a good way. Also you don't need to create thread for main(), just call it directly. Using multiple Tk() is not recommended, use Toplevel() in abort_window_call().

    – acw1668
    Mar 6 at 23:56

















  • You can force the application to terminate by executing sys.exit() in the lambda function of abort_button, but it is not a good way. Also you don't need to create thread for main(), just call it directly. Using multiple Tk() is not recommended, use Toplevel() in abort_window_call().

    – acw1668
    Mar 6 at 23:56
















You can force the application to terminate by executing sys.exit() in the lambda function of abort_button, but it is not a good way. Also you don't need to create thread for main(), just call it directly. Using multiple Tk() is not recommended, use Toplevel() in abort_window_call().

– acw1668
Mar 6 at 23:56





You can force the application to terminate by executing sys.exit() in the lambda function of abort_button, but it is not a good way. Also you don't need to create thread for main(), just call it directly. Using multiple Tk() is not recommended, use Toplevel() in abort_window_call().

– acw1668
Mar 6 at 23:56












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033724%2fthreading-with-tkinter-how-to-terminate-script-from-button-press-while-functio%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033724%2fthreading-with-tkinter-how-to-terminate-script-from-button-press-while-functio%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко