最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】尝试Python的图形库:graphics

Python crifan 11240浏览 0评论

参考Python 3.x的官网解释:

Graphics

去使用Python 3.2.2去试试graphics图形库。


1.参考其解释,去运行代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
Function:
【记录】尝试Python的图形库:graphics
https://www.crifan.com/try_with_python_gui_lib_graphics

Author:     Crifan
Verison:    2012-11-30
-------------------------------------------------------------------------------
"""

from graphics import *;

def graphicsDemo():
    """Demo Python 3.x graphics lib
    """
    
    win = GraphWin();
    pt = Point(100, 50);
    pt.draw(win);
    
    cir = Circle(pt, 25);
    cir.draw(win);
    
###############################################################################
if __name__=="__main__":
    graphicsDemo();

结果却出错:

Traceback (most recent call last):
  File "G:\python\graphics_demo\graphics_demo.py", line 14, in <module>
    from graphics import *;
ImportError: No module named graphics

很明显,找不到对应的graphics模块。

2.再回去参考官网教程好好看看,结果也还是没搞懂:

from the shortcut provided in the examples folder (in the same folder as graphics.py)

中所说的“examples folder”是在哪里。

3.后来网上搜了搜,参考:

Error Importing Graphics Python

才知道,原来这个graphics,是需要另外安装的。

悲催的官网教程,也不写清楚,需要另外安装。。。

4.然后就是去下载上述页面介绍的:

http://mcsp.wartburg.edu/zelle/python/graphics.py

 

不过,另外也去看了看,作者的主页:

http://mcsp.wartburg.edu/zelle/python/

 

5.下载到graphics.py后,放到和当前文件python文件同目录下。

然后再去运行,结果出错,具体过程参见:

【已解决】Python中运行graphics图形库,结果出错:Runtime Error! R6025 pure virtual function call

 

后来证明,只能通过下面的,graphics中的examples中的特殊方式去启动IDLE,然后后续图形界面才可以显示的。

即,graphics不能直接在windows的cmd中运行显示的。

 

6.再去安装教程上所说的,去打开examples文件夹内,IDLE的快捷方式(shortcut),此处即Idle32Shortcut:

Idle32shortcut_thumb

结果出错:

shortcut not exist_thumb

很明显,是因为我此处没有把Python32安装到默认的C:\Python32\中,去看了看Idle32Shortcut的属性就可以看到这点:

Idle32shortcut property_thumb

所以,把对应的

C:\Python32\

改为我此处的:

E:\dev_install_root\Python32

变成:

E:\dev_install_root\Python32\pythonw.exe E:\dev_install_root\Python32\Lib\idlelib\idle.pyw

然后再双击运行,就可以了:

can run python shell_thumb

7.然后接着去测试代码。

然后输入了:

from graphics import *
win = GraphWin()

后,可以正常运行了:

run Graphwin ok

8.接下来,就是继续参考官网教程,去执行对应代码,显示对应的效果了。

注意,还是要一直在通过graphics的examples中的特殊启动方式启动此IDLE,即Python shell,然后才可以正常运行上述代码的。

然后又测试了很多代码:

from graphics import *;
win = GraphWin();
pt = Point(100, 50);
pt.draw(win);
cir = Circle(pt, 25);
cir.draw(win);
cir.setOutline('red')
cir.setFill('blue')
line = Line(pt, Point(150, 100))
line.draw(win)

然后都是可以正常显示的:

run many line can show ok

 

【总结】

1.(根据Python 3.x的官网的描述)graphics图形库,只有在 >=Python 3.2.1的版本上才支持;

2.先要去:

http://anh.cs.luc.edu/python/hands-on/3.1/examples.zip

下载对应的源码压缩包。解压后,才能得到那个所谓的examples文件夹的,里面才有graphics.py和graphIntroSteps.py等等文件的。

3.直接在windows的cmd下,运行对应的graphics的代码,至少我这里是:

4.想要正常运行graphics代码,看到演示效果的话,最好是去需要参考官网的解释:

Graphics

去按照特定的方式,打开IDLE,然后运行代码,然后才可以显示的:

  • 双击对应的版本IDLE的快捷方式,启动IDLE
    • 此处我的是Python 3.2.2,所以用Idle32Shortcut
    • 如果Python不是安装在C盘根目录:C:\Python32\,那么需要修改对应的Python安装路径
    • 然后自己按照官网教程,一行行输入代码,然后就可以看到对应的图形界面,一点点显示出对应的效果的
    • 比如下面的代码:
    • from graphics import *;
      win = GraphWin();
      pt = Point(100, 50);
      pt.draw(win);
      cir = Circle(pt, 25);
      cir.draw(win);
      cir.setOutline('red')
      cir.setFill('blue')
      line = Line(pt, Point(150, 100))
      line.draw(win)
    • 对应的效果:
      • run many line can show ok
  • 右击examples文件夹内的某个python的demo程序,选择IDLE打开
    • 比如此处右击greet.py,选择Edit with IDLE:
      • greet edit with IDLE
    • 然后选择Run -> Run Module F5:
      • greet run run module f5
    • 然后就可以看到运行出来的greeting的图形界面了:
      • can see geet gui

 

总之,想要使用Python 3.x中的graphics,还是有点麻烦的。

转载请注明:在路上 » 【记录】尝试Python的图形库:graphics

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

    93 queries in 0.182 seconds, using 22.18MB memory