Python 3 - How do I use tkinter progressbar with a bat file?

本小妞迷上赌 提交于 2019-12-10 23:52:32

问题


So far, the bat runs but the progress bar doesn't. How do I connect the two with each other? Here is the image of the output. http://imgur.com/lKbHepS

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

回答1:


This answer ended up not working. The question is still open.

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

Basically, there's a thread that reads the data from the process and makes it available to a function that updates the progress bar every so often. You didn't mention the output's format, so I wrote it to use a regular expression to search for the first number and convert it.



来源:https://stackoverflow.com/questions/43836875/python-3-how-do-i-use-tkinter-progressbar-with-a-bat-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!