我有两个功能: screenshot()和reader() 。 我想在按下0时调用screenshot() ,按下1时调用reader() 。 如果按下d ,它应该退出。 这是我的尝试:
from PIL import Image import pyscreenshot as ImageGrab import datetime import time import os import pyperclip def screenshot(): if __name__ == "__main__": im=ImageGrab.grab() timestr = time.strftime("%Y%m%d-%H%M%S") file_name = "sample_" + timestr + ".png" file_location = os.path.join('/path', file_name) im.save(file_location) def reader(): pyperclip.copy("sample") while True: x = raw_input('Press enter for a random letter...') print x if x == "0": screenshot() if x == "1": reader() if x == "d": break但它没有按预期工作。 当我按0 ,没有任何反应。 当我按1 ,没有任何反应。 但是当第一次按d时,它会调用screenshot() 。 下一次d被按下时,它退出。
I have two functions: screenshot() and reader(). I want screenshot() to be called when 0 is pressed, and reader() to be called when 1 is pressed. And it should exit, if d is pressed. This is how I tried:
from PIL import Image import pyscreenshot as ImageGrab import datetime import time import os import pyperclip def screenshot(): if __name__ == "__main__": im=ImageGrab.grab() timestr = time.strftime("%Y%m%d-%H%M%S") file_name = "sample_" + timestr + ".png" file_location = os.path.join('/path', file_name) im.save(file_location) def reader(): pyperclip.copy("sample") while True: x = raw_input('Press enter for a random letter...') print x if x == "0": screenshot() if x == "1": reader() if x == "d": breakBut it is not working as expected. When I press 0, nothing happens. When I press 1, nothing happens. But when d is pressed for the first time, it calls screenshot(). And next time d is pressed, it exits.
最满意答案
if __name__ == "__main__":删除此行if __name__ == "__main__": 。 阅读器()正在工作,你知道它是如何工作的吗? 它复制一个sample文本,尝试粘贴到您的文本编辑器。
Remove this line if __name__ == "__main__":. And reader() is working, do you know how it works? its copying a sample text, try to paste in your text editor.
更多推荐
发布评论