我目前正在使用Zelle的Introductory文本学习Python,我正在尝试重新创建一个使用附带文件graphics.py的示例程序.因为我使用的是Python 3.1并且文本是为2.x编写的,所以我使用的是在http://mcsp./zelle/python找到的GraphicsPy3.py文件,并在我的计算机上将其重命名为graphics.py.
名为futval_graph.py的文件如下:
from graphics import *
def main():
print("This program plots the growth of a 10-year investment.")
principal = eval(input("Enter the initial principal: "))
apr = eval(input("Enter the annualized interest rate: "))
win = GraphWin("Investment Grown Chart", 320, 420)
win.setBackground("white")
Text(Point(20, 230), ' 0.0K').draw(win)
Text(Point(20, 180), ' 2.5K').draw(win)
Text(Point(20, 130), ' 5.0K').draw(win)
Text(Point(20, 80), ' 7.5K').draw(win)
Text(Point(20, 30), '10.0K').draw(win)
# Rest of code is here but I've commented it out to isolate the problem.
main()
当我在新的IDLE会话上运行’import futval_graph’时,程序只需运行然后在输入’apr’后挂起而不打开新的图形窗口.当我从命令行运行程序时,我得到以下结果:
C:\Python31>futval_graph.py
This program plots the growth of a 10-year investment.
Enter the initial principal: error in background error handler:
out of stack space (infinite loop?)
while executing
“::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level 0 -errorco de NONE -errorinfo {out of stack space (infinite loop?)
while execu…”
特别令人沮丧的是,这一系列命令在进入新的IDLE会话时起作用.然后在所有命令自行运行后从IDLE运行’import futval_graph’时,futval_graph正常工作.
所以我的问题是:如何从命令行和IDLE中正确运行futval_graph.py?对不起,如果我对问题的解释有点分散.如果有任何进一步的信息有助于澄清,请告诉我. 解决方法: Python 3版本的graphics.py似乎存在问题.
我下载了Python 3版本,将其重命名为graphics.py,然后执行以下操作.
PS C:\Users\jaraco\Desktop> python
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from graphics import *
>>> dir()
['BAD_OPTION', 'Circle', 'DEAD_THREAD', 'DEFAULT_CONFIG', 'Entry', 'GraphWin', 'GraphicsError', 'GraphicsObject', 'Image', 'Line', 'OBJ_ALREADY_DRAWN', 'Oval',
'Pixmap', 'Point', 'Polygon', 'Queue', 'Rectangle', 'Text', 'Transform', 'UNSUPPORTED_METHOD', '\_\_builtins\_\_', '\_\_doc\_\_', '\_\_name\_\_', '\_\_package\_\_', 'atexit', 'color_rgb', 'copy', 'os', 'sys', 'test', 'time', 'tk']
>>> error in background error handler:
out of stack space (infinite loop?)
while executing
"::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?)
while execu..."
如您所见,我得到了同样的错误,我甚至没有在模块中执行任何操作.库本身似乎存在问题,而不是您在代码中执行的操作.
我建议,我会向作者报告.
如果我只是导入图形模块,我确实发现我没有得到错误.
>>> import graphics
>>> dir(graphics)
我发现如果我对你的代码执行了这个操作,然后将GraphWin的引用更改为graphics.GraphWin,Text to graphics.Text和Point to graphics.Point,问题似乎消失了,我可以从命令行运行它.
import graphics
def main():
print("This program plots the growth of a 10-year investment.")
principal = eval(input("Enter the initial principal: "))
apr = eval(input("Enter the annualized interest rate: "))
win = graphics.GraphWin("Investment Grown Chart", 320, 420)
win.setBackground("white")
graphics.Text(graphics.Point(20, 230), ' 0.0K').draw(win)
graphics.Text(graphics.Point(20, 180), ' 2.5K').draw(win)
graphics.Text(graphics.Point(20, 130), ' 5.0K').draw(win)
graphics.Text(graphics.Point(20, 80), ' 7.5K').draw(win)
graphics.Text(graphics.Point(20, 30), '10.0K').draw(win)
# Rest of code is here but I've commented it out to isolate the problem.
main()
为什么会这样?它不应该.看来graphics.py模块有一些不正常的副作用.
我怀疑你不会在Python 2.x版本下遇到这些错误. 来源:https://www./content-1-286401.html
|