简体   繁体   English

tkinter中带有python的Progressbar(ttk.Progressbar)不显示

[英]Progressbar (ttk.Progressbar) with python in tkinter not showing

Using the ttk.Progressbar in Python (in a tkinter GUI), I'm trying to have a progressbar appear when I press a button. 在Python(在tkinter GUI中)中使用ttk.Progressbar ,我试图在按下按钮时显示进度条。 Clicking the "Analyse" button on the gui calls the function analyze in the script, which I've meant to be a three-stage rocket of sorts: 单击gui上的“ Analyse”按钮,将在脚本中调用函数analysis,我打算将其作为三阶段火箭:

  1. grid() the progressbar-widget to add it to the mainframe, and start() it. grid() progressbar-widget,将其添加到大型机,然后start()

  2. Do the actual computing (which takes about three seconds in my test-runs) 进行实际的计算(在我的测试运行中大约需要三秒钟)

  3. stop() the progressbar and grid_remove() it to hide it from the user again, now that the job is complete. stop() progressbar和grid_remove()可以将其再次向用户隐藏,因为作业已完成。

If I comment out step 3 , the progressbar will be shown and started as expected, while step 2 is executed. 如果我注释掉第3步,则会在执行第2步的同时显示进度条并按预期方式启动。 But if step three remains, the progressbar is never shown in the first place. 但是,如果仍然保留第三步,则进度条永远不会显示在第一位。 Now, since the work in step two is being executed either way, I'm assuming that it might have something to do with gridding and removing of the progressbar-widget in the same function-call, but since this is my first real gui project in python, I'm a bit at a loss on how to work around that (if that indeed is the problem). 现在,由于以任何一种方式都执行了第二步中的工作,因此我假设它可能与网格化以及在同一函数调用中删除progressbar-widget有关,但是由于这是我的第一个真正的gui项目在python中,我对如何解决这个问题有点茫然(如果确实是问题所在)。 Is there a way around this, or a way of more elegantly accomplishing the same task? 有没有办法解决这个问题,或者更优雅地完成同一任务? The function the button calls is as follows: 按钮调用的功能如下:

def analyze():
    # --Step 1--
    progressbar.grid()
    progressbar.start()

    # --Step 2--
    global analysis
    analysis = analyzer.Analysis(file_path.get())
    analysis.output_to_file()

    # --Step 3--
    progressbar.stop()
    progressbar.grid_remove()

I'd post a picture of my interface to clarify further, but my since my current reputation score does not allow posting of images, I hope I've made the problem clear enough with my explanation (and code). 我将发布界面的图片以进一步阐明,但是由于我目前的声誉得分不允许发布图片,因此我希望我已通过解释(和代码)使问题足够清楚。

In order for the a widget to appear after being created, the event loop must be allowed to run. 为了使小部件在创建后出现,必须允许事件循环运行。 That is because the actual drawing of the widget on the screen happens as a result of a redraw event. 那是因为窗口小部件在屏幕上的实际绘制是重绘事件的结果。

You can call update_idletasks which will allow the event loop to service such redraw events, after creating and starting the progress bar. 您可以调用update_idletasks ,在创建并启动进度条之后,它将允许事件循环为此类重绘事件提供服务。 However, while your calculation is running your app will likely appear to be frozen. 但是,在运行计算时,您的应用可能会被冻结。 Again, this is because the event loop must be allowed to service events, which it can't do when doing other things. 同样,这是因为必须允许事件循环为事件提供服务,而在执行其他操作时则不能这样做。

The normal way to solve this problem is to run your long running calculation in a thread or separate process. 解决此问题的通常方法是在线程或单独的进程中运行长时间运行的计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM