简体   繁体   English

Ttk 按钮按下时进度条不确定

[英]Ttk Indeterminate progress bar on button press

I am trying to create a progress bar that runs as long as my function is running to show the user that things are happening and not just frozen.我正在尝试创建一个进度条,只要我的 function 正在运行,它就会向用户显示事情正在发生,而不仅仅是冻结。 My function (generate_reports) makes queries to the database and writes to CSV files.我的 function (generate_reports) 对数据库进行查询并写入 CSV 文件。 Here is an abstract version of my code:这是我的代码的抽象版本:

from tkinter import *
from tkinter import ttk
from billing import generate_reports

class app:
  def __init__(self, root):
    self.mainframe = ttk.Frame(root, padding = '4 4 12 12')
    self.mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
    ttk.Button(self.mainframe, text = "Generate Billing Reports", command = self.do_reports).grid(column = 2, row = 3, sticky = (W, E))
  
  def do_reports(self, *args):
     pbar = ttk.Progressbar(self.mainframe, orient = HORIZONTAL, mode = 'indeterminate')
     pbar.grid(row = 4, column = 3, sticky = (W, E))
     t1 = threading.Thread(target = generate_reports, args = [start, end])
     t1.start()
     pbar.start()
     t1.join()
     pbar.stop()
     return

root = Tk()
BillingApp(root)
root.mainloop()

With this code, the progress bar doesn't pop up until after the generate_reports thread is completed and it is unmoving.使用此代码,直到 generate_reports 线程完成并且它不动之后才会弹出进度条。 If I remove the join, everything works fine but it never stops loading.如果我删除连接,一切正常,但它永远不会停止加载。 How can I make the loading bar run for only the duration of the generate_reports thread?如何使加载栏仅在 generate_reports 线程的持续时间内运行?

Heh welcome to the fun world of event driven programming:).呵呵,欢迎来到事件驱动编程的有趣世界:)。 You can't use join here, the point of that function is to block until the thread is done and the whole point of using a thread is to avoid blocking the mainloop.你不能在这里使用join ,function 的要点是阻塞直到线程完成,使用线程的全部目的是避免阻塞主循环。 You have 2 choices: either set up the GUI to constantly poll the thread to see if it's still running, or set up the thread to send a message back to the GUI when it's done.您有 2 个选择:设置 GUI 以不断轮询线程以查看它是否仍在运行,或者设置线程以在完成后将消息发送回 GUI。 This latter option is probably the cleanest, and it's often done using tkinter's event mechanism.后一种选择可能是最干净的,并且通常使用 tkinter 的事件机制来完成。

from tkinter import *
from tkinter import ttk
import threading
import time

def generate_reports(start, end):
    print("provide a mcve next time!")
    time.sleep(5)

def run_report(root, *args):
    generate_reports(*args)
    root.event_generate("<<PhishDoneEvent>>") # yes, this is using tkinter in a thread, but some tkinter methods are ok to use in threads

class BillingApp:
  def __init__(self, root):
    self.mainframe = ttk.Frame(root, padding = '4 4 12 12')
    self.mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
    ttk.Button(self.mainframe, text = "Generate Billing Reports", command = self.do_reports).grid(column = 2, row = 3, sticky = (W, E))
    root.bind("<<PhishDoneEvent>>", self.report_done)

  def do_reports(self, *args):
    # note this makes a new widget with every click ... this is bad. Refactor to reuse the widget. 
    self.pbar = ttk.Progressbar(self.mainframe, orient = HORIZONTAL, mode = 'indeterminate')
    self.pbar.grid(row = 4, column = 3, sticky = (W, E))
    start, end = 4,5
    t1 = threading.Thread(target = run_report, args = [root, start, end])
    t1.start()
    self.pbar.start()

  def report_done(self, event=None):
    self.pbar.stop()
    Label(self.mainframe, text="report done").grid(row = 4, column = 3)

root = Tk()
BillingApp(root)
root.mainloop()

I see that you have accepted the answer above.我看到你已经接受了上面的答案。 However, I would post my answer since it is not based on threading, which I think is simpler and may be more suitable:但是,我会发布我的答案,因为它不是基于线程,我认为这更简单并且可能更合适:

from tkinter import *
from tkinter.ttk import *
import os



root = Tk()

# I set the length and maximum as shown to demonstrate the process in the 
# proceeding function
progress = Progressbar(root, orient = HORIZONTAL,
            length = 200/5, maximum=200/5, mode = 'determinate')

# Function 


def my_func():
    t=0
    r= 1/5   
    for i in range(200):
        print(i)
        t=t+r
        progress['value'] = t
        root.update_idletasks()
  

progress.pack()

# Button
Button(root, text = 'Start', command = bar).pack(pady = 10)


mainloop()

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

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