2017-10-16 70 views
-1

我已經爲我的代碼創建了截圖方法,但是我想將所述代碼轉換爲函數,因爲我想多次使用它。這是代碼:Python將屏幕截圖代碼轉換爲函數

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') 

格式編輯

+0

你想要的函數內的代碼是什麼? –

+0

我想基本上把我上面發佈的所有東西放到它裏面,這樣我就可以輸入一行,並在需要時激活該代碼,而不必輸入所有這些行。 – INeedHelpAndFast

+0

方法和功能是一回事。你還應該閱讀_very_,_very_基本的Python,因爲其中大部分解釋了函數如何工作 – OptimusCrime

回答

0

我無法理解你的完全問題,但在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.") 
1

我建議找了一個基本的Python介紹/教程,但在任何情況下:

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