中使用相同的进度条进行多次计算

中使用相同的进度条进行多次计算

本文介绍了在 tkinter 中使用相同的进度条进行多次计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用相同的进度条(确定和不确定)的最佳方法.在此示例中,相同的进度条用于不确定、确定和函数不确定的计算.当我运行代码时,只显示最后一个进度条.

I wish to know how is the best method to use the same progressbar (determinate and indeterminate). In this example the same progressbar is used for a indeterminate, for a determinate, and function-indeterminate computation. When i run the code only the last progressbar is showed.

from Tkinter import *
import ttk
import tkFileDialog
import time

def foo(m, n, self_from_class):
    for i in xrange(m):
        i * n
        self_from_class.pbar_f.step(1)
        self_from_class.update()
        time.sleep(0.1)
    return i

class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("ProgressBar example")
        self.master.minsize(200, 100)
        self.grid(sticky=E+W+N+S)

        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        self.start = Button(self, text='Start', command=self.start, activeforeground="red")
        self.start.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.pbar_ind = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate")
        self.pbar_ind.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=300, mode="determinate")
        self.pbar_det.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.pbar_f = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate")
        self.pbar_f.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)

    def start(self):
        for i in xrange(10):
            self.pbar_ind.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)
        for i in xrange(10):
            self.pbar_ind.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)
        res = foo(10, 2, self)


if __name__=="__main__":
   d = MainWindow()
   d.mainloop()

更新 - 我不优雅的解决方案是:

UPDATE - my not elegant solution is:

class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("ProgressBar example")
        self.master.minsize(200, 100)
        self.grid(sticky=E+W+N+S)

        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        self.start = Button(self, text='Start', command=self.start, activeforeground="red")
        self.start.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.pbar_f = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate")

        self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=300, mode="determinate")

        self.pbar_ind = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate")
        self.pbar_ind.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)



    def start(self):
        for i in xrange(10):
            self.pbar_ind.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)
        self.pbar_ind.grid_forget()
        self.pbar_det.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)
        for i in xrange(10):
            self.pbar_det.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)
        self.pbar_det.grid_forget()
        self.pbar_f.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)
        res = foo(10, 2, self)

推荐答案

如果将所有小部件放在一个 cell 中 - 它们具有相同的 columnrow - 然后你把它们放在另一个上面.最后一个在顶部,您会看到.

If you put all widgets in one cell - they have the same column and row - then you put them one over another. Last one is on the top and you see it.

您只能使用一个 Progressbar 小部件 - 用于所有计算.

You can use only one Progressbar widget - for all computation.

你可以有几个 Progressbar 并使用 grid/grid_forget 来显示一个并隐藏另一个.

You can have few Progressbars and use grid/grid_forget to show one and hide anothers.

您可以将 Progressbar 放在不同的单元格中,并在屏幕上显示所有内容.

You can put Progressbars in different cells and have all on the screen.

带有 grid_forget

from Tkinter import *

#-----------------------------------

def on_press():
    global visible

    if visible == 1:
        l1.grid_forget()
        l3.grid(row=0,column=3)
        visible = 3
    else:
        l1.grid(row=0,column=0)
        l3.grid_forget()
        visible = 1

#-----------------------------------

master = Tk()

l1 = Button(master, text='press there >>')
l1.grid(row=0,column=0)

l2 = Button(master, text='change', command=on_press)
l2.grid(row=0,column=1)

l3 = Button(master, text='<< press there')
#l3.grid(row=0,column=3)

visible = 1

master.mainloop()

这篇关于在 tkinter 中使用相同的进度条进行多次计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:43