首页 > 解决方案 > 将 Plotly-Dash 与 Tkinter GUI 相结合 - Dash 强制再次执行 Tk-mainloop

问题描述

我目前正在开发一个小工具,用于解析用户在 Tkinter GUI 中提供的一些输入文件。然后它启动一个 Dash-Server 来可视化解析的数据。

我现在面临的问题是调用dash_app.run_server()似乎重新执行mainloop了 GUI,以便打开一个新窗口并且 Dash-Server 崩溃。但这只会发生一次。如果我从新窗口再次启动该过程,服务器会正​​确启动并且不会出现新窗口。

我也尝试在一个新线程中启动 Dash,但它似乎需要在主线程中运行,所以这是没有选择的。

关于如何解决这个问题有什么建议吗?

最小的例子:

from tkinter import *
import dash
import dash_html_components as html


class App(Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        Button(self, text="Start",
               command=self.start).pack()

    def start(self):
        self.destroy()
        plotter = Plotter()


class Plotter():
    def __init__(self):
        self.__run_dash()

    def __run_dash(self):
        dash_app = dash.Dash(__name__, prevent_initial_callbacks=True)
        dash_app.layout = html.H1("Test")
        dash_app.run_server(debug=True)


def main():
    app = App()
    app.mainloop()


if __name__ == "__main__":
    main()

标签: pythontkinterplotlyplotly-dash

解决方案


推荐阅读