简体   繁体   English

如何通过main()函数中的pass命令使代码在Shell中保持打开状态?

[英]How do I keep my code open in the shell with a pass command in the main() function?

I'm trying to learn how to build a web browser bot as half learning half project for someone else and I've hit a snag. 我正在尝试学习如何将Web浏览器机器人构建为对其他人的一半学习一半的项目,但我遇到了麻烦。

The site I'm using as guide has: 我用作指导的网站有:

def main():
     pass

Which he claims keeps the shell window open do he can run various functions like get x,y cords of the mouse position and take screen shots. 他声称可以使外壳窗口保持打开状态,他可以运行各种功能,例如获取鼠标位置的x,y线并进行屏幕截图。

However when I run my code exactly as he has it in the guide it immediately opens and closes. 但是,当我完全按照指南中的说明运行代码时,它会立即打开和关闭。

What I don't want is something like, "make it so pressing enter closes shell instead", what needs to happen is the window stays open so I can enter various functions. 我不需要的是这样的东西,“使其按Enter即可关闭shell”,需要发生的是窗口保持打开状态,以便我可以输入各种功能。

What am I doing wrong? 我究竟做错了什么? Am I suppose to just import the code in a different shell and run the functions outside it? 我是否想只将代码导入另一个shell中并在其外部运行函数?

The code: 编码:

import os   
import time
import ImageGrab

x_pad = 0   
y_pad = 61

def screenGrab():   
    box = (x_pad,y_pad,x_pad+1919,y_pad+970)    
    im = ImageGrab.grab(box)    
    im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) + '.png','PNG')

def main(): 
    pass    

if __name__ == '__main__':    
    main()

The guide is: http://code.tutsplus.com/tutorials/how-to-build-a-python-bot-that-can-play-web-games--active-11117 该指南为: http : //code.tutsplus.com/tutorials/how-to-build-a-python-bot-that-c​​an-play-web-games--active-11117

You have three ways: 您有以下三种方式:

  1. Start the intepreter with the -i option, as suggested by Ulrich in the comments: 根据Ulrich在注释中的建议,使用-i选项启动解释器:

     python -i my-script.py 

    This way, the interpreter will be left open as soon as your script finishes execution and a prompt will be shown. 这样,您的脚本完成执行后,解释器将保持打开状态,并显示提示。

  2. Use pdb . 使用pdb This is often used for debugging, and has a different interface than the usual Python prompt. 这通常用于调试,并且具有与通常的Python提示符不同的界面。 If you're not familiar with it, it might not be the best option in your case. 如果您不熟悉它,那么它可能不是您的最佳选择。 Replace pass with these two lines: 用以下两行替换pass

     import pdb pdb.set_trace() 
  3. Use code . 使用code This will give you an interface much more similar to the usual Python shell and can be an alternative to pdb if you're not familiar with it: 这将为您提供一个与通常的Python Shell更相似的接口,并且如果您不熟悉它,可以替代pdb

     import code code.interact() 

By the way, you were not doing anything wrong per se. 顺便说一句,您本身并没有做错任何事情。 The pass statement is not meant to "halt Python and start a prompt", it's just needed as a filler for functions or loops with an empty body. pass语句并不是要“停止Python并启动提示”,而只是将其填充为函数或循环为空的主体。

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

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