带百分比的TKinter加载栏 - 特定代码

时间:2015-10-25 07:33:56

标签: python tkinter

如何使此 特定 加载条形码同时显示百分比。有人也可以帮我把它放在TKInter窗口,然后关闭 特定的 TKInter窗口???

print 'LOADING'

toolbar_width = 40


for i in range(toolbar_width):
    time.sleep(0.1) # do real work here
    # update the bar
    sys.stdout.write("-")
    sys.stdout.flush()

sys.stdout.write("\n")

1 个答案:

答案 0 :(得分:0)

根据您的代码,我看不到在任何地方打印的百分比。

您想如何同时打印?在哪里?

我个人看到了多种选择:

  • 使用Tkinter并在顶部或旁边使用不同的小部件,同时增加两个值。
  • 使用控制台输出,您可以清除控制台并根据您的过程打印格式化的字符串(例如&#34; ------ 70%---&#34; )< / LI>

如果你想使用tkinter,你应该首先创建你的脚本,然后尝试运行它,如果你遇到错误你可以问一个特定问题的特定错误的问题集。

正如布莱恩之前提到的,这是StackOverflow而不是某种&#34; LetMeCodeThatForYou&#34; 平台。不过,我会附上一些伪代码作为可以完成的提示。

您可以尝试使用某些tkinter代码:

import Tkinter as tk
class MyWidget(tk.Window):
    def __init__(self, *args, **kwargs):
        # [...] for real code there needs to be more in here...
        self.percentage=tk.StringVar()
        self.progressbarwidget=SomeWidget(self)
        self.progressbarwidget.grid()        
    def increase_counter(step=1):
        self.percentage.set("%s%%"%(int(self.percentage.get()[-1:])+step))
        self.progessbarwidget.configure(width=width+step)
        if self.percentage.get()=="100%":
            #destroy the window / widget / whatever here...
            self.destroy()
        else:
            self.after(1000, self.increase_counter)

在你的应用程序中使用这样的东西会同时更新两个值,百分比值和小部件的外观。

请注意,这是仅示例代码,某种暗示如何构建您想要的结构

另请查看Python: Is it possible to create an tkinter label which has a dynamic string when a function is running in background?