2017-05-07 6 views
2

これまでのところ、バットは走っていますが、進捗バーは動きません。どのように私はお互いに2つを接続するのですか?ここに出力のイメージがあります。 http://imgur.com/lKbHepSPython 3 - tkinterプログレスバーとbatファイルを使用するにはどうすればよいですか?

from tkinter import * 
from tkinter import ttk 
from subprocess import call 

def runBat(): 
    call("mp3.bat") 

root = Tk() 

photobutton3 = PhotoImage(file="smile.png") 
button3 = Button(root, image=photobutton3, command=runBat) 
button3.grid() 

pbar = ttk.Progressbar(root, orient=HORIZONTAL, length=200, mode='determinate') 
pbar.grid() 

root.mainloop() 
+0

バッチファイルは完了したパーセントを出力しますか? – anonymoose

+0

バッチファイルは、完了したパーセンテージの割合を継続的に示しています。 –

+0

パーセントはどの形式ですか? – anonymoose

答えて

0

この答えは働いていないことになりました。質問はまだ開いています。

はこれを試してみてください:

import subprocess 
import threading 
import ctypes 
import re 
from tkinter import * 
from tkinter import ttk 

class RunnerThread(threading.Thread): 
    def __init__(self, command): 
     super(RunnerThread, self).__init__() 
     self.command = command 
     self.percentage = 0 
     self.process = None 
     self.isRunning = False 

    def run(self): 
     self.isRunning = True 
     self.process = process = subprocess.Popen(self.command, stdout = subprocess.PIPE, shell = True) 
     while True: 
      #Get one line at a time 
      #When read() returns nothing, the process is dead 
      line = b"" 
      while True: 
       c = process.stdout.read(1) 
       line += c 
       if c == b"" or c == b"\r": #Either the process is dead or we're at the end of the line, quit the loop 
        break 
      if line == b"": #Process dead 
       break 
      #Find a number 
      match = re.search(r"Frame\=\s(\d+\.?(\d+)?)", line.decode("utf-8").strip()) 
      if match is not None: 
       self.percentage = float(match.group(1)) 
     self.isRunning = False 

    def kill(self): #Something I left in case you want to add a "Stop" button or something like that 
     self.process.kill() 


def updateProgress(): 
    progressVar.set(rt.percentage) #Update the progress bar 
    if rt.isRunning: #Only run again if the process is still running. 
     root.after(10, updateProgress) 

def runBat(): 
    global rt 
    rt = RunnerThread("mp3.bat") 
    rt.start() 
    updateProgress() 

root = Tk() 

photobutton3 = PhotoImage(file="smile.png") 
button3 = Button(root, image=photobutton3, command=runBat) 
button3.grid() 

progressVar = DoubleVar() 
pbar = ttk.Progressbar(root, orient=HORIZONTAL, length=200, mode='determinate', variable = progressVar) 
pbar.grid() 

root.mainloop() 

基本的には、プロセスからデータを読み取り、しょっちゅうプログレスバーを更新機能で使用できるようにしますスレッドがあります。出力の形式については言及していないので、正規表現を使用して最初の数値を検索して変換するように記述しました。

+0

出力ファイルについて説明してもらえますか?これまでのご協力ありがとうございます。私は本当にあなたの献身的な努力のためにそれを感謝します。進行状況バーが更新されていないため、適切なデータを取得する方法がわかりません。私はMP3コンバータで働いています。データは、合計mp3ファイルから完成した時間を示します。例:1分が完了し、2分が完了しました。 –

+0

@BHok私のコードに小さなタイプミスがあります。私はそれを修正しました。今すぐ実行してみてください。 – anonymoose

+0

プログレスバーは更新されないので、まだ出力されていると思っています。 –

関連する問題