2014-11-02 107 views
0

我查看了其他用戶提出的各種其他問題,並且沒有找到我正在尋找的內容,如果我錯過了某些東西,請告訴我。我正在尋找一種簡單的方法將進度條連接到我的tkinter GUI中的每個函數。我是python的新手,所以我仍然在學習如何做基本的事情。我希望進度條在用戶按下按鈕時以確定的模式進行更新,以便他們知道GUI仍在工作,並知道完成每項任務需要多長時間。因此,例如,當他們點擊script1時,如何編寫完成該任務期間更新的進度條的代碼?這裏是我的代碼如下:將進度條連接到個人功能+ tkinter gui

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() 

我只包括第一個腳本調用,因爲其他調用幾乎相同。在功能之外調用進度條是否更好,即衡量每個功能的進度條?或者我應該有一個進度條連接到每個單獨的功能?無論哪種方式,我如何構建這樣的事情?提前致謝。

回答

0

你可以看看這些帖子: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打電話給我哪些代碼,我不能再做很多事情來幫助你,對不起。 (是否真的需要外部代碼的呼叫?)

祝你好運。

Arthur。