Dash在单击按钮上打开tkinter文件选择对话框

时间:2019-07-25 23:07:13

标签: plotly-dash

我正在尝试打开文件对话框以选择一些本地文件。显然,Dash没有内置此选项,因此我尝试使用tkinter来实现。我发现Dash语法与所有这些奇怪的依赖项非常混淆。当我运行以下示例时,文件对话框在启动脚本后立即打开,但在单击按钮时不会打开。我想要相反的行为。当我单击按钮时,应该打开文件选择器对话框,而不是在启动脚本后打开。

import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
from tkinter import Tk, filedialog

app = dash.Dash(
    __name__, external_stylesheets=["https://codepen.io/chriddyp/pen/bWLwgP.css"]
)

app.layout = html.Div(
    [
        html.H1("File-selector"),
        html.Button('Select files', id='button'),
        html.Div(id='container-button-basic',
                 children='Enter a value and press submit'),
        dcc.Graph(id="graph", style={"width": "75%", "display": "inline-block"}),
    ]
)

@app.callback(
    Output('container-button-basic', 'children'),
    [Input('button', 'children')] )
def on_click(bla):
    root = Tk()
    root.withdraw()
    root.call('wm', 'attributes', '.', '-topmost', True)
    files = filedialog.askopenfilename(multiple=True)
    #...do something with the files...
    return 'test'

app.run_server(debug=True)

1 个答案:

答案 0 :(得分:0)

也许您正在寻找上载组件... https://dash.plot.ly/dash-core-components/upload

如果没有,那么您的Input依赖项应该位于按钮的“ n_clicks”属性上,而不是“ children”上:https://dash.plot.ly/dash-core-components/button