Diego Church
Diego Church

Reputation: 25

Module turtle has no write member

I'm using Python on VS Code, and although I'm still learning to code in general, I'm a little confused by this.

I'm running this code:

import scipy.integrate
import turtle

ans, err = scipy.integrate.quad(lambda x: x**2,0.,4)

print(ans) 
turtle.write(ans, font=("Comic Sans", 40, "normal"))

As I run the debugger, turtle is marked and the lint says that turtle has no write member, umm right. I ran the code without debugging and the correct window pops up but it closes after half a second. All of this even though I mark the break at the last line where I write with turtle.

To see if something was wrong with my code or not, I ran it in a PY shell and it worked perfectly, no issues.

I'm guessing the issue is VS Code specific, although Im not sure if its how I imported turtle (should I just import the function im using?)

Upvotes: 1

Views: 1206

Answers (1)

cdlane
cdlane

Reputation: 41872

As I run the debugger, turtle is marked and the lint says that turtle has no write member

Turtle exposes two interfaces, a functional one and an object-oriented one. The functional interface is derived at load time, so static analysis tools don't see it, and thus the lint error. Instead of the functional interface:

import scipy.integrate
import turtle

ans, err = scipy.integrate.quad(lambda x: x**2, 0.0, 4)

turtle.write(ans, font=("Comic Sans", 40, "normal"))

Try using the object-oriented interface:

import scipy.integrate
from turtle import Turtle, Screen

ans, err = scipy.integrate.quad(lambda x: x**2, 0.0, 4)

yertle = Turtle()

yertle.write(ans, font=("Comic Sans", 40, "normal"))

the correct window pops up but it closes after half a second

A turtle program normally ends with a call to the mainloop() method (of screen) or function. This turns over event handling to tkinter. Some programming environments don't require it, though I believe they know to disable it. Add a call to .mainloop() as the last thing in your code to see if that resolves your issue:

screen = Screen()
screen.mainloop()

Upvotes: 2

Related Questions