【问题标题】:Apply two or more commands at the same time in python在python中同时应用两个或多个命令
【发布时间】:2019-12-19 17:01:34
【问题描述】:

我创建了一个简单的程序来将视频转换为音频,使用moviepy库和程序如下:

import moviepy.editor as mp
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import asksaveasfile, askopenfile
from tkinter.messagebox import showerror

def Open():
    video_path = askopenfile(initialdir = "Video", title = "Open Video", filetypes = (("Video", "*.MP4;*.AVI;*.WAV;*.MKV;*.webm;*.TS;*.ASF;*OOG"), ("All files", "*.*")))

    try:
        open(video_path.name, "r")
        ent_mp4.delete(0, END)
        ent_mp4.insert(0, video_path.name)
    except:
        pass

def save_in():
    if not(ent_mp4.get() == ""):
        saveas = asksaveasfile(initialdir = "Music", title = "Save in", filetype = [("Aduio", "*.MP3;*.FLAC;*.CD;*.WMV"), ("All filest", "*.*")])

        ent_mp3.delete(0, END)
        ent_mp3.insert(0, saveas.name)
    else:
        showerror("Error: Empty Entry", "Pleas Enter Your Video Path")

def convert():
    video = ent_mp4.get()
    music = ent_mp3.get()

    if not(music == ""):
        root.destroy()

        clip = mp.VideoFileClip(video)
        clip.audio.write_audiofile(music)

        main()
    else:
        showerror("Error: Empty Entry", "Pleas Enter Your Music Path")

ent_mp4, ent_mp3, root = None, None, None
bu1, bu2, bu3 = None, None, None
def main():
    global ent_mp3, ent_mp4, root, bu1, bu2, bu3, sv
    root = Tk()
    root.title("Convert Video to Audio")

    ent_mp4 = ttk.Entry(root, width = 50)
    ent_mp4.grid(row = 0, column = 0)

    ent_mp3 = ttk.Entry(root, width = 50)
    ent_mp3.grid(row = 1, column = 0)

    ttk.Button(root, text = "Open video", command = lambda: Open()).grid(row = 0, column = 1)

    ttk.Button(root, text = "Save in", command = lambda: save_in()).grid(row = 1, column = 1)

    f = ttk.Frame(root)
    f.grid(row = 2, column = 0, columnspan = 2)

    ttk.Button(f, text = "Convert", command = lambda: convert()).pack(side = LEFT)

    ttk.Button(f, text = "Exit", command = lambda: exit()).pack(side = RIGHT)

    root.mainloop()


main()

程序成功了但是我只想加点简单的,就是在root.destroy ()切换进程运行的时候,不是去掉窗口,而是加了一个动画GIF,表示进程正在进行中

我怀疑解决方案是同时应用两个东西以使过程成功

但我没有找到如何尝试“with”命令,但没有成功

那么解决办法是什么

【问题讨论】:

标签: python python-3.x user-interface tkinter moviepy


【解决方案1】:

线程将允许两个或多个函数同时运行。

from threading import Thread

def func1():
    print('Working')

def func2():
    print('Working')

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    相关资源
    最近更新 更多