简体   繁体   English

如何从 2 个不同的 def 函数中获取 2 个东西并在不同的 def 中使用它们

[英]How can I grab 2 things from 2 different def functions and use them in a different def

I'm trying to grab both the image and audio from those and use them both in another def, how would I do that?我正在尝试从中获取图像和音频,并在另一个 def 中同时使用它们,我该怎么做?

def image():
    image = filedialog.askopenfilename(filetypes = (("Image Files",".png .jpg"),("all files","*.*")))
def audio():
    audio = filedialog.askopenfilename(filetypes = (("Audio Files",".mp3 .wav"),("all files","*.*")))

This is the def I'm trying to use them in.这是我试图使用它们的定义。

def create():
    os.system(f"ffmpeg -i {image} -i {audio} -vf scale=1920:1080 output.mp4")

You need to return the value of image and audio , like so:您需要返回imageaudio的值,如下所示:

def image():
    image = filedialog.askopenfilename(filetypes = (("Image Files",".png .jpg"),("all files","*.*")))

    return image

def audio():
    audio = filedialog.askopenfilename(filetypes = (("Audio Files",".mp3 .wav"),("all files","*.*")))

    return audio

And then use them:然后使用它们:

def create():
    image = image()
    audio = audio()
    
    os.system(f"ffmpeg -i {image} -i {audio} -vf scale=1920:1080 output.mp4")

Additionally, it'd probably be a good idea to wrap the file names in shlex.quote to make it work with file names that have spaces in them and files named by a malicious user.此外,将文件名包装在shlex.quote中可能是一个好主意,以使其与包含空格的文件名和恶意用户命名的文件一起使用。

Just return values from the other functions and call them in the function which needs these values.只需从其他函数返回值并在需要这些值的 function 中调用它们。

def image():
    image = filedialog.askopenfilename(filetypes = (("Image Files",".png .jpg"),("all files","*.*")))
    return image

def audio():
    audio = filedialog.askopenfilename(filetypes = (("Audio Files",".mp3 .wav"),("all files","*.*")))
    return audio

def create():
    os.system(f"ffmpeg -i {image()} -i {audio()} -vf scale=1920:1080 output.mp4")

What actually happens when you return a value:返回值时实际发生的情况:

Ex 1.例 1。

def example():
    a = "Hello"
    return a
value = example() # --> value = a = "Hello"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM