I'm working with Python 2.7 and PyGTK 2.24. I am working with the following tutorial. Please read it for code context.
http://www.pygtk.org/pygtk2tutorial/sec-PackingDemonstrationProgram.html
The bottom block of code (reprinted below) is throwing the following error when I type it in (verbatum):
if __name__ =="__main__":
if len(sys.argv) != 2:
sys.stderr.write("usage: packbox.py num, where num is 1, 2, or 3.\n")
sys.exit(1)
PackBox1(string.atoi(sys开发者_Python百科.argv[1]))
main()
usage: packbox.py num, where num is 1, 2, or 3.
Traceback (most recent call last): File "C:/GTKTutorial/packbox.py", line 161, in sys.exit(1) SystemExit: 1
Additionally, if I change the code to the following to overcome the first error, I get the next error message:
if __name__ =="__main__":
if len(sys.argv) != 1:
sys.stderr.write("usage: packbox.py num, where num is 1, 2, or 3.\n")
sys.exit(1)
PackBox1(string.atoi(sys.argv[1]))
main()
Traceback (most recent call last): File "C:/GTKTutorial/packbox.py", line 162, in PackBox1(string.atoi(sys.argv[1])) IndexError: list index out of range
What is wrong? How do I fix the code so I can work with the tutorial>
You need to call it from the command line with packbox.py 1
, packbox.py 2
, or packbox.py 3
.
This will result in there being two arguments (the name of the program and the first thing you pass to it), so you won't trigger the sys.exit(1)
, and argv[1]
will be a valid index access.
To run PackBox.py directly from IDLE,
REPLACE:
if __name__ =="__main__":
if len(sys.argv) != 2:
sys.stderr.write("usage: packbox.py num, where num is 1, 2, or 3.\n")
sys.exit(1)
PackBox1(string.atoi(sys.argv[1]))
main()
WITH:
if __name__ == "__main__":
packbox = PackBox1(3)
main()
To see all three example widget arrangements, substitute argument (3) with arguments (1) & (2). Click on X to exit the window; the Quit buttons aren't connected in this code.
精彩评论