wxPython状态栏中的电影进度条

问题描述 投票:0回答:1

[在Windows 10的Python 3.x上运行

我正在研究一个脚本,以帮助将.tiff图像序列自动编译到视频中。我正在使用wxPython构建GUI。首先,我创建窗口类并为窗口设置全局变量。

global main_window
main_window = self

然后,我有一个用于写入状态栏的功能,还可以将值打印到控制台(有时我还添加代码以从发送至此功能的文本中写入日志文件。)>

def update_status_bar(window, text):
    status = str(text)
    window.statusbar.SetStatusText(status)
    print(status)
    window.Refresh()
    window.Update()
    wx.SafeYield(win=None, onlyIfNeeded=False)

这是我编写的moviePy函数,用于将图像序列转换为视频。

def video_from_sequence(image_sequence, video, fps):
    img = []
    update_status_bar(main_window, 'Getting Image Directory')
    path = os.path.dirname(os.path.realpath(image_sequence))
    print(path)
    update_status_bar(main_window, 'Getting List of Image Files')
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith('.tiff'):
                img.append(file)
    os.chdir(path)
    update_status_bar(main_window, 'Creating Video From Image Sequence')
    clip = ImageSequenceClip(img, fps=fps)
    update_status_bar(main_window, clip.write_videofile(video, fps=fps))

打印到控制台不会显示进度,但是状态栏不会填充该过程的当前进度。因为我最终希望以.pyw的形式运行,状态栏显示进度,所以对我来说很重要的一点是,进度栏将显示在状态栏中,但是我很难找到一种方法来执行此操作。

更新(06/01/2020):

我设法使用2个功能来启动和停止进度条,并创建了2个面板而不是一个面板的状态栏。

我在MainWindow类中的状态栏代码已更改为:

self.statusbar = self.CreateStatusBar(2)
self.progress_bar = wx.Gauge(self.statusbar, -1, size=(280,25), style=wx.GA_PROGRESS)
self.progress_bar_active = False
self.Show()
self.progress_bar.SetRange(50)
self.progress_bar.SetValue(0)

我启动动画的功能:

def start_busy_statusbar(window):
    window.count = 0
    window.proc = subprocess.Popen(['ping', '127.0.0.1', '-i', '0.2'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    while True:
        wx.Yield()
        try:
            list_data = window.proc.stdout.readline()
            wx.Yield()
        except:
            break
        if len(list_data) == 0:
            break
        window.progress_bar.Pulse()
        wx.Yield()
        window.count += 1

还有我停止动画的功能:

def stop_busy_statusbar(window):
    window.progress_bar.Destroy()
    window.progress_bar = wx.Gauge(window.statusbar, -1, size=(280, 25), style=wx.GA_PROGRESS)

这确实有点粗糙,但是有效。

所以我的调用video_from_sequence()函数的convert()函数如下所示:

    def convert(self, event):
        start_busy_statusbar(main_window)
        image_sequence = str(self.text_image_sequence_dir.GetValue())
        original_video = str(self.text_original_video_dir.GetValue())
        if image_sequence.endswith('.tiff') and original_video.endswith('.mkv'):
            try:
                new_video = str(original_video)[:-4] + '_1080p.mkv'
                temp_video = str(original_video)[:-4] + '_temp.mkv'
                print(image_sequence)
                print(original_video)
                print(new_video)
                fps = get_frame_rate(original_video)
                print(fps)
                video_from_sequence(image_sequence, temp_video, fps)

            except FileNotFoundError as e:
                e = str(e).replace('Errno 2] ', '')
                e = e.replace('directory:', 'directory:\n')
                warning(e)
            update_status_bar(self, 'Finished')
        else:
            warning('You must enter valid paths for both a tiff sequence and original video.')
        stop_busy_statusbar(main_window)

我现在遇到的是,窗口在处理图像序列时将其锁定。有人建议我使用单独的线程或使用wx.Yield。我不太确定如何执行这些操作。我已经尝试过CallAfter(function(vars)),但这似乎不起作用。

我已经复制了代码片段,以演示它在wxPython中的工作原理,但是我不太了解它的工作原理,因此无法在我的脚本中使用它。

[在Windows 10上的Python 3.x上运行。我正在研究一个脚本,以帮助将.tiff图像序列自动编译为视频。我正在使用wxPython构建GUI。首先,我创建窗口类,然后...

python python-3.x wxpython moviepy
1个回答
1
投票

这里是在状态栏中实现进度条的非常古老的示例。它应该给您足够的工作空间。

© www.soinside.com 2019 - 2024. All rights reserved.