python PyQt4中sys.argv是做什么用的 [英] what is sys.argv used for in python PyQt4

查看:74
本文介绍了python PyQt4中sys.argv是做什么用的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编程方面,我仍然是初学者,而在 GUI 编程方面,我尤其陌生.我在 PyQt4 中使用 python,我正在遵循教程指南.下面的代码块比较容易理解:

I'm still a beginner when it comes to programming, and I'm especially new when it comes GUI programming. I'm using python with PyQt4 and im following a tutorial guide. The following code block is relatively easy to follow:

import sys
from PyQt4 import QtGui
def window():
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    b= QtGui.QLabel(w)
    b.setText("Hello World!")
    w.setGeometry(100,100,200,50)
    b.move(50,20)
    w.setWindowTitle("PyQt")
    w.show()
    sys.exit(app.exec_())
if __name__ == '__main__':
    window()

我可以很好地了解这里发生的事情,但是有人可以向我解释 sys.argv 实际上在做什么吗?我不想每次都盲目地把它放进去,希望它能让我的代码工作!

I can follow whats going on here quite well, but could someone explain to me what the sys.argv is actually doing? I don't want to just blindly put this in every time in the hope that it will make my code work!

推荐答案

argc(参数计数 = 参数数量)和 argv(一个数组或列表参数值,取决于语言)是操作系统提供给程序的参数.在其他世界:它是如何命名的".

The argc (argument count = number of arguments) and argv (an array, or list of argument values, depending on the language) is what the operating system gives to the program as parameters. In other worlds: "how it was called".

正如cptPH 的回答所明确指出的,这并非特定于QtPython.看看这里:

As clearly stated by cptPH's answer, this is not specific to Qt or Python. Have a look here:

https://en.wikipedia.org/wiki/Entry_point

对于 C 和 C++:

For C and C++:

https://en.wikipedia.org/wiki/Entry_point#C_and_C.2B.2B

对于 Python:

https://en.wikipedia.org/wiki/Entry_point#Python

即使是 shell 脚本也有这些,称为位置参数":

Even shell scripting have these, called "Positional parameters":

http://www.tldp.org/LDP/abs/html/othertypesv.html

即使您的程序根本没有命令行选项,无论是什么语言或操作系统(除非非常、非常 异国情调),您也将始终需要最低限度:

Even if your program takes no command line options at all, whatever the language or OS (unless very, very exotic ones), you will always have to a bare minimum:

  • a argc 为 1,因为,
  • argv 数字 0 将是您的程序被调用的名称(这可能会有所不同,例如,如果您通过符号链接调用它,或者仅仅因为您更改了构建过程的目标二进制文件,并且您的程序foo"现在是bar" - 例如使用:打印输出时不要使用硬编码的程序名称,而是使用 argv 数字 0).
  • a argc of 1, because,
  • argv number 0 will be the name under which your program is called (this may vary, for example if you call it via a symlink, or simply because you changed the target binary of the build process, and your program "foo" is now "bar" - ex use: don't use hardcoded program name when printing output, rather use argv number 0).

迟早,您将编写使用选项/参数的程序.所以你会用到这些.示例:您可能想要添加--verbose"或--debug"选项.或者--input-file",如果它是关于文件处理的.任何.您的程序将在启动时解析这些 argv,并通过您明智的编码,相应地更改其运行时行为.

Sooner or later, you'll be writing programs which use options/parameters. So you'll use these. Example: you may want to add a "--verbose" or "--debug" option. Or a "--input-file" if it's about file processing. Whatever. Your program will parse these argv at startup, and through your wise coding, will change its runtime behavior accordingly.

示例:即使是 Python 也可以采用相当多的可选选项,并使用 argc/argv 处理它们.来自 Python 3 手册页:

Example: even the Python can takes quite of lot of optional options, and processes them using argc/argv. From Python 3 man page:

   python [ -B ] [ -b ] [ -d ] [ -E ] [ -h ] [ -i ] [ -I ]
          [ -m module-name ] [ -q ] [ -O ] [ -OO ] [ -s ] [ -S ] [ -u ]
          [ -v ] [ -V ] [ -W argument ] [ -x ] [ [ -X option ] -?  ]
          [ -c command | script | - ] [ arguments ]

...这些对于 Python 解释器来说都是可能的 argv.

...these are all possible argv for the Python interpreter.

Qt 应用程序的真实示例:

Real life example for a Qt application:

我参与开发了一个 Qt 网络应用程序.它可以在 2 种互斥模式下运行:

I take part in the developed a Qt networking application. It can run under 2 mutually exclusive modes:

  • 正常"模式,
  • 或间谍模式":这是特殊的,因为它需要网络一些低级操作的管理员权限.

所以我添加了一个 CLI -s/--spy-mode CLI 选项.这是一个可选的 argv.

So I added a CLI -s/--spy-mode CLI options to it. These is an optional argv.

在应用程序代码的开头,我检查了这个 argv -s/--spy-mode CLI 选项

At the beginning of the application code, I check for this argv -s/--spy-mode CLI options

  • 如果在没有这个选项的正常"模式下运行,GUI 元素相关间谍"模式甚至不会显示给用户,也不会显示任何它的类实例化.那是干净的,因为否则它会在许多对象实例化/方法中无论如何都会失败到处都是错误,因为低级调用会失败无论如何,然后我会以非零返回码退出.那不是好的.如果您可能会失败,请尽快失败.
  • 如果使用这个 argv 选项以间谍"模式启动,首先要做的是我在我的代码中做的是检查调用用户确实有这样的网络管理员"权限,然后 1/如果他没有,则发出一个明确的错误消息/弹出窗口解释你应该拥有 blablabla 特权,blablabal 尝试 sudo blablabla",然后以非零返回码退出,或者 2/如果用户确实有所需的特权,提供特殊的间谍模式"GUI 元素和使用它们,没问题(并隐藏大部分 GUI正常模式"元素,在这种操作模式下毫无意义).
  • If run in "normal" mode without this option, the GUI elements related to "spy" mode are simply not even displayed to the user, nor any of its class instanciated. That is clean, because otherwise it would fail miserably anyway in many object instanciation/methods, with errors all over the place, since the low-level calls would fail anyway, and then I'd exit with a non-zero return code. That is not good. If you might fail, fail ASAP.
  • If launched in "spy" mode using this argv options, the first things I do in my code is check that the calling user do actually have such "network administrator" privileges, and then 1/ if he does not, issue an explicit error message/popup window explaining the "you should have blablabla privilege, blablabal try sudo blablabla", and then exit with a non-zero return code, or 2/ if the user does indeed have the required privilege, offer the special "spy mode" GUI elements and put them to use, no problem (and hide most of the GUI "normal mode" elements, meaningless in this mode of operation).

这就是 argc/argv 的优点.

这篇关于python PyQt4中sys.argv是做什么用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆