How to make Stop button to terminate start function already running in Tkinter (Python)

2024/11/13 10:20:29

I am making a GUI using Tkinter with two main buttons: "Start" and "Stop". Could you, please, advise on how to make the "Stop" button to terminate the already running function called by "Start" button for the following code?

The problem as you may expect is that the entire window including the "Stop" button is stuck/not responsive while "start" function is running.

The "start" function extracts some information from a number of html files which may take pretty while (for 20 huge files it can take around 10 minutes), and I would like for a user to be able to interrupt that process at any moment.

from tkinter import *
import Ostap_process_twitter_file_folderroot = Tk()def start (event):    Ostap_process_twitter_file_folder.start_extraction()def stop (event):# stop "start" functionlabel1 = Label(root, text = "source folder").grid(row=0)label2 = Label(root, text = "output folder").grid(row=1)e_sF = Entry(root)e_oF = Entry(root)e_sF.grid(row=0, column=1)e_oF.grid(row=1, column=1)startButton = Button(root, text = "start")startButton.grid(row=2)startButton.bind("<Button-1>", start)stopButton = Button(root, text = "stop")stopButton.grid(row=2, column=1)stopButton.bind("<Button-1>", stop)root.mainloop()

I suppose that using threads will be a solution for this issue. Although I've been looking through similar questions here on stackoverflow and various introductory resources on threading in Python (not that much introductory, btw), it is still not clear for me how exactly to implement those suggestions to this particular case.

Answer

why do you think using threads would be a solution? ...

you cannot stop a thread/process even from the main process that created/called it. (at least not in a mutliplatform way ... if its just linux thats a different story)

instead you need to modify your Ostap_process_twitter_file_folder.start_extraction() to be something more like

halt_flag = False
def start_extraction(self):while not Ostap_process_twitter_file_folder.halt_flag:process_next_file()

then to cancel you just do Ostap_process_twitter_file_folder.halt_flag=True

oh since you clarified i think you just want to run it threaded ... I assumed it was already threaded ...

def start(evt):th = threading.Thread(target=Ostap_process_twitter_file_folder.start_extraction)th.start()return th
https://en.xdnf.cn/q/119485.html

Related Q&A

adding language to markdown codeblock in bulk

My Problem is to add to every single block of code a language in my markdown files. Ive hundreds of files in nested directories. The files have this form: ```language a ```Normal text``` b ```Normal te…

Cant randomize list with classes inside of it Python 2.7.4

I am new to coding and I need some help. Im trying to randomize these rooms or scenes in a text adventure but whenever I try to randomize it they dont even show up when I run it! Here is the script:fro…

calculate the queue for orders based on creation and delivery date, by product group

I have a Pandas dataframe containing records for a lot of orders, one recorde for each order. Each record has order_id, category_id, created_at and picked_at. I need to calculate queue length for each …

Python print with string invalid syntax

I have a rock, paper, scissors code Ive been working on lately (yes, I am a total noob at coding), and I get an Invalid Syntax error with this specific line:print(The magical 8ball reads "Your for…

How to load images and text labels for CNN regression from different folders

I have two folders, X_train and Y_train. X_train is images, Y_train is vector and .txt files. I try to train CNN for regression. I could not figure out how to take data and train the network. When i us…

How to calculate number of dates within a year of a date in pandas

I have the following dataframe and I need to calculate the amount of ER visit Dates with a score of 1 that are one year after the PheneDate for that pheneDate for a given subject. So basically phenevi…

Remove substring from string if substring in list in data frame column

I have the following data frame df1string lists 0 i have a dog [fox, dog, cat] 1 there is a cat [dog, house, car] 2 hello everyone [hi, hello, everyone] 3 …

how to save data in the db django model?

Good day, I cant really understand what Im doing wrong in here. I was using this function base view to store my scrap data in the database with the django model, but now its not saving any more. I cant…

Move existing jointplot legend

I tried answers from a previous question to no avail in Matplotlib 1.5.1. I have a seaborn figure:import seaborn as sns %matplotlib inline import matplotlib.pyplot as plt import numpy as np tips = sns.…

timezone conversion of a large list of timestamps from an excel file with python

I have an excel file named "hello.xlsx". There is a column of timestamps that has a lot of rows (more than 80,000 rows for now). The file basically looks like this:03/29/2018 19:24:5003/29/20…