微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

嘿,我在使用进度栏以及同时运行的流程时遇到问题

如何解决嘿,我在使用进度栏以及同时运行的流程时遇到问题

您可以阅读下面的代码获取详细信息。主要问题是,按下转换键时,它确实将视频转换为音频,但是进度条不起作用,“ pB.join()”之后的语句也没有执行,并且我的程序停止响应。我也尝试使用标志全局变量来杀死函数中的线程,但它也无法正常工作。在tkinter的情况下,线程使用可能受到限制。期待答案。

'''

    import moviepy.editor as mp
    from tkinter import *
    from tkinter.ttk import Progressbar,Style
    import tkinter.font as font
    import time
    import threading

    #Conversion Function
    def convertToAudio():
        finished = False
        result.configure(text=" ")
        window.update()
        time.sleep(1)
        pB = threading.Thread(target=progressBar,args=(lambda : finished,))
        pB.start()          #I do not think the thread starts as the function is not executed
        try:
            f1 = Video.get()
            f2 = Audio.get()
            clip = mp.VideoFileClip(f1)    
            clip.audio.write_audiofile(f2)
            finished = True
            pB.join()
            result.configure(text="DONE")   #This statement does not get executed
        except:
            finished = True
            pB.join()
            result.configure(text="Failed") #This statement does not get executed

    #Progress Bar   
    def progressBar(finished):
        i=1
        while(True):
            progress['value'] = (20*i)%120
            window.update()
            time.sleep(0.4)
            i+=1
            if(finished()):
                break


    #Main Window
    #Basic
    window = Tk()
    window.geometry("500x200")
    window.title("MP3 CONVERTER")
    window.configure(bg = "grey")
    Myfont = font.Font(family="Calibri",size=16)

    #Variables
    Video = StringVar()
    Audio = StringVar()

    #Video
    L1 = Label(window,font =Myfont,text="VIDEO FILE :",background="grey")
    L1.place(x=100,y=40)
    E1 = Entry(window,textvariable = Video)
    E1.place(x=215,y=45,width=200)

    #Audio
    L2 = Label(window,font=Myfont,text="AUdio FILE :",background="grey")
    L2.place(x=98,y=70)
    E2 = Entry(window,textvariable = Audio)
    E2.place(x=215,y=75,width=200)

    #Main Button
    convert = Button(window,text="Convert",command=convertToAudio)
    convert.place(x=334,y=105,height=25)

    #Result Label
    result = Label(window,background="grey")
    result.place(x=230,y=160)

    #Loading
    progress = Progressbar(window,orient=HORIZONTAL,length=100,mode='indeterminate')
    progress.place(x=210,y=130)

    window.mainloop()

'''

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。