龟模块似乎缺乏turtle.demo()

时间:2016-02-04 09:54:35

标签: python turtle-graphics demo

当我尝试在python 2.7.8 shell中执行以下操作时:

>>> import turtle
>>> turtle.demo()

我收到以下错误:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    turtle.demo()
AttributeError: 'module' object has no attribute 'demo'

这是我生命中第一次尝试使用乌龟,并假设在安装python时会安装它。我无法在计算机上的任何地方找到它,也不知道如何在正确的地方找到它。任何人都可以解释我必须做些什么来至少让turtle.demo()工作吗?

1 个答案:

答案 0 :(得分:0)

由于demo2()if __name__ == "__main__":是在python C:\Python27\Lib\lib-tk\turtle.py 语句下定义的,所以不要尝试从Python shell中运行它们,而是像运行turtle.py库一样运行它一个独立的Python程序:

File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="sha" class="MessageDigestFactoryBean">
        <property name="algorithm" value="SHA1"/>
    </bean>

    <bean id="md5" class="MessageDigestFactoryBean"/>

</beans>


File: Main.java

import java.security.MessageDigest;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {
  public static void main(String[] args) {
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    String d1 = (String) factory.getBean("sha");
    String d2 = (String) factory.getBean("md5");
    System.out.println(d1);
    System.out.println(d2);
  }

}

class MessageDigestFactoryBean implements FactoryBean, InitializingBean {
  private static final String DEFAULT_ALGORITHM = "MD5";

  private String algorithm = DEFAULT_ALGORITHM;

  public Object getObject() throws Exception {
    return this.algorithm;
  }

  public Class getObjectType() {
    return MessageDigest.class;
  }

  public boolean isSingleton() {
    return true;
  }

  public void setAlgorithm(String algorithm) {
    this.algorithm = algorithm;
  }

  public void afterPropertiesSet() throws Exception {
    this.algorithm += " after setting";
  }
}

应该一个接一个地调用两个演示例程。值得观看,只是为了看到撤消功能。