据我所知,在使用tkinter时,这是一个经常出现的问题,因为它不是multi-threaded.我读了很多关于这个问题的书,但我不知道我该如何解决这个问题.

我的应用程序由一个在本地服务器上运行的plotly dash组成,它有一个按钮,该按钮调用一个函数,该函数对EXCEL执行一系列处理.起初,我按下按钮就得到了main thread is not in main loop.尽管如此,我还是设法避开了daemon度.但是,如果我再次按下按钮,程序将停止工作,并出现上面提到的错误.

这是我的情节短跑的总结 playbook :

def interact_callbacks(self):
        @callback(
            Output('add-automatically-button', 'children'),
            Input('add-automatically-button', 'n_clicks')
        )
        def update_output(n_clicks):
            if n_clicks is not None:
                t = threading.Thread(target=startInteraction)
                t.setDaemon(True)
                t.start()
                return 'Clicked!'
            else:
                return 'Add automatically'

这是另一个.py中的函数startInteraction()

def startInteraction():
    gui = GUI()
    gui.get_excel_path()
    path=gui.path

    confirmation = gui.confirmationPopUp(path)
    sys.stderr.write("***OK: selected**\n")
    
    if(confirmation):
        sys.stderr.write("***OK***\n")
        defectLoader(path)
        
    else:
        sys.stderr.write("***CANCEL***\n")
        gui.errImport()
        exit()

这是我的gui类(我在这个类中有更多的弹出窗口)

class GUI():
    def __init__(self):
        self.path = None    

    def get_excel_path(self):
        size = {
            "size": (40, 1)
        }

        layout = [
            [sg.Text('Select Database (Excel):')],
            [sg.Input(**size), sg.FileBrowse(file_types=(("Excel File", "*.xlsx"),))],
            [sg.OK(), sg.Cancel()]
        ]

        window = sg.Window('File Browser', layout)
        event, values = window.read()
        window.close()

        try:
            file_path = values[0]
            if event == 'Cancel' or not file_path:
                sys.stderr.write("***CANCEL***\n")
                raise Exception('No file selected')

            self.path= file_path
        except Exception as e:
            sg.popup(f'Error: {e}')
            exit()

如何在此代码中编写线程队列?还有别的 Select 吗? 先谢谢你.

推荐答案

这是try 将plotly dash库与tkinterpysimplegui组合时的典型问题.几年前我也遇到过类似的问题,并通过以下方式解决了这个问题:

p = Process(target=yourFunctionName)
p.start()

不要忘记执行以下导入操作:

from multiprocessing import Process

在您的 case 中:

def interact_callbacks(self):
    @callback(
        Output('add-automatically-button', 'children'),
        Input('add-automatically-button', 'n_clicks')
    )
    def update_output(n_clicks):
        if n_clicks is not None:
            p = Process(target=startInteraction)
            p.start()
            return 'Clicked!'
        else:
            return 'Add automatically'

我希望这解决了你的问题,你可以完成你的应用程序.

Python相关问答推荐

如何让 turtle 通过点击和拖动来绘制?

如何使用Python将工作表从一个Excel工作簿复制粘贴到另一个工作簿?

管道冻结和管道卸载

Python键入协议默认值

在Python argparse包中添加formatter_class MetavarTypeHelpFormatter时, - help不再工作""""

Pandas DataFrame中行之间的差异

什么是最好的方法来切割一个相框到一个面具的第一个实例?

如何使用Pandas DataFrame按日期和项目汇总计数作为列标题

joblib:无法从父目录的另一个子文件夹加载转储模型

从嵌套的yaml创建一个嵌套字符串,后面跟着点

用渐近模计算含符号的矩阵乘法

为什么\b在这个正则表达式中不解释为反斜杠

网格基于1.Y轴与2.x轴显示在matplotlib中

如何杀死一个进程,我的Python可执行文件以sudo启动?

如何将数据帧中的timedelta转换为datetime

freq = inject在pandas中做了什么?''它与freq = D有什么不同?''

Pandas:计数器的滚动和,复位

为什么我的scipy.optimize.minimize(method=";newton-cg";)函数停留在局部最大值上?

如何将一个文件的多列导入到Python中的同一数组中?

我如何处理超类和子类的情况