tkinter 进度条 - 链接到功能? [英] tkinter progressbar - linked to function?

查看:59
本文介绍了tkinter 进度条 - 链接到功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解 tkinter (python 2.7) 中的 Progressbar 小部件时遇到了一些麻烦,我去了 这里here 但我仍然不明白如何将 Progressbar 本身(在确定"模式下)链接到我的函数.

I'm having some trouble understanding the Progressbar widget in tkinter (python 2.7) I went here and here but I still don't understand how to link the Progressbar itself (in "determinate" mode) to my functions.

有没有人有关于如何将功能完成链接到进度条更新的示例?我确实研究过这个,我只是找不到任何有用的信息.关于我的(已经可以工作的)代码的注释 - 在代码运行时创建了一个 .txt 文件,该文件打印了函数的总数和正在运行的当前函数.我想将 progressbar 与这些 .txt 文件中的信息链接起来.

Does anyone have any examples on how to link the completion of functions to the progress-bar updating? I did research this, I just can't find any helpful information. A note on my (already working) code - a .txt file is created as the code runs that prints the total number of functions and the current function that is being run. I would like to link the progressbar with the information in these .txt files.

更具体地说,我如何将值更改为正确的数字?

More specifically, how do I get value to change to be the correct number?

推荐答案

工作示例 - 您可以在两种类型的进度条中看到当前值和最大值.

Working example - you can see current value and max value in both types progressbars.

--

您可以在函数中使用 self.var_ind.set()self.var_det.set()(在类 MainWindow 内)更新进度条.

You can use self.var_ind.set() or self.var_det.set() in your function (inside class MainWindow) to update progressbar.

--

如果您的函数在类之外,则必须使用 self.var_indself.var_det 作为参数来调用它.

If your function is outside class you have to call it with self.var_ind or self.var_det as argument.

 your_function(self.var_det)

和内部函数使用它

 def your_function(progress):
     progress.set(10) # set new value
     progress.set( progress.get() + 23 ) # add 23 to current value

我在BEGINSET 23中使用var_ind.set()self.var_det.set()>, END 设置新值.

I use var_ind.set() and self.var_det.set() in BEGIN, SET 23, END to set new value.

--

如果您必须在进度条中设置 max,您还需要添加 self.pbar_indself.pbar_det 作为参数.

If you have to set max in progressbar you will need add self.pbar_ind or self.pbar_det as argument too.

 your_function(self.var_det, self.pbar_det)

和内部函数使用它

 def your_function(progress, bar):

     bar.config(maximum=200) # set max to 200

     bar.step() # add 1 to current value
     bar.step(23) # add 23 to current value

     progress.set(10) # set new value
     progress.set( progress.get() + 23 ) # add 23 to current value

<小时>

带有更多按钮的新示例:


new example with more buttons:

from Tkinter import *
import ttk

class MainWindow(Frame):

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

        #-----

        self.var_ind = IntVar(self)

        self.pbar_ind = ttk.Progressbar(self, orient="horizontal", length=400, mode="indeterminate", variable=self.var_ind, maximum=100)
        self.pbar_ind.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S, columnspan=3)

        Label(self, text="indeterminate").grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.lab_ind_var = Label(self, text="VAR:")
        self.lab_ind_var.grid(row=1, column=1, pady=2, padx=2, sticky=E+W+N+S)

        self.lab_ind_max = Label(self, text="MAX:")
        self.lab_ind_max.grid(row=1, column=2, pady=2, padx=2, sticky=E+W+N+S)

        #-----

        self.var_det = IntVar(self)

        self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=400, mode="determinate", variable=self.var_det, maximum=100)
        self.pbar_det.grid(row=2, column=0, pady=2, padx=2, sticky=E+W+N+S, columnspan=3)

        Label(self, text="determinate").grid(row=3, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.lab_det_var = Label(self, text="VAR:")
        self.lab_det_var.grid(row=3, column=1, pady=2, padx=2, sticky=E+W+N+S)

        self.lab_det_max = Label(self, text="MAX:")
        self.lab_det_max.grid(row=3, column=2, pady=2, padx=2, sticky=E+W+N+S)

        #-----

        Label(self, text="ANIMATION:").grid(row=4, column=0, pady=2, padx=2, sticky=E+W+N+S)

        Button(self, text='START', command=self.animation_start).grid(row=4, column=1, pady=2, padx=2, sticky=E+W+N+S)
        Button(self, text='STOP', command=self.animation_stop).grid(row=4, column=2, pady=2, padx=2, sticky=E+W+N+S)

        #-----

        Label(self, text="SET:").grid(row=5, column=0, pady=2, padx=2, sticky=E+W+N+S)

        Button(self, text='BEGIN', command=self.set_begin).grid(row=5, column=1, pady=2, padx=2, sticky=E+W+N+S)
        Button(self, text='END', command=self.set_end).grid(row=5, column=2, pady=2, padx=2, sticky=E+W+N+S)

        #-----

        Label(self, text="SET:").grid(row=6, column=0, pady=2, padx=2, sticky=E+W+N+S)

        Button(self, text='23', command=lambda:self.set(23)).grid(row=6, column=1, pady=2, padx=2, sticky=E+W+N+S)
        Button(self, text='77', command=lambda:self.set(77)).grid(row=6, column=2, pady=2, padx=2, sticky=E+W+N+S)

        #-----

        Label(self, text="SET:").grid(row=7, column=0, pady=2, padx=2, sticky=E+W+N+S)

        Button(self, text='+23', command=lambda:self.set_plus(23)).grid(row=7, column=1, pady=2, padx=2, sticky=E+W+N+S)
        Button(self, text='-77', command=lambda:self.set_plus(-77)).grid(row=7, column=2, pady=2, padx=2, sticky=E+W+N+S)

        #-----

        Label(self, text="STEP:").grid(row=8, column=0, pady=2, padx=2, sticky=E+W+N+S)

        Button(self, text='+1', command=lambda:self.step(1)).grid(row=8, column=1, pady=2, padx=2, sticky=E+W+N+S)
        Button(self, text='-10', command=lambda:self.step(-10)).grid(row=8, column=2, pady=2, padx=2, sticky=E+W+N+S)

        #-----

        Label(self, text="MAX:").grid(row=9, column=0, pady=2, padx=2, sticky=E+W+N+S)

        Button(self, text='100', command=lambda:self.max(100)).grid(row=9, column=1, pady=2, padx=2, sticky=E+W+N+S)
        Button(self, text='200', command=lambda:self.max(200)).grid(row=9, column=2, pady=2, padx=2, sticky=E+W+N+S)

        #-----

        self.update_labels_after = False
        self.update_labels()

    #-----          

    def animation_start(self):
        self.update_labels_after = True

        self.pbar_ind.start() # .start(1)
        self.pbar_det.start() # .start(1)

        self.update_labels()

    def animation_stop(self):
        self.update_labels_after = False

        self.pbar_ind.stop()
        self.pbar_det.stop()

        self.update_labels()

    #-----          

    def set_begin(self):
        self.var_ind.set( 0 )
        self.var_det.set( 0 )

        self.update_labels()

    def set_end(self):
        self.var_ind.set( self.pbar_ind.cget('maximum') )
        self.var_det.set( self.pbar_det.cget('maximum') )

        self.update_labels()

    #-----          

    def set(self, val):
        self.var_ind.set( val )
        self.var_det.set( val )

        self.update_labels()

    #-----          

    def set_plus(self, val):
        self.var_ind.set( self.var_ind.get() + val )
        self.var_det.set( self.var_det.get() + val )

        self.update_labels()

    #-----          

    def step(self, val=1):
        self.pbar_ind.step(val) # .step()
        self.pbar_det.step(val) # .step()

        self.update_labels()

    #-----          

    def max(self, val=1):
        self.pbar_ind.config(maximum=val)
        self.pbar_det.config(maximum=val)

        self.update_labels()

    #-----          

    def update_labels(self):

        self.lab_ind_var.config(text='VAR: %d' % (self.var_ind.get()))
        self.lab_ind_max.config(text='MAX: %d' % (self.pbar_ind.cget('maximum')))

        self.lab_det_var.config(text='VAR: %d' % (self.var_det.get()))
        self.lab_det_max.config(text='MAX: %d' % (self.pbar_det.cget('maximum')))

        if self.update_labels_after:
            self.after(50, self.update_labels)

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

(为了创建示例,我使用了 中的部分代码Gianni Spear 问题 使用相同tkinter 中的进度条,用于 Python 中的多个计算)

(To create example I used part of code from Gianni Spear question Using the same progressbar in tkinter for several computation in Python)

这篇关于tkinter 进度条 - 链接到功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆