【问题标题】:Python 3 - How do I use tkinter progressbar with a bat file?Python 3 - 如何将 tkinter 进度条与 bat 文件一起使用?
【发布时间】:2017-05-07 21:24:57
【问题描述】:

到目前为止,bat 运行但进度条没有运行。我如何将两者相互连接?这是输出的图像。 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()

【问题讨论】:

  • 批处理文件是否打印完成百分比?
  • 批处理文件确实不断告诉我完成了多少百分比。
  • 百分比的格式是什么?

标签: python-3.x tkinter tk ttk


【解决方案1】:

这个答案最终不起作用。这个问题仍然悬而未决。

试试这个:

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

基本上,有一个线程从进程中读取数据,并使其可用于每隔一段时间更新进度条的函数。你没有提到输出的格式,所以我写了它使用正则表达式来搜索第一个数字并转换它。

【讨论】:

  • 你能帮忙解释一下输出文件吗?感谢您迄今为止的帮助。我真的很感谢你的辛勤努力。进度条没有更新,我不知道如何为它获取正确的数据。我正在使用 mp3 转换器。数据显示整个 mp3 文件的完成时间。示例:完成 1 分钟、完成 2 分钟等
  • @BHok 我在我的代码中发现了一个小错字。我已经修好了。现在尝试运行它。
  • 进度条没有更新,所以我还在想是输出的原因。
  • @BHok 请发布批处理文件的示例输出。
  • @BHok 哎呀,正则表达式拼写错误。此外,代码假定输出的是百分比,而不是分钟数。如果批处理文件还打印出开始时所需的总时间,我可以让它工作,但同样,我需要知道格式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
相关资源
最近更新 更多