据我所知,在使用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 吗? 先谢谢你.