Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 由于线程问题,按钮回调只工作一次_Python 3.x_Multithreading_Tkinter_Python Multithreading - Fatal编程技术网

Python 3.x 由于线程问题,按钮回调只工作一次

Python 3.x 由于线程问题,按钮回调只工作一次,python-3.x,multithreading,tkinter,python-multithreading,Python 3.x,Multithreading,Tkinter,Python Multithreading,由于如何设置'command='参数,我只能使用此按钮的回调一次。我希望能够在回调函数完成后再次运行它,但是我不知道如何为'command='参数提供一个新的线程对象。我按下它一次并完成函数的过程,但在它完成后再次按下按钮,我会得到“RuntimeError:线程只能启动一次”。下面是按钮和回调的代码: def ocr_callback(): no_file_to_save_to = False try: status_label.pack_forget()

由于如何设置'command='参数,我只能使用此按钮的回调一次。我希望能够在回调函数完成后再次运行它,但是我不知道如何为'command='参数提供一个新的线程对象。我按下它一次并完成函数的过程,但在它完成后再次按下按钮,我会得到“RuntimeError:线程只能启动一次”。下面是按钮和回调的代码:

def ocr_callback():
    no_file_to_save_to = False
    try:
        status_label.pack_forget()
        for f in files:    # files comes from another callback and is globally defined
            name, extension = os.path.splitext(f)
            if extension != '.pdf':
                raise
        if len(files) == 1:
            new_file = filedialog.asksaveasfilename(filetypes=[('PDF', '.pdf')], defaultextension='.pdf')
            if not new_file:
                no_file_to_save_to = True
                raise
            try:
                ocrmypdf.ocr(files[0], new_file, use_threads=True)
            except ocrmypdf.exceptions.PriorOcrFoundError:
                ocrmypdf.ocr(files[0], new_file, redo_ocr=True, use_threads=True)
        elif len(files) > 1:
            directory = filedialog.askdirectory()
            if not directory:
                no_file_to_save_to = True
                raise
            for f in files:
                file_name = f.split('/')[-1]
                try:
                    ocrmypdf.ocr(f, directory + '/' + file_name, use_threads=True)
                except ocrmypdf.exceptions.PriorOcrFoundError:
                    ocrmypdf.ocr(f, directory + '/' + file_name, redo_ocr=True, use_threads=True)
        status_label.config(text='Process Complete!', fg='blue')
        status_label.pack(expand='yes')
    except:
        if no_file_to_save_to:
            status_label.config(text='No file to save to. Process Cancelled', fg='blue')
        else:
            status_label.config(text='Error: One or more of the files could be corrupt.', fg='red')
        status_label.pack(expand='yes')


ocr_button = Button(root, text='OCR Files', relief='groove', bg='#5D1725', bd=0, width=scaled(20), fg='white',
                    command=threading.Thread(target=ocr_callback).start, state=DISABLED)
ocr_button.pack()

有什么想法可以改变它使它工作吗?我知道这个函数必须线程化,否则窗口将暂停并冻结自己,直到它完成。“ocr”功能是导致线程阻塞和必要性的原因。

您可能应该从启动功能启动线程,而不是从按钮命令内部启动线程

也许是这样:

def launch_cmd(dummy=None):
    threading.Thread(target=ocr_callback).start()

...
ocr_button = Button(root, text='OCR Files', relief='groove', bg='#5D1725',\
                    bd=0, width=scaled(20), fg='white', command=launch_cmd, state=DISABLED)

这成功了!非常感谢。另外,“dummy”参数做什么?good,good-
dummy
是一个占位符,以防回调发送一个默认情况下不使用的arg。您可以安全地移除它。