Diese Antwort endete nicht funktioniert. Die Frage ist noch offen.
Try this:
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()
Grundsätzlich gibt es einen Thread, der die Daten aus dem Prozess liest und macht es auf eine Funktion zur Verfügung, die den Fortschrittsbalken aktualisiert jeder so oft. Sie haben das Format der Ausgabe nicht erwähnt, also habe ich es geschrieben, um mit einem regulären Ausdruck nach der ersten Nummer zu suchen und sie zu konvertieren.
Wird in der Batchdatei der Prozentwert gedruckt? – anonymoose
Die Batch-Datei sagt mir kontinuierlich, wie viel Prozent abgeschlossen ist. –
In welchem Format ist der Prozentsatz? – anonymoose