在 tkinter 应用程序中使用短横线的方法是什么?

huangapple go评论53阅读模式
英文:

Is there a way to use dash in a tkinter app

问题

无法在同时使用tkinter应用程序并在关闭dash后使用它

我正在使用tkinter和dash。在tkinter应用程序中单击按钮后,会在浏览器上打开一个仪表板。问题是,一旦浏览器打开,即使关闭它,我之后也无法再使用tkinter应用程序。

从lauchDash.py中提取:

def openDashboard():
    app1 = dash.Dash(__name__, external_stylesheets=[dbc.themes.SIMPLEX])
    if __name__ == '__main__':
        app1.run_server(port=8050)

从tkinter应用程序中提取:

from threading import *
import dash

if __name__ == "__main__":
    app = App()
    app.mainloop()

def button_click(self):
        # 启动仪表板
        t1 = Thread(target=lauchDash.openDashboard())
        t1.start()

我尝试使用多线程,但我猜我使用得不对。是否有一种方法可以同时使用tkinter应用程序和dash?

谢谢。

英文:

Cannot use tkinter App at the same time and after dash has been closed

I am working with tkinter and dash. Upon clicking on a button in the tkinter app, a dashboard is opened on a browser. The issue is, while the browser is opened, and even if it is closed, I cannot use the tkinter app afterwards.

Extract from lauchDash.py

def openDashboard():
    app1 = dash.Dash(__name__, external_stylesheets=[dbc.themes.SIMPLEX])
    if __name__ == '__main__':
        app1.run_server(port=8050)

Extract from tkinter app

from threading import *
import dash

if __name__ == "__main__":
    app = App()
    app.mainloop()

def button_click(self):
        # Lauch Dashboard 
        t1 = Thread(target = lauchDash.openDashboard())
        t1.start()

I have tried using multithreading, but I guess I have been using it wrong. Is there a way to use tkinter app along with dash?

Thank you

答案1

得分: 1

以下是翻译好的部分:

我测试了您的代码,并添加了一些自己的代码以使其工作。从我所了解的情况来看,问题出在您调用button_click方法时,tk窗口会冻结。问题很简单,当您指定线程的目标(即t1 = Thread(target = lauchDash()))时,您传递的是lauchDash方法的返回值,而不是它的引用。因此,您实际上是在定义线程时直接执行该方法,而不是让线程调用它。要修复这个问题,您只需要删除括号,以便不传递方法的值,而是传递它的引用。

以下是应该这样做的示例代码:

from threading import *
import dash
from tkinter import Tk, Button
import dash_bootstrap_components as dbc

def button_click():
    # 启动仪表板
    t1 = Thread(target=lauchDash)
    t1.start()

class App(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.draw_widgets()

    def draw_widgets(self):
        start_dash = Button(self, text="Start", command=button_click)
        start_dash.pack()

def lauchDash():
    app1 = dash.Dash(__name__, external_stylesheets=[dbc.themes.SIMPLEX])
    if __name__ == '__main__':
        app1.run_server(port=8050)

# 从tkinter应用程序中提取
if __name__ == "__main__":
    app = App()
    app.mainloop()

希望这有所帮助!如果您需要更多解释,请编辑您的问题或评论我的答案。

英文:

I tested your code adding a little bit of my own to make it work.
So from what I gather the problem was when you called the button_click method the tk window would freeze.
The problem was simply that when you specified the threads target (i.e t1 = Thread(target = lauchDash())) you are passing the return value of the lauchDash method rather then its referance. So effectively you were executing the method directly when defining your thread rather then letting the thread call it. All you need to do to fix this is to remove the parentheses so as to not pass the method value but it's reference.

Here is an example of how you should do this.

from threading import *
import dash
from tkinter import Tk, Button
import dash_bootstrap_components as dbc

def button_click():
    # Lauch Dashboard
    t1 = Thread(target = lauchDash)
    t1.start()

class App(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.draw_widgets()

    def draw_widgets(self):
        start_dash = Button(self, text="Start", command=button_click)
        start_dash.pack()

def lauchDash():
    app1 = dash.Dash(__name__, external_stylesheets=[dbc.themes.SIMPLEX])
    if __name__ == '__main__':
        app1.run_server(port=8050)

# extract from tkinter app
if __name__ == "__main__":
    app = App()
    app.mainloop()

I hope this helps ! Edit your question or comment on my answer if you want more clarification.

huangapple
  • 本文由 发表于 2023年5月25日 16:50:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定