如何使“停止"按钮终止“开始"函数已经在 Tkinter (Python) 中运行 [英] How to make "Stop" button to terminate "start" function already running in Tkinter (Python)

查看:54
本文介绍了如何使“停止"按钮终止“开始"函数已经在 Tkinter (Python) 中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tkinter 制作一个带有两个主要按钮的 GUI:开始"和停止".请您建议如何使停止"按钮终止以下代码的开始"按钮调用的已运行功能?

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.

开始"函数从一些可能需要很长时间的 html 文件中提取一些信息(对于 20 个巨大的文件,它可能需要大约 10 分钟),我希望用户能够在任何时候.

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_folder

root = Tk()

def start (event):    
     Ostap_process_twitter_file_folder.start_extraction()

def stop (event):
     # stop "start" function
    label1 = 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()

我想使用线程将是解决此问题的方法.尽管我一直在查看有关 stackoverflow 的类似问题以及有关 Python 线程的各种介绍性资源(没有那么多介绍性,顺便说一句),但我仍然不清楚如何将这些建议应用于这种特殊情况.

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.

推荐答案

为什么您认为使用线程是一种解决方案?...

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

即使从创建/调用它的主进程中也无法停止线程/进程.(至少不是以多平台方式......如果它只是 linux 那是另一回事)

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)

相反,您需要修改您的 Ostap_process_twitter_file_folder.start_extraction() 以使其更像

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()

然后取消您只需执行 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

这篇关于如何使“停止"按钮终止“开始"函数已经在 Tkinter (Python) 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆