我一整天都在為下面的代碼苦苦掙扎。
import webbrowser
import dash
from dash import html, dcc
from dash.dependencies import Input, Output, State
from Open_Save_File import open_file_dialog
import tkinter
from tkinter import filedialog as fd
FILE_DIR = 'H:/Codes/'
webbrowser.get('windows-default').open('http://localhost:8050', new=2)
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.H4('Update my list',
style={
'textAlign': 'center'
}),
html.Br(),
html.Hr(),
html.Div([
html.H5('Excel Directory:',
style = {'width': '20%', 'display': 'inline-block', \
'text-align': 'left'}),
html.Div(id='selected_directory', children='No file selected!', \
style={'width': '30%', 'display': 'inline-block'}),
html.Button('Browse', id='open_excel_button', \
n_clicks=0, style={'float': 'right', 'display': 'inline-block'})
]),
])
# 1. Callback for open_excel button
@app.callback(
Output(component_id='selected_directory', component_property='children'),
Input(component_id='open_excel_button', component_property='n_clicks'),
prevent_initial_call=True
)
def open_excel_function(open_excel):
print ('*** 1A. Callback open_file_dialog')
ctx = dash.callback_context
trigger = ctx.triggered[0]['prop_id'].split('.')[0]
print("***", trigger, "is triggered.")
root = tkinter.Tk()
root.withdraw()
# root.iconbitmap(default='Extras/transparent.ico')
if trigger == 'open_excel_button':
file_directory = tkinter.filedialog.askopenfilename(initialdir=FILE_DIR) <-- Source of all evil....
print('***', file_directory)
else:
file_directory = None
return file_directory
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=False)
它應該使用 Dash 庫和 Tkinter 在瀏覽器中打開以下 UI:

如果單擊“瀏覽器”按鈕,將在指定的初始目錄打開一個打開檔案對話框:

第一次作業正常,下次我會收到以下錯誤:

任何人都可以幫助找出tkinter.filedialog...的行有什么問題嗎?
我嘗試過其他解決方案,例如這個。但是我用這個嘗試了一切,但不知道如何設定初始目錄。對于InitialDir這個不起作用。
使用 tkinkter,我可以設定初始目錄,但得到錯誤(作業一次后),如上面的螢屏截圖所示。基本上我被卡住了。
提前感謝您的任何指示。
uj5u.com熱心網友回復:
在callback你
- 創建 tkinter 主視窗
root = tkinter.Tk() - 把它藏起來
root.withdraw()
但是當你退出檔案對話框時你永遠不會破壞它。
當它再次運行時,callback它會嘗試創建第二個主視窗,并與之前的(隱藏的)主視窗發生沖突。
如果我使用root.destroy(),那么代碼可以正常作業。
app.callback(
Output(component_id='selected_directory', component_property='children'),
Input(component_id='open_excel_button', component_property='n_clicks'),
prevent_initial_call=True
)
def open_excel_function(open_excel):
print ('*** 1A. Callback open_file_dialog')
ctx = dash.callback_context
trigger = ctx.triggered[0]['prop_id'].split('.')[0]
print("***", trigger, "is triggered.")
root = tkinter.Tk()
root.withdraw()
# root.iconbitmap(default='Extras/transparent.ico')
if trigger == 'open_excel_button':
file_directory = tkinter.filedialog.askopenfilename(initialdir=FILE_DIR)
print('***', file_directory)
else:
file_directory = None
root.destroy() # <--- SOLUTION
return file_directory
甚至
def open_excel_function(open_excel):
print ('*** 1A. Callback open_file_dialog')
ctx = dash.callback_context
trigger = ctx.triggered[0]['prop_id'].split('.')[0]
print("***", trigger, "is triggered.")
if trigger == 'open_excel_button':
root = tkinter.Tk()
root.withdraw()
# root.iconbitmap(default='Extras/transparent.ico')
file_directory = tkinter.filedialog.askopenfilename(initialdir=FILE_DIR)
print('***', file_directory)
root.destroy() # <--- SOLUTION
else:
file_directory = None
return file_directory
編輯:
如果你只想得到 dirname 那么也許你應該使用tkinter.filedialog.askdirectory()而不是tkinter.filedialog.askopenfilename()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/427105.html
