python graphics画圆_【记录】尝试Python的图形库:graphics

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

去使用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

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.后来网上搜了搜,参考:

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

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

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

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

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

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

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

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

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

Idle32shortcut_thumb_thumb.png

结果出错:

shortcut-not-exist_thumb_thumb.png

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

Idle32shortcut-property_thumb_thumb.png

所以,把对应的

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_thumb.png

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

然后输入了:from graphics import *

win = GraphWin()

后,可以正常运行了:

run-Graphwin-ok_thumb.png

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_thumb.png

【总结】

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

2.先要去:

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

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

4.想要正常运行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_thumb1.png

右击examples文件夹内的某个python的demo程序,选择IDLE打开比如此处右击greet.py,选择Edit with IDLE:greet-edit-with-IDLE_thumb.png

然后选择Run -> Run Module F5:greet-run-run-module-f5_thumb.png

然后就可以看到运行出来的greeting的图形界面了:can-see-geet-gui_thumb.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值