【问题标题】:Connect progressbar to individual functions+tkinter gui将进度条连接到各个函数+tkinter gui
【发布时间】:2014-11-02 22:10:16
【问题描述】:

我查看了其他用户提出的各种其他问题,但没有找到我正在寻找的确切内容,如果我可能遗漏了什么,请告诉我。我正在寻找一种简单的方法来将进度条连接到我的 tkinter GUI 中的每个功能。我是 python 新手,所以仍在学习如何做基本的事情。每当用户按下按钮时,我希望进度条在确定模式下更新,因此他们知道 GUI 仍在工作并且知道完成每项任务还剩多少时间。因此,例如,当他们单击 script1 时,我如何编写在该任务完成期间更新的进度条的代码?下面是我的代码:

 from sys import exit
 from Tkinter import *
 from subprocess import Popen, PIPE
 import os
 import time
 import sys
 import ttk

 Root = Tk()
 Root.title("PIER PROCESSING GUI")

 class Command:
     def __init__(self, func, *args, **kw):
         self.func = func
         self.args = args
         self.kw = kw
     def __call__(self, *args, **kw):
         args = self.args+args
         kw.update(self.kw)
         self.func(*args, **kw)

     def script1(Where):
         Cmd = "build_new_pier %s"%Where
         print "Cmd =", Cmd
         Sp = Popen(Cmd, shell = True, stderr = PIPE)
         Ret = Sp.stderr.read()
         if len(Ret) != 0:
             print Ret
         return
     def quitter():
        exit(0)

     Label(Root, text = "Push button to build a new PIER").pack(side = TOP, pady = 5)
     Sub = Frame(Root)
     Button(Sub, text = "build_new_pier WEST", command = Command(script1, \
     "WEST")).pack(side = LEFT, pady = 5)
     Button(Sub, text = "build_new_pier EAST", command = Command(script1, \
     "EAST")).pack(side = LEFT, pady = 5) 
     Button(Sub, text = "build_new_pier SOUTH", command = Command(script1, \
     "SOUTH")).pack(side = LEFT, pady = 5)

     Sub.pack(side = TOP)
     Button(Root, text = "Quit", command = quitter).pack(side = TOP, pady = 5)
     Root.geometry("700x500+500+500")
     Root.mainloop()

我只包含了第一个脚本调用,因为其他调用几乎相同。在函数之外调用进度条是否更好,即测量每个函数的进度条?或者我应该有一个连接到每个单独功能的进度条?无论哪种方式,我如何构建这样的东西?提前致谢。

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    你可以看看这些帖子:Tkinter command ignores some lines 我提供了一个在特定时间内连续移动的进度条示例。

    适合您的用例可能是:

    Create a thread
    Call your shell command through "popen" in this thread
    While waiting the thread end, make the progress bar moving !
    Return the result and delete the progress bar.
    

    我让您尝试这个并更新您的问题以提供更新的尝试。 由于我不知道应该用 popen 调用哪个脚本,所以我无法为您提供更多帮助,抱歉。 (真的需要调用外部脚本吗?)

    祝你好运。

    亚瑟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 2021-05-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多