Python 将截图代码转化为函数

Python turning screenshot code into function

我已经为我的代码创建了一个屏幕截图方法,但我想将所述代码转换为一个函数,因为我想使用它几次。这是代码:

import os

for i in range(20):

       print("")

 print("Screenshot taken.")

 for i in range(20):

        print("")

 os.system("screencapture Screenshottie.png")

 os.system('Screenshottie.png')

formatting edits

我建议查找基本的 Python introduction/tutorial,但无论如何:

def screenshot(fname):
    print("Screenshot taken.")
    os.system("screencapture {}".format(fname))
    os.system(fname)

我不能完全理解你的问题,但是在 python 中,当你想创建一个函数时,你应该像这样使用 def:

import os


def screenshotter():

    os.system("screencapture Screenshottie.png")

    os.system('Screenshottie.png')

    return "Screenshot taken."


for i in range(20):

    print(screenshotter())

print("Done.")