首页 > 解决方案 > wxPython中如何使仪表跟随线程中的下载功能?

问题描述

我是 YouTube 下载器,但仪表进度不遵循下载此视频的功能我希望仪表从视频下载开始,当下载完成时仪表完成

import wx
from pytube import YouTube
from time import sleep
from threading import Thread


class Tube(wx.Frame):
  def __init__(self, *args, **kwargs):
    super(Tube, self, *args, **kwargs).__init__(parent=None,id=wx.ID_ANY, title="New Frame", size=(600, 400))
    self.Centre()
    self.panel = wx.Panel(self, id=wx.ID_ANY, name="New Panel")
    self.textLink = wx.StaticText(parent=self.panel, id=wx.ID_ANY, label="Edit YouTube URL", pos=(100, 50))
    self.editLink = wx.TextCtrl(parent=self.panel, id=wx.ID_ANY, size=(200, 25), pos=(250,50))
    self.downloadButton = wx.Button(parent=self.panel, id=wx.ID_ANY, label="Download Video", pos=(250, 100))
    self.downloadButton.Bind(event=wx.EVT_BUTTON, handler=self.thread, source=self.downloadButton)
    self.progress = wx.Gauge(parent=self.panel, id=wx.ID_ANY, range=100,size=(200,25), pos=(200, 200))
    self.Show()

  def progressBar(self, *args, **kwargs):
    for value in range(101):
      wx.CallAfter(self.progress.SetValue, value)
      sleep(0.2)

  def Download(self, *args, **kwargs):
    self.video = YouTube(self.editLink.GetValue())
    self.stream = self.video.streams.get_lowest_resolution()
    self.stream.download()

  def thread(self, *args, **kwargs):
    self.threads = []
    self.thread1 = Thread(target=self.Download)
    self.thread2 = Thread(target=self.progressBar)
    self.threads.append(self.thread1)
    self.threads.append(self.thread2)
    for thread in self.threads:
      thread.setDaemon(True)
      thread.start()

if __name__ == '__main__':
  app = wx.App()
  screen = Tube()
  app.MainLoop()

标签: pythondownloadyoutubewxwidgetsgauge

解决方案


为什么不使用 wxProgressDialog?

检查对话框示例以获取详细信息...

谢谢你。


推荐阅读